c# - 简单的从 gridview 在 asp.net web 应用程序中选择一行

标签 c# asp.net gridview web row

我知道这个问题已被问过一百次,但我在实现不同的解决方案时遇到了困难。我需要从 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/

相关文章:

c# - 流利的 NHibernate 错误 : The entity 'ClassMap` 1' doesn' t have an Id mapped

c# - SignalR sqlServer监听

asp.net - Bundle.config 可以包含 ScriptBundles 吗?

java - 如何在 android 的 gridview 中添加搜索功能?

android - 在异步任务中加载 GridView 位图

android - 使用 Arraylist 时 Gridview 上的空白屏幕

c# - xamarin.forms 中开关为 "on"(蓝色到绿色)时如何更改默认颜色在 ios 中默认为绿色,但在 android 中为蓝色

c# - 如果元素个数是变量,如何显示数组元素

c# - FormsAuthenticationModule 中的这段代码应该如何工作?

c# - 在 MVC 5 中填充 DropDownList