ASP.NET中Gridview读取数据表列表页面常用操作-序号,编辑,删除,删除确认
前台ASPX代码:
实现功能:自动增加的序号列,编辑删除按钮
GridView1_RowDataBound事件:初始化时候新增删除按钮时候的删除确认对话框,详见CS代码.
GridView1_RowCommand事件:点击列表中的编辑和删除按钮后的事件操作,详见CS代码.
关键设置0.设置好GridView1序号列:Text='<%# this.GridView1.PageIndex * this.GridView1.PageSize + this.GridView1.Rows.Count + 1%>
关键设置1.设置好GridView1的DataKeyNames属性为商业逻辑层的数据表中的主键
关键设置2.设置好GridView1的CommandName属性为"Edit"编辑或者"Delete"删除
关键设置3.设置好GridView1的CommandArgument='<%# DataBinder.Eval(Container,"RowIndex") %>'(获取行号)或者
CommandArgument='<%# Eval("ID") %>'(ID为数据表主键)
<asp:GridView ID="GridView1" runat="server" CellPadding="4" DataSourceID="ObjectDataSource1"
ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" Width="778px" DataKeyNames="ID" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="序号" InsertVisible="False">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="5%" />
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# this.GridView1.PageIndex * this.GridView1.PageSize + this.GridView1.Rows.Count + 1%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TradeWay" HeaderText="交易方式" />
<asp:BoundField DataField="CityName" HeaderText="县市" />
<asp:BoundField DataField="TownName" HeaderText="乡镇" />
<asp:TemplateField HeaderText="更新操作" InsertVisible="False" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container,"RowIndex") %>' CausesValidation="false" Text="更新"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除操作" InsertVisible="False" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="DelButton" runat="server" CommandName="Delete" CommandArgument='<%# Eval("ID") %>' CausesValidation="false" Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetBuildingInfoList" TypeName="BLL.ShopInfo">
</asp:ObjectDataSource>
后台CS代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
/*代码测试分页后不能正确运行,已纠正
LinkButton lkbDel = (LinkButton)e.Row.FindControl("DelButton");
lkbDel.Attributes.Add("OnClick", "javascript:return confirm('是否要删除该记录? " + this.GridView1.DataKeys[e.Row.DataItemIndex].Value + "')");
*/
//以下为分页后正常绑定删除确认对话框脚本代码
LinkButton lkbDel = (LinkButton)e.Row.FindControl("DelButton");
int CurrentPageIndex = this.GridView1.PageIndex;
int CurrentPageSize = this.GridView1.PageSize;
int CurrentDateItem = e.Row.DataItemIndex - CurrentPageIndex * CurrentPageSize;
lkbDel.Attributes.Add("OnClick", "javascript:return confirm('是否要删除该记录? " + this.GridView1.DataKeys[CurrentDateItem].Value + "')");
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Response.Write("<script>alert('" + e.CommandName + "');</script>");
if (e.CommandName == "Edit")//如果传来的操作命名名字中是Edit,就编辑记录
{
//获取当前记录主键的办法一
int myKeyID1 = Convert.ToInt32(GridView1.DataKeys[Int32.Parse(e.CommandArgument.ToString())].Value);//根据行号获取主键值
Response.Write("<script>alert('" + myKeyID1.ToString() + "');</script>");
//int myKeyID1 = (int)GridView1.DataKeys["ID"].Value;
//Response.Write(myKeyID.ToString());
//获取当前记录主键的办法二
//绑定行号CommandArgument='<%# DataBinder.Eval(Container,"RowIndex") %>'
string myKeyID2 = e.CommandArgument.ToString();
Response.Write("<script>alert('2" + myKeyID2 + "');</script>");
Response.End();
//EditRecordByID(categoryID);
}
if (e.CommandName == "Delete")//如果传来的命名名字是Delete,就删除记录
{
//获取当前记录主键的办法一
//int myKeyID1 = (int)GridView1.DataKeys["ID"].Value;
//Response.Write(myKeyID1.ToString());
//获取当前记录主键的办法二
string myKeyID2 = e.CommandArgument.ToString();
Response.Write("<script>alert('" + myKeyID2 + "');</script>");
Response.End();
//EditRecordByID(categoryID);
}
}
分享到:
相关推荐
ASP.NET固定GridView表头_SuperTables
asp.net 导出GridView里的数据到Excel中
asp.net中GridView控件的各种操作方法,使用大全,附加数据库。包含:GridView导入导出,多层嵌套,表头表尾,分页排序,结合控件,设置属性,增除删改,主键索引...物超所值,亲情放送!~
asp.net中GridView 的编辑、添加和删除功能,采用vb语言进行编写。
Formatting ASP.NET GridView using jQuery Highlight row on mouseover in GridView using jQuery Set Alternate color for GridView columns using jQuery Change cursor to hand on mouseover in GridView ...
ASP.NET中的数据绑定-gridview,对此不了解的朋友们可以看下。
本文关键词: TonyPatton TonyPatton GridView ASP.NET ASP.NET ASP.Net Asp.net ASP.NET 2.0
Asp.net中GridView的使用详解
asp.net的GridView控件的技术使用 asp.net的GridView控件的技术使用 asp.net的GridView控件的技术使用 asp.net的GridView控件的技术使用 (本人保证绝对实用经典整合!!!)
ASp.NET GridView 编辑模板ASp.NET GridView 编辑模板
asp.net ajax GridView 无刷新
附详细使用说明,非常简单,解决问题直接了当。
文章: http://blog.csdn.net/yysyangyangyangshan/article/details/38807287 asp.net gridview 复杂 复合 表头
asp.net 获取gridview绑定列索引,以便后续根据绑定字段名对各列操作。
在项目中主要用了gridview来显示数据,其中的方法都是常用的。
C# 写的一个简单的 ASP.NET 2.0 GridView 的示例,在示例1的基础上又添加了隐藏列和给ButtonField添加提示窗体的示范。
asp.net中gridview控件中内容导出成文件,同时文件可以存储为word文档或者Excel文档
一个漂亮的GridView CSS 模板样式。 测试过,可用。
ASP.NET的GridView控件自定义模板设计实例
在ASP.NET 2.0中操作数据:在GridView控件中使用TemplateField(源码)