【问题标题】:gridview button fieldgridview 按钮字段
【发布时间】:2012-09-17 04:35:12
【问题描述】:

我有一个带有按钮域的网格视图。我想更新我的数据库中的表并更改按钮单击时的按钮图像。我也可以访问行索引的最佳事件是什么? 我尝试使用 RowCommand 事件,但无法从该事件访问行索引

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

    您可以尝试使用此代码 - 基于 CommandArgument

    void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
        if(e.CommandName=="Test")
        {
          int index = Convert.ToInt32(e.CommandArgument);
          GridViewRow row = CustomersGridView.Rows[index];
        }
      }
    

    链接:http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.gridview.rowcommand(v=vs.80).aspx

    【讨论】:

      【解决方案2】:

      在其中使用 asp:TemplateFieldasp:Button 而不是 asp:ButtonField。设置一个CommandName 说,MyCommand。现在在RowCommand 事件中,执行此操作

      var clickedRow = (GridViewRow)((Button)sender).NamingContainer;
      var clickedIndex = clickedRow.RowIndex;
      

      【讨论】:

        【解决方案3】:

        我刚刚检查了这里的文档http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

        如果您查看“注释”部分(为方便起见,请参见下方),您应该能够从 CommandArguement 属性中获取 RowIndex:

        注意要确定引发事件的行的索引,请使用 传递给事件参数的 CommandArgument 属性 事件。 ButtonField 类自动填充 具有适当索引值的 CommandArgument 属性。对于他人 命令按钮,您必须手动设置的 CommandArgument 属性 命令按钮。例如,您可以将 CommandArgument 设置为 当 GridView 控件没有分页时 已启用。

        【讨论】:

          猜你喜欢
          • 2015-03-14
          • 2015-12-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多