ASP.NET中GridView控件ButtonField的使用

ASP.NET中GridView控件ButtonField的使用

  在ASP.NET的GridView控件中使用ButtonField列,通过当前行数据绑定按钮的显示文字,即按钮的text属性,以及对应的后台单击事件。

1.前台页面部分代码  

 1     <asp:GridView runat="server" ID="GridViewBooks" AutoGenerateColumns="false" CellPadding="4"
 2                          OnRowCommand="GridView_OnRowCommand"
 3                          AllowSorting="true"  >
 4                         <Columns >
 5                             <asp:BoundField DataField ="ID"  HeaderText="ID" ReadOnly = "true" /> 
 6                             <asp:BoundField DataField="name" HeaderText="书名" ReadOnly ="true" />
 7                             <asp:BoundField DataField="count" HeaderText="数量" ReadOnly ="true" />
 8                             <asp:BoundField DataField="status" HeaderText="状态" ReadOnly ="true" />
 9                             <asp:ButtonField ButtonType="Button" Text="" HeaderText="操作" CommandName="Btn_Operation" />
10                         </Columns>
11                         <%--... --%>                       
12                         </asp:GridView>

在GridView控件中添加ButtonField列,并设置按钮类型、(为Button(普通按钮))、显示文本(可以不用设置)、列名、以及传递的参数(这里设置的是CommandName,用来唯一标识列),在GridView属性中添加OnRowCommand事件。

2.后台实现

1         protected void GridView_OnRowCommand(object sender, GridViewCommandEventArgs e)
2         {
3             if (e.CommandName == "Btn_Operation")
4             {
5                 int index = Convert.ToInt32(e.CommandArgument);
6                 //...
7 
8             }
9         }

  这里ButtonField 类自动以适当的索引值填充 CommandArgument 属性,即e.CommandArgument值为GirdView当前的行号(从0开始);e.Command为列的CommandName属性。

 

2015年12月28日11:27:01

 

posted @ 2015-12-28 11:27  Zima  阅读(15836)  评论(0编辑  收藏  举报