我正在实现一个特性,当用户按下GridView中行中的任何一点时,该行将被选中,而不是Select按钮.

在此处输入图像描述

为了实现这一点,我使用以下代码:

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

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

  • 只有当页面的IEnableEventValidation设置为false时,才能正常工作.
  • 只有在Page_Load中调用Grid.DataBind()(在每次回发中)时,才会触发SelectedIndexChanged.

我做错什么了吗?是否有更好的实施方案?


Edit:

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.

推荐答案

您必须在每次回发中添加此项,而不仅仅是在数据绑定中.因此,应该使用GridView的RowCreated事件.

例如

(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

Asp.net相关问答推荐

Stimulsoft 没有在智能手机中显示正确的 pdf 字体

$(document).ready 不工作

在 appSettings 中存储字符串数组?

Automapper - 映射器已初始化错误

ASP.NET - AppDomain.CurrentDomain.GetAssemblies() - AppDomain 重新启动后缺少程序集

是否有用于 Asp.net 标记的#IF DEBUG?

使用 Visual Studio 2012 恢复删除的文件

在 Asp.net mvc5 中使用用户名而不是邮箱作为身份

如何在 ASP.NET 下拉列表中添加选项组?

如何设置 CSS 切换器

防止在 Visual Studio 2008 ASP.NET Web 窗体中复制粘贴时自动生成 ID

带有文件名的 ASP.net MVC4 WebApi 路由

将通用列表绑定到转发器 - ASP.NET

.NET 上的 HTTP/2(HTTP2 或 SPDY)

aspnet_compiler 发现错误版本的 System.Web.WebPages 1.0.0.0 而不是 2.0.0.0

<%# Eval("State") %> 或 <%# DataBinder.Eval(Container.DataItem, "state")%>

在资源文件中使用 HTML

如何在 .NET Core 中实现 DbContext 连接字符串?

主机与 DnsSafeHost

ASP.Net 哪个用户帐户在 IIS 7 上运行 Web 服务?