如何在Gridview的ButtonField列按钮中作为命令参数访问并显示行索引?
<gridview>
<Columns>
<asp:ButtonField ButtonType="Button"
CommandName="Edit" Text="Edit" Visible="True"
CommandArgument=" ? ? ? " />
.....
如何在Gridview的ButtonField列按钮中作为命令参数访问并显示行索引?
<gridview>
<Columns>
<asp:ButtonField ButtonType="Button"
CommandName="Edit" Text="Edit" Visible="True"
CommandArgument=" ? ? ? " />
.....
这里有一个非常简单的方法:
<asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True"
CommandArgument='<%# Container.DataItemIndex %>' />
MSDN指出:
ButtonField类会自动将CommandArgument属性填充为适当的索引值。对于其他命令按钮,您必须手动设置命令按钮的CommandArgument属性。例如,在GridView控件未启用分页时,可以将CommandArgument设置为<%# Container.DataItemIndex %>。
因此,您不需要手动设置它。使用GridViewCommandEventArgs的行命令将使其可访问,例如:
protected void Whatever_RowCommand( object sender, GridViewCommandEventArgs e )
{
int rowIndex = Convert.ToInt32( e.CommandArgument );
...
}
这里是微软的建议: 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事件中执行。
<gridview>
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True" CommandArgument="<%# Container.DataItemIndex %>" />
</Columns>
</gridview>
void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Button b = (Button)e.CommandSource;
b.CommandArgument = ((GridViewRow)sender).RowIndex.ToString();
}
<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的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();
}
}
<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"一样使用字符串。
使用分页功能需要进行一些计算。
int index = Convert.ToInt32(e.CommandArgument) % GridView1.PageSize;