首页 > Web开发 > 详细

ASP.NET中GridView控件ButtonField的使用

时间:2015-12-28 12:17:56      阅读:840      评论:0      收藏:0      [点我收藏+]

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

 

ASP.NET中GridView控件ButtonField的使用

原文:http://www.cnblogs.com/forevertime/p/5082013.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!