分享

ASP.NET中GridView控件ButtonField的使用

 行走在理想边缘 2019-11-01

  在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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多