【问题标题】:Simple Selecting a row from gridview in asp.net web application在 asp.net Web 应用程序中简单地从 gridview 中选择一行
【发布时间】:2014-02-14 23:38:52
【问题描述】:

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


        }

【问题讨论】:

  • 请不要在标题中使用标签。

标签: c# asp.net gridview web row


【解决方案1】:

如果我做对了,这应该可以满足您的要求:

.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 页面,设计器,单击您的网格视图,您应该会看到“启用选择”属性)。

【讨论】:

  • 我没有找到启用选择属性,您可以告诉我启用它的 .aspx 代码...我使用的是 Visual Studio 2010
  • 坏消息是我从一个数组列表手动绑定我的数据......在我的代码中......我认为这就是为什么我没有看到 3 个启用......所以我看到了在我的应用程序启动之前未绑定...
  • 啊....我提到我不想使用按钮...例如启用选择激活时出现的链接按钮
  • 首先我遇到了一些与无效回发或回调参数有关的错误。使用配置或页面中的 启用事件验证。出于安全目的,此功能验证回发或回调事件的参数是否源自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用 ClientScriptManager.RegisterForEventValidation 方法注册回发或回调数据以进行验证。
  • 还有,另一个问题是,我不知道如何使用它来检索选定的行...你能帮我吗?:))给我一个例子?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2015-11-11
    • 2017-04-24
    • 2014-01-29
    • 2010-11-10
    • 1970-01-01
    • 2010-09-17
    相关资源
    最近更新 更多