RegisterForEventValidation .net 3.5 gridview 行,如何?

fla*_*404 1 c# gridview .net-3.5 visual-studio-2008

我有一个 gridview 控件,在我的原始版本中我设置了 grid 属性:

AutoGenerateSelectButton="True"
Run Code Online (Sandbox Code Playgroud)

这很好,使我能够在我的 gridview 中选择一行时进行回发。但是,我并不高兴,因为它确实不像一个不错的列表,我希望用户能够单击行中的任意位置来选择它,而不必选择“该”选择按钮。所以我查看了底层代码,找到了被选择按钮调用的函数,并将其添加到 RowDataBound 事件中:

protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onclick", "javascript:__doPostBack('grid','Select$" + e.Row.RowIndex + "')");
        }
    }
Run Code Online (Sandbox Code Playgroud)

太棒了,所以我去删除“选择”按钮,现在我收到错误

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> 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.
Run Code Online (Sandbox Code Playgroud)

好的,所以我在网上研究,发现我需要注册事件(是的,被调用的javascript仍然存在)所以我添加了以下代码:

<script runat="server">
    protected override void Render(HtmlTextWriter writer)
    {
        foreach (GridViewRow r in grid.Rows)
        {
            if (r.RowType == DataControlRowType.DataRow)
            {
                Page.ClientScript.RegisterForEventValidation(r.UniqueID);
            }
        }

        base.Render(writer);
    }
</script>
Run Code Online (Sandbox Code Playgroud)

但我仍然遇到同样的错误。如何正确注册事件以便我可以删除选择按钮?谢谢。

ynn*_*nok 5

C# 中的解决方案:

protected override void Render(HtmlTextWriter writer) {

    foreach (GridViewRow r in gridviewPools.Rows) {
        if (r.RowType == DataControlRowType.DataRow) {
            Page.ClientScript.RegisterForEventValidation(gridviewPools.UniqueID, "Select$" + r.RowIndex);
        }
    }

    base.Render(writer);
}
Run Code Online (Sandbox Code Playgroud)