【问题标题】:ASP.NET GridView RowIndex As CommandArgumentASP.NET GridView RowIndex 作为 CommandArgument
【发布时间】:2010-09-28 05:06:03
【问题描述】:

如何访问和显示 gridview 项目的行索引作为按钮字段列按钮中的命令参数?

<gridview>
<Columns>
   <asp:ButtonField  ButtonType="Button" 
        CommandName="Edit" Text="Edit" Visible="True" 
        CommandArgument=" ? ? ? " />
.....

【问题讨论】:

标签: c# asp.net gridview


【解决方案1】:

这是一个非常简单的方法:

<asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True" 
                 CommandArgument='<%# Container.DataItemIndex %>' />

【讨论】:

  • 对于 GridView,它的 DataItemIndex 而不是 ItemIndex。 ItemIndex 用于中继器
  • 我收到错误“System.Web.UI.WebControls.ButtonField 没有 DataBinding 事件。”当我尝试时。但是 CommandArgument 会自动设置为 rowindex(请参阅 Rich 的答案)。
  • 但它显示错误索引超出范围。必须是非负数且小于集合的大小。参数名称:索引在 System.Collections.ArrayList.get_Item(Int32 index) at System.Web.UI.WebControls.GridViewRowCollection.get_Item(Int32 index) at Philip.Modules.ItemMapping.ViewItemMapping.gvItem_RowCommand(Object sender, GridViewCommandEventArgs e)
  • 当gridview允许分页时,此方案不起作用。请参阅 Mohamed Ahmed Abdel-Hafez 的回答,Container.DisplayIndex 与 ButtonField.CommandArgument 值完全一样。
【解决方案2】:

MSDN 说:

ButtonField 类自动使用适当的索引值填充 CommandArgument 属性。对于其他命令按钮,您必须手动设置命令按钮的 CommandArgument 属性。例如,当 GridView 控件未启用分页时,您可以将 CommandArgument 设置为 。

所以您不需要手动设置它。然后,带有 GridViewCommandEventArgs 的行命令将使其可访问;例如

protected void Whatever_RowCommand( object sender, GridViewCommandEventArgs e )
{
    int rowIndex = Convert.ToInt32( e.CommandArgument );
    ...
}

【讨论】:

  • 是的。那是我生命中的 5 个小时,因为我没有先阅读这篇文章,所以我刚刚失去了。
【解决方案3】:

这是微软对此的建议 http://msdn.microsoft.com/en-us/library/bb907626.aspx#Y800

在网格视图上添加一个命令按钮并将其转换为模板,然后在这种情况下给它一个命令名称“AddToCart”并添加CommandArgument “"

<asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="AddButton" runat="server" 
      CommandName="AddToCart" 
      CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
      Text="Add to Cart" />
  </ItemTemplate> 
</asp:TemplateField>

然后在 gridview 的 RowCommand 事件上创建,确定何时触发“AddToCart”命令,然后从那里做任何你想做的事情

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "AddToCart")
  {
    // Retrieve the row index stored in the 
    // CommandArgument property.
    int index = Convert.ToInt32(e.CommandArgument);

    // Retrieve the row that contains the button 
    // from the Rows collection.
    GridViewRow row = GridView1.Rows[index];

    // Add code here to add the item to the shopping cart.
  }
}

**我犯的一个错误是我想在模板按钮上添加操作,而不是直接在 RowCommand 事件上执行。

【讨论】:

    【解决方案4】:

    我认为这会奏效。

    <gridview>
    <Columns>
    
                <asp:ButtonField  ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True" CommandArgument="<%# Container.DataItemIndex %>" />
            </Columns>
    </gridview>
    

    【讨论】:

    • 单引号:CommandArgument=''
    • @balexandre,当 ASP.NET 标签内没有双引号时,不需要单引号
    【解决方案5】:
    void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Button b = (Button)e.CommandSource;
        b.CommandArgument = ((GridViewRow)sender).RowIndex.ToString();
    }
    

    【讨论】:

      【解决方案6】:

      我通常使用 RowDatabound 事件将这些数据与 GridView 绑定:

      protected void FormatGridView(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
      {
         if (e.Row.RowType == DataControlRowType.DataRow) 
         {
            ((Button)e.Row.Cells(0).FindControl("btnSpecial")).CommandArgument = e.Row.RowIndex.ToString();
         }
      }
      

      【讨论】:

        【解决方案7】:
        <asp:TemplateField HeaderText="" ItemStyle-Width="20%" HeaderStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:LinkButton runat="server" ID="lnkAdd" Text="Add" CommandName="Add" CommandArgument='<%# Eval("EmpID"))%>' />
                            </ItemTemplate>
                        </asp:TemplateField>
        

        这是asp.net框架的传统方式和最新版本,具有强类型数据,不需要像“EMPID”这样的字符串

        【讨论】:

          【解决方案8】:
          <asp:LinkButton ID="LnkBtn" runat="server" Text="Text" RowIndex='<%# Container.DisplayIndex %>' CommandArgument='<%# Eval("??") %>' OnClick="LnkBtn_Click" />
          

          在您的事件处理程序中:

          Protected Sub LnkBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
            dim rowIndex as integer = sender.Attributes("RowIndex")
            'Here you can use also the command argument for any other value.
          End Sub
          

          【讨论】:

          • 我不知道为什么投反对票,当gridview允许分页时Container.DataItemIndex不起作用。该解决方案工作得很好(与 ButtonField.CommandArgument 值完全相同)。 +1
          【解决方案9】:

          分页需要做一些计算

          int index = Convert.ToInt32(e.CommandArgument) % GridView1.PageSize;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-06-19
            • 2011-04-18
            • 2012-01-19
            • 1970-01-01
            • 1970-01-01
            • 2012-04-01
            • 2017-12-22
            • 2014-04-25
            相关资源
            最近更新 更多