c# - 如何在没有选择按钮的情况下在 GridView 中实现全行选择?

标签 c# asp.net gridview selectedindexchanged

我正在实现一项功能,当用户按下 GridView 中行中的任意点时,将选择该行而不是选择按钮。

enter image description here

为了实现它,我使用了以下代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Set the hand mouse cursor for the selected row.
        e.Row.Attributes.Add("OnMouseOver", "this.style.cursor = 'hand';");

        // The seelctButton exists for ensuring the selection functionality
        // and bind it with the appropriate event hanlder.
        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, "");
    }
}

使用上面的代码,存在以下问题:

  • 仅当我将页面的 EnableEventValidation 设置为 false 时才能正常工作。
  • SelectedIndexChanged 仅在 Grid.DataBind() 在页面的 Page_Load 中调用(在每次回发中)时才会触发.

我做错了什么吗?有没有更好的实现方式?


编辑:EnableEventValidation设置为true时,会出现如下错误:

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

最佳答案

您必须在每次回发时添加它,而不仅仅是在数据绑定(bind)上。因此你应该使用 RowCreated -GridView 的事件。

例如

(C#):

protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) {
        e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
        e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
        e.Row.ToolTip = "Click to select row";
        e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
    }
}

(VB.Net):

Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Attributes("onmouseover") = "this.style.cursor='pointer';this.style.textDecoration='underline';"
        e.Row.Attributes("onmouseout") = "this.style.textDecoration='none';"
        e.Row.ToolTip = "Click to select row"
        e.Row.Attributes("onclick") = Me.Page.ClientScript.GetPostBackClientHyperlink(Me.GridView1, "Select$" & e.Row.RowIndex)
    End If
End Sub

关于c# - 如何在没有选择按钮的情况下在 GridView 中实现全行选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6250545/

相关文章:

c# - .Net Core/控制台应用程序/配置/XML

c# - 在 Entity Framework Core 中添加数据库后子模型列表为空

c# - GridView 文本框值插入另一个文本框中

ASP.NET Gridview - 存在什么属性来确定网格是否呈现?

asp.net - 如何将 ItemTemplate 文本值设置为组合框 DataTextField 而不是 DataValueField

c# - 如何在 XBAP 应用程序中使用 BinaryFormatter 反序列化流?

c# - 使用 Asp.Net Core Cookie 身份验证的注销操作

C# Asp.net Membership.GetAllUsers 通过电子邮件订购

c# - 预期的方法名称?

asp.net - 如何防止用户重复登录