gpt4 book ai didi

c# - 当我在 gridview 中单击 linkbutton 时如何获取行索引

转载 作者:行者123 更新时间:2023-11-30 14:00:41 25 4
gpt4 key购买 nike

当我点击链接按钮获取行索引时,我必须定位。但是,我无法得到它。

我的 C# 代码:

 int rowIndex = Convert.ToInt32(e.CommandArgument);

当代码出现在此处时会出现错误({“输入字符串的格式不正确。”})但是,例如当我单击按钮字段时它会起作用。我该怎么做?

asp.net代码

  <asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="View"><%#Eval("RSS_Title") %></asp:LinkButton>
</ItemTemplate>

最佳答案

我会这样做:

ASPX

<asp:GridView ID="YourGrid" 
OnRowCommand="YourGrid_RowCommand"
OnRowCreated="YourGrid_RowCreated"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="View">
<%#Eval("RSS_Title") %></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

CS

protected void YourGrid_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="View")
{
int index = Convert.ToInt32(e.CommandArgument);
}
}
protected void YourGrid_RowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var LinkButton2 = (LinkButton)e.Row.FindControl("LinkButton2");
LinkButton2.CommandArgument = e.Row.RowIndex.ToString();
}

}

关于c# - 当我在 gridview 中单击 linkbutton 时如何获取行索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10054157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com