[转]GridView中直接新增行、编辑和删除
本文转自: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中直接新增行、编辑和删除的更多相关文章
- jquery-easyui 中表格的行编辑功能
具体实现代码如下: <table id="tt"></table> $('#tt').datagrid({ title:'Editable DataGrid ...
- DevExpress GridControl控件行内新增、编辑、删除添加选择框
以下为内容以图片居多1234表示点击顺序 先新增一行 操作和新增数据行一样 打开ColumnEdit 选择new ButtenEdit new上方会出现一个系统命名的button 命名可以更改必须 ...
- jquery-easyui中表格的行编辑功能
datagrid现在具有行编辑能力了,使用时只须在columns中为需要编辑的列添加一个editor属性,编辑保存时同时具有数据校验能力. 看一个例子效果图: 代码如下: $('#tt').datag ...
- Gridview中奇偶数行颜色设置
在gridview中的RowDataBound事件里面写 switch (e.Row.RowType) {case DataControlRowType.Header: e.Row.BackColor ...
- layui 学习笔记一:layui table 查询、新增、编辑、删除
一.table数据的呈现(对应查询) 页面代码: @{ ViewBag.Title = "TableGrid"; } @section styles{ <link href= ...
- el-table单元格新增、编辑、删除功能
<template> <div class="box"> <el-button class="addBtn" type=" ...
- EasyUI中datagrid的行编辑模式中,找到特定的Editor,并为其添加事件
有时候在行编辑的时候,一个编辑框的值要根据其它编辑框的值进行变化,那么可以通过在开启编辑时,找到特定的Editor,为其添加事件 // 绑定事件, index为当前编辑行 var editors = ...
- (原创)EasyUI中datagrid的行编辑模式中,找到特定的Editor,并为其添加事件
有时候在行编辑的时候,一个编辑框的值要根据其它编辑框的值进行变化,那么可以通过在开启编辑时,找到特定的Editor,为其添加事件 // 绑定事件, index为当前编辑行 var editors = ...
- DevExpress GridControl控件行内新增、编辑、删除添加选择框(转)
http://blog.csdn.net/m1654399928/article/details/21951519 1.首先到GridControl控件设计里设置属性Repository (In ...
随机推荐
- WPF 竖排文字
---恢复内容开始--- 想做一个WPF 文字竖排 类似上图.用在TabItem的header上面. <TextBlock FontSize="30" Text=" ...
- 老毛桃安装Win8(哪里不会点哪里,so easy)
先来一张美女图,是不是很漂亮呢!继续往下看! 英雄不问出路,美女不看岁数!求推荐啊! 每次碰到妹子找我装系统的时候我都毫不犹豫的答应了,心里暗暗想到:好好表现啊!此刻的心情比见家长还要激动和紧张! 有 ...
- Log4net中的调错
在使用log4net时,感觉最麻烦的就是配置文件了,为了使用方便,我不得不先准备好一个完整的配置文件方案,测试了输出到文本.控制台.windows事件.SQL Server数据库都没有问题,但输出到o ...
- AX2012 R3 Data upgrade checklist sync database step, failed to create a session;
最近在做AX2012 R3 CU9 到CU11的upgrade时 (用的Admin帐号), 在Date upgrade 的 synchronize database 这步 跑了一半,报出错误 说“fa ...
- 破解入门【OllyDebug爆破程序】
逆向破解这块我也是个刚起步的小菜,入门都还算不上吧,看了点基础教程,先动手练习一下增加点兴趣.嘿嘿 工具: peid //查壳工具 OllyDebug //反汇编.动态调试工具 ...
- Struts2--Helloworld
搭建Struts2环境时,我们一般需要做以下几个步骤的工作: 1.找到开发Struts2应用需要使用到的jar文件. 2.创建Web工程 3.在web.xml中加入Struts2 MVC框架启动配置 ...
- IOS NSThread
任何一个 iOS 应用程序都是由一个或者多个线程构成的.无论你是否使用了多线程编程技术,至少有 1 个 线程被创建.多线程就是为了提高引用程序的工作效率!避免阻塞主线程!当我们没有用任何多线程技术的话 ...
- UnityShader之Shader格式篇【Shader资料1】
关于Shader,在Unity里面我们一般叫做ShaderLab,只要你的职业是与渲染搭边,Unity就与ShaderLab有着直接的关联,你都应该试着去学会它,其实我们在新手未有入门的时候,我们总是 ...
- iOS9 HTTP 不能正常使用的解决办法
Google后查证,iOS9引入了新特性App Transport Security (ATS).详情:App Transport Security (ATS) 新特性要求App内访问的网络必须使用H ...
- 万恶的hao123
Windows 10没办法直接在系统菜单栏上修改快捷图标的参数 在确认系统里面没有流氓软件之后,只能手工到文件夹下去修改了 C:\Users\你的用户名\AppData\Roaming\Microso ...