5

I know this question had been asked a hundred times, but I have difficulties implementing different solutions.I need to retrieve a selected row from a grid view in asp.net web application C#.I've done the databinding.I don't want to use edit/update buttons or checkboxe/radio button, just to select it by clicking on the row.Please help, I'm a little stuck, and I would like not to implement javascript based solutions.Thanks.

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, "");


        }
4

2 回答 2

7

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

.aspx:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"    
  DataKeyNames="id" onselectedindexchanged="GridView1_SelectedIndexChanged">

后面的代码:

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

}

这应该做你想要的。.index 会给你选择的行 ID,由DataKeyNames你的 .aspx 页面中的属性提供。但是,这确实需要检查“启用选择”。(转到您的 .aspx 页面,设计器,单击您的网格视图,您应该会看到“启用选择”属性)。

于 2012-06-22T22:26:22.793 回答
2

如果要从文件后面的代码中添加DataSource ,则必须将名为“ AutoGenerateSelectButton ”的属性设置为True。这将使您能够选择一行。

于 2013-04-01T20:21:56.163 回答