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);
}
}