C# ASP.NET GridView行索引作为命令参数
如何访问gridview项的行索引并将其显示为buttonfield列按钮中的命令参数C# ASP.NET GridView行索引作为命令参数,c#,asp.net,gridview,C#,Asp.net,Gridview,如何访问gridview项的行索引并将其显示为buttonfield列按钮中的命令参数 <gridview> <Columns> <asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True" CommandArgument=" ? ? ? " /> ..... ..... 我通常使用RowDatabound事件
<gridview>
<Columns>
<asp:ButtonField ButtonType="Button"
CommandName="Edit" Text="Edit" Visible="True"
CommandArgument=" ? ? ? " />
.....
.....
我通常使用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();
}
}
我想这会管用的
<gridview>
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True" CommandArgument="<%# Container.DataItemIndex %>" />
</Columns>
</gridview>
这里有一个非常简单的方法:
说:
ButtonField类自动使用适当的索引值填充CommandArgument属性。对于其他命令按钮,必须手动设置命令按钮的CommandArgument属性。例如,当GridView控件未启用分页时,可以将CommandArgument设置为
所以你不需要手动设置。带有GridViewCommandEventArgs的行命令将使其可访问;e、 g
protected void Whatever_RowCommand( object sender, GridViewCommandEventArgs e )
{
int rowIndex = Convert.ToInt32( e.CommandArgument );
...
}
下面是微软对此的建议 在gridview上添加一个命令按钮并将其转换为模板,然后在本例中为其指定一个commandname“AddToCart”并添加CommandArgument” **我犯的一个错误是,我想在模板按钮上添加操作,而不是直接在RowCommand事件上添加操作。
<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 framework的传统方式和最新版本,具有强类型数据,您不需要使用类似“EMPID”的字符串作为分页,您需要进行一些计算
int index = Convert.ToInt32(e.CommandArgument) % GridView1.PageSize;
它使用单引号:CommandArgument=''@balexandre,如果GridView、其DataItemIndex和not ItemIndex的ASP.NET标记中没有双引号,则不必使用单引号。ItemIndex是针对RepeaterI的,我在尝试时遇到错误“System.Web.UI.WebControl.ButtonField没有数据绑定事件”。但CommandArgument会自动设置为rowindex(请参阅Rich的答案)。但它显示错误索引超出范围。必须为非负数且小于集合的大小。参数名称:System.Collections.ArrayList.get_Item(Int32 index)at System.Web.UI.WebControl.GridViewRowCollection.get_Item(Int32 index)at Philip.Modules.ItemMapping.ViewItemMapping.gvItem_RowCommand(对象发送器,GridViewCommandEventArgs e)当gridview允许分页时,此解决方案不起作用。参见Mohamed Ahmed Abdel Hafez的答案,Container.DisplayIndex与ButtonField.CommandArgument value.Yep完全相同。这是我生命中的5个小时,因为我没有先读这篇文章。试试这个链接,我不知道为什么当gridview允许分页时,down vote Container.DataItemIndex不起作用。此解决方案工作得很好(与ButtonField.CommandArgument值完全相同)+1.
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.
}
}
<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: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
int index = Convert.ToInt32(e.CommandArgument) % GridView1.PageSize;