ASP.NET GridView行索引作为CommandArgument

57

如何在Gridview的ButtonField列按钮中作为命令参数访问并显示行索引?

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

请尝试此链接 get-gridview-rowindex-upon-button-click - melaouhia mohamed amine
9个回答

97

这里有一个非常简单的方法:

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

3
GridView控件中使用DataItemIndex而不是ItemIndex。ItemIndex用于Repeater控件。 - Tawani
4
当我尝试时,收到了错误提示“System.Web.UI.WebControls.ButtonField没有DataBinding事件”。但是CommandArgument会自动设置为行索引(请参考Rich的答案)。 - joeriks
但它显示错误索引超出范围。必须是非负数且小于集合的大小。 参数名称:索引 在 System.Collections.ArrayList.get_Item(Int32 index) 在 System.Web.UI.WebControls.GridViewRowCollection.get_Item(Int32 index) 在 Philip.Modules.ItemMapping.ViewItemMapping.gvItem_RowCommand(Object sender, GridViewCommandEventArgs e)。 - ranjenanil
当GridView允许分页时,此解决方案无效。请参见Mohamed Ahmed Abdel-Hafez的答案,Container.DisplayIndex正好像ButtonField.CommandArgument值一样有效。 - Osvier

35

MSDN指出:

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

因此,您不需要手动设置它。使用GridViewCommandEventArgs的行命令将使其可访问,例如:

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

3
是的。因为我没有先阅读这篇文章,所以我浪费了5个小时的时间。 - Daniel Szabo

13

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

在 GridView 上添加一个命令按钮,并将其转换为模板,然后给它一个命令名称,在这种情况下为 "AddToCart",还要添加 CommandArgument "<%# ((GridViewRow) Container).RowIndex %>"

<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='<%# Container.DataItemIndex %>' - balexandre
3
@balexandre,在ASP.NET标记内部没有双引号时,不需要使用单引号。 - AaA

1
void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    Button b = (Button)e.CommandSource;
    b.CommandArgument = ((GridViewRow)sender).RowIndex.ToString();
}

0
<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

我不知道为什么会有负投票,Container.DataItemIndex在允许分页的GridView中不能工作。这个解决方案很好用(就像ButtonField.CommandArgument值一样)。+1 - Osvier

0

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

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();
   }
}

0
<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"一样使用字符串。


0

使用分页功能需要进行一些计算。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接