简单从asp.net Web应用程序中的gridview中选择一行

TBo*_*dan 5 c# asp.net gridview row web

我知道这个问题已被问了一百次,但是我很难实现不同的解决方案.我需要从asp.net web应用程序C#中的网格视图中检索一个选定的行.我已经完成了数据绑定.我不想要使用编辑/更新按钮或复选框/单选按钮,只需通过单击行选择它.请帮助,我有点卡住了,我不想实现基于javascript的解决方案.谢谢.

if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("OnMouseOver", "this.style.cursor='pointer';this.style.textDecoration='underline';");
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
            e.Row.ToolTip = "Click on select row";
            e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(this.SingleSelectGrid, "Select$" + e.Row.RowIndex);


            LinkButton selectbutton = new LinkButton()
            {
                CommandName = "Select",
                Text = e.Row.Cells[0].Text
            };
            e.Row.Cells[0].Controls.Add(selectbutton);
            e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(selectbutton, "");


        }
Run Code Online (Sandbox Code Playgroud)

Tho*_*and 6

如果我做对了,这应该做你想要的:

的.aspx:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"    
  DataKeyNames="id" onselectedindexchanged="GridView1_SelectedIndexChanged">
Run Code Online (Sandbox Code Playgroud)

代码背后:

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    int index = Convert.ToInt16(GridView1.SelectedDataKey.Value);

}
Run Code Online (Sandbox Code Playgroud)

这应该做你想要的..index将为你提供所选的行ID,由DataKeyNames.aspx页面中的属性提供.但是,这需要检查"启用选择".(转到.aspx页面,设计器,单击gridview,您应该看到"启用选择"属性).