C# ASP.NET GridView RowIndex 作为 CommandArgument
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/389403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET GridView RowIndex As CommandArgument
提问by mirezus
How can you access and display the row index of a gridview item as the command argument in a buttonfield column button?
如何访问和显示 gridview 项目的行索引作为按钮字段列按钮中的命令参数?
<gridview>
<Columns>
<asp:ButtonField ButtonType="Button"
CommandName="Edit" Text="Edit" Visible="True"
CommandArgument=" ? ? ? " />
.....
采纳答案by George
Here is a very simple way:
这是一个非常简单的方法:
<asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True"
CommandArgument='<%# Container.DataItemIndex %>' />
回答by Dillie-O
I typically bind this data using the RowDatabound event with the GridView:
我通常使用 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();
}
}
回答by Christopher Edwards
I think this will work.
我认为这会奏效。
<gridview>
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True" CommandArgument="<%# Container.DataItemIndex %>" />
</Columns>
</gridview>
回答by fARcRY
void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Button b = (Button)e.CommandSource;
b.CommandArgument = ((GridViewRow)sender).RowIndex.ToString();
}
回答by Rich
MSDNsays that:
MSDN说:
The ButtonField class automatically populates the CommandArgument property with the appropriate index value. For other command buttons, you must manually set the CommandArgument property of the command button. For example, you can set the CommandArgument to <%# Container.DataItemIndex %> when the GridView control has no paging enabled.
ButtonField 类使用适当的索引值自动填充 CommandArgument 属性。对于其他命令按钮,您必须手动设置命令按钮的 CommandArgument 属性。例如,当 GridView 控件未启用分页时,您可以将 CommandArgument 设置为 <%# Container.DataItemIndex %>。
So you shouldn't need to set it manually. A row command with GridViewCommandEventArgs would then make it accessible; e.g.
所以你不需要手动设置它。带有 GridViewCommandEventArgs 的行命令将使其可访问;例如
protected void Whatever_RowCommand( object sender, GridViewCommandEventArgs e )
{
int rowIndex = Convert.ToInt32( e.CommandArgument );
...
}
回答by Cesar Duran
Here is Microsoft Suggestion for this http://msdn.microsoft.com/en-us/library/bb907626.aspx#Y800
这是微软对此的建议 http://msdn.microsoft.com/en-us/library/bb907626.aspx#Y800
On the gridview add a command button and convert it into a template, then give it a commandname in this case "AddToCart" and also add CommandArgument "<%# ((GridViewRow) Container).RowIndex %>"
在 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>
Then for create on the RowCommand event of the gridview identify when the "AddToCart" command is triggered, and do whatever you want from there
然后在 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.
}
}
**One mistake I was making is that I wanted to add the actions on my template button instead of doing it directly on the RowCommand Event.
**我犯的一个错误是我想在我的模板按钮上添加操作,而不是直接在 RowCommand 事件上执行。
回答by Bala
<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>
This is the traditional way and latest version of asp.net framework having strongly typed data and you don't need to use as string like "EMPID"
这是具有强类型数据的传统方式和最新版本的asp.net框架,您不需要使用像“EMPID”这样的字符串
回答by Mohamed Ahmed Abdel-Hafez
<asp:LinkButton ID="LnkBtn" runat="server" Text="Text" RowIndex='<%# Container.DisplayIndex %>' CommandArgument='<%# Eval("??") %>' OnClick="LnkBtn_Click" />
inside your event handler :
在您的事件处理程序中:
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