本文转自:http://www.cnblogs.com/gdjlc/archive/2009/11/10/2086951.html

.aspx 

<div><asp:Button runat="server" ID="btnAdd" Text="新增" OnClick="btnAdd_Click" /></div>
<asp:GridView ID="gv" runat="server" AllowPaging="True" AllowSorting="True" DataKeyNames="id"
AutoGenerateColumns="False" OnRowCancelingEdit="gv_RowCancelingEdit" OnRowEditing="gv_RowEditing"
OnRowUpdating="gv_RowUpdating" OnRowDeleting="gv_RowDeleting" OnRowDataBound="gv_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="名称">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("txtName") %>'></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfvtxtName" ControlToValidate="txtName"
Display="Dynamic" ErrorMessage="*不能为空" ForeColor="Red"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("txtName") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="状态">
<EditItemTemplate>
<asp:DropDownList ID="ddlIsEnable" runat="server">
<asp:ListItem Value="True">启用</asp:ListItem>
<asp:ListItem Value="False">停用</asp:ListItem>
</asp:DropDownList>
<asp:HiddenField ID="hfIsEnable" runat="server" Value='<%# Eval("IsEnable")%>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblIsEnable" runat="server" Text='<%# Eval("IsEnable").ToString() == "True" ? "启用" : "停用" %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="排序号">
<EditItemTemplate>
<asp:TextBox ID="txtOrder" runat="server" Text='<%# Bind("Order") %>'></asp:TextBox>
<asp:RegularExpressionValidator runat="server" ID="revOrder" ControlToValidate="txtOrder"
ValidationExpression="\d+" Display="Dynamic" ErrorMessage="*必须为整数"></asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblOrder" runat="server" Text='<%# Eval("Order") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="true" ShowEditButton="true" EditText="编辑" DeleteText="删除"
CancelText="取消" HeaderText="操作" />
</Columns>
<EmptyDataTemplate>
没有您查询的数据
</EmptyDataTemplate>
<PagerSettings Visible="false" />
</asp:GridView> .aspx.cs public partial class CategoryList : System.Web.UI.Page
{
private static List<Category> cateList;
private ICategory iCate = IOC.R<ICategory>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGV(true);
}
}
private void BindGV(bool refresh)
{
if (refresh || cateList == null)
{
cateList = iCate.List();
}
this.gv.DataSource = cateList;
this.gv.DataBind();
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
BindGV(false);
HiddenField hfIsEnable = (HiddenField)gv.Rows[e.NewEditIndex].FindControl("hfIsEnable");
DropDownList ddlIsEnable = (DropDownList)gv.Rows[e.NewEditIndex].FindControl("ddlIsEnable");
ddlIsEnable.SelectedValue = hfIsEnable.Value;
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -;
BindGV(false);
}
//更新
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
long id = Convert.ToInt32(gv.DataKeys[e.RowIndex].Values[].ToString());
string name = ((TextBox)gv.Rows[e.RowIndex].FindControl("txtName")).Text.Trim();
bool isEnable = Convert.ToBoolean(((DropDownList)gv.Rows[e.RowIndex].FindControl
lIsEnable")).SelectedValue);
string txtOrder = ((TextBox)gv.Rows[e.RowIndex].FindControl("txtOrder")).Text;
int order = string.IsNullOrEmpty(txtOrder) ? : Convert.ToInt32(txtOrder); long newId = id;
Category category = iCate.Get(id);
if (category != null)//如果数据库已存在该条记录,则更新
{
iCate.Update(id, c =>
{
c.Name = name;
c.IsEnable = isEnable;
c.Order = order;
});
}
else//新增
{
Category cNew = new Category
{
Name = name,
IsEnable = isEnable,
Order = order
};
iCate.Insert(cNew);
newId = cNew.ID;
}
Category cate = cateList.Where(c => c.ID == id).ToList().SingleOrDefault();
if (cate != null)
{
cate.ID = newId;
cate.Name = name;
cate.IsEnable = isEnable;
cate.Order = order;
} gv.EditIndex = -;
BindGV(false);
}
//删除
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
long id = Convert.ToInt64(gv.DataKeys[e.RowIndex].Values[].ToString());
Category cate = cateList.Where(c => c.ID == id).ToList().SingleOrDefault();
if (cate != null)
{
cateList.Remove(cate);
}
iCate.Delete(id);
this.BindGV(false);
}
//新增一行记录
protected void btnAdd_Click(object sender, EventArgs e)
{
long maxId = cateList.Max(c => c.ID) + ;
Category cate = new Category()
{
ID = maxId,
Name = "",
Order = ,
IsEnable = true
};
cateList.Add(cate);
this.gv.EditIndex = cateList.Count - ;
this.BindGV(false);
}
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btn = ((LinkButton)e.Row.Cells[].Controls[]);
if (btn.Text == "删除")
{
btn.Attributes.Add("onclick", "javascript:return confirm('您确信要删除吗!?')");
}
}
}
}

[转]GridView中直接新增行、编辑和删除的更多相关文章

  1. jquery-easyui 中表格的行编辑功能

    具体实现代码如下: <table id="tt"></table> $('#tt').datagrid({ title:'Editable DataGrid ...

  2. DevExpress GridControl控件行内新增、编辑、删除添加选择框

    以下为内容以图片居多1234表示点击顺序 先新增一行 操作和新增数据行一样 打开ColumnEdit  选择new ButtenEdit  new上方会出现一个系统命名的button 命名可以更改必须 ...

  3. jquery-easyui中表格的行编辑功能

    datagrid现在具有行编辑能力了,使用时只须在columns中为需要编辑的列添加一个editor属性,编辑保存时同时具有数据校验能力. 看一个例子效果图: 代码如下: $('#tt').datag ...

  4. Gridview中奇偶数行颜色设置

    在gridview中的RowDataBound事件里面写 switch (e.Row.RowType) {case DataControlRowType.Header: e.Row.BackColor ...

  5. layui 学习笔记一:layui table 查询、新增、编辑、删除

    一.table数据的呈现(对应查询) 页面代码: @{ ViewBag.Title = "TableGrid"; } @section styles{ <link href= ...

  6. el-table单元格新增、编辑、删除功能

    <template> <div class="box"> <el-button class="addBtn" type=" ...

  7. EasyUI中datagrid的行编辑模式中,找到特定的Editor,并为其添加事件

    有时候在行编辑的时候,一个编辑框的值要根据其它编辑框的值进行变化,那么可以通过在开启编辑时,找到特定的Editor,为其添加事件 // 绑定事件, index为当前编辑行 var editors = ...

  8. (原创)EasyUI中datagrid的行编辑模式中,找到特定的Editor,并为其添加事件

    有时候在行编辑的时候,一个编辑框的值要根据其它编辑框的值进行变化,那么可以通过在开启编辑时,找到特定的Editor,为其添加事件 // 绑定事件, index为当前编辑行 var editors = ...

  9. DevExpress GridControl控件行内新增、编辑、删除添加选择框(转)

    http://blog.csdn.net/m1654399928/article/details/21951519 1.首先到GridControl控件设计里设置属性Repository    (In ...

随机推荐

  1. Python好用的网站收集

    第三方Pthon包查找:http://www.lfd.uci.edu/ http://www.cnblogs.com/lanxuezaipiao/p/3543658.html

  2. 【AngularJS学习笔记】02 小杂烩及学习总结

    表格示例 <div ng-app="myApp" ng-controller="customersCtrl"> <table> < ...

  3. 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedFilesVector VirtualizedItemsVector 绑定

    [源码下载] 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedF ...

  4. 将表数据生成Insert脚本

    set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo-- =============================================-- Author ...

  5. 程序设计模式 —— State 状态模式

    我应该如何阅读? 本文将使用优雅的文字风格来告诉你什么是状态模式. 注意: 1.在阅读本文之前请保证你已经掌控了 面对对象的思想与 多态的基本概念,否则将难以理解. 2.本文实现将用C++实现,你不一 ...

  6. 回文串---Palindrome

    POJ   3974 Description Andy the smart computer science student was attending an algorithms class whe ...

  7. fibonacci数列从a到b的个数

    Description 我们定义斐波那契数列如下: f1=1 f2=2 f(n)=f(n-1)+f(n-2)(n>=3)   现在,给定两个数a和b,计算有多少个斐波那契数列中的数在a和b之间( ...

  8. CSS之浮动那些事

    1.清除浮动 下面是两种常用的方式,而这两招也够用了(不用千招会,只需一招精). 1.结尾处加空div标签 clear:both <style type="text/css" ...

  9. 初学Node(四)事件循环

    Node中的事件循环 事件循环是Node的核心,正是因为有了事件循环JS才能够在服务端占有一席之地.JS是一种单线程语言,但是它的执行环境是多线程的在加上JS的事件驱动这一特点,使使JS在执行的过程中 ...

  10. Force.com微信开发系列(七)OAuth2.0网页授权

    OAuth是一个开放协议,允许用户让第三方应用以安全且标准的方式获取该用户在某一网站上存储的私密资源(如用户个人信息.照片.视频.联系人列表),而无须将用户名和密码提供给第三方应用.本文将详细介绍OA ...