0

我在 asp.net gridview 中有一个超链接字段,它在 aspx 文件中运行代码,例如

<asp:GridView ID="gvCoursesList" runat="server">
    <Columns>
        <asp:BoundField DataField="Course" HeaderText="Course">
        </asp:BoundField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lblSignup" runat="server" Text="Sign Up" OnClick= "lblSignup_Click" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

在我的文件后面的代码中,我具有以下功能:

    protected void lblSignup_Click(object sender, EventArgs e)
    {
    }

如何从此函数中检索第一列的值。我想做类似的事情

string course = gvCoursesList.DataKeys[e.RowIndex].Value.ToString();

但它不起作用。还有其他想法吗?

4

4 回答 4

1

@jishnusaha 应该是您应该采取的方法。这是另一种选择:http ://forums.asp.net/p/1137287/1821091.aspx

protected void lblSignup_Click(object sender, EventArgs e)
{
    LinkButton btn = sender as LinkButton;
    GridViewRow row = btn.NamingContainer as GridViewRow;

    string course = gvCoursesList.DataKeys[row.RowIndex].Value.ToString();
}
于 2013-03-12T19:47:55.703 回答
1
<asp:GridView ID="gvCoursesList" runat="server" OnRowCommand="gvCoursesList_RowCommand">
<Columns>
    <asp:BoundField DataField="Course" HeaderText="Course">
    </asp:BoundField>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="lblSignup" runat="server" Text="Sign Up" CommandName="SignUp" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

protected void gvCoursesList_RowCommand(object sender, GridViewRowCommandEventArgs e)
{
  if(e.CommandName=="SignUp")
  {
     string course = gvCoursesList.DataKeys[e.RowIndex].Value.ToString();
  }
}
于 2013-03-12T19:38:01.747 回答
0

感谢codingbiz。他的解决方案只需要根据以下内容进行一些调整。

LinkButton btn = sender as LinkButton;
GridViewRow row = btn.NamingContainer as GridViewRow;
string course = row.Cells[0].Text;
于 2013-03-12T20:06:36.547 回答
0

使用 .cells.text 获取值:

protected void lblSignup_Click(object sender, EventArgs e)
{
    GridViewRow row = gvCoursesList.rows[e.rowindex]
    string course = row.cells[cellIndex].text;
}
于 2013-03-12T19:37:14.173 回答