我知道这个问题已被问过一百次,但我在实现不同的解决方案时遇到了困难。我需要从 asp.net Web 应用程序 C# 中的 GridView 中检索选定的行。我已经完成了数据绑定(bind)。我不知道不想使用编辑/更新按钮或复选框/单选按钮,只是通过单击行来选择它。请帮忙,我有点卡住了,我不想实现基于 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, "");
}
最佳答案
如果我做对了,这应该可以满足您的要求:
.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,由 .aspx 页面中的 DataKeyNames
属性提供。然而,这确实需要选中“启用选择”。 (转到您的 .aspx 页面,设计器,单击您的 gridview,您应该看到“启用选择”属性)。
关于c# - 简单的从 gridview 在 asp.net web 应用程序中选择一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11164878/