我有一个gridview控件,在我的原始构建中我设置了grid属性:
AutoGenerateSelectButton="True"
当我选择了gridview中的一行时,这很好并且让我可以进行回发。然而,我并不高兴,因为它真的不像一个很好的列表,我希望用户能够点击行中的任何地方来选择它而不必选择“选择”按钮。所以我查看了底层代码,找到了由select按钮调用的函数并将其添加到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 + "')");
}
}
太棒了,所以我去了“选择”按钮,现在我收到了错误
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.
好的,所以我在网上研究并发现我需要注册该事件(是的,所调用的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>
但我仍然遇到同样的错误。如何正确注册事件以便我可以删除选择按钮?感谢。
答案 0 :(得分:4)
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);
}
答案 1 :(得分:0)
您必须注册控件和eventArgs:
If r.RowType = DataControlRowType.DataRow Then
Page.ClientScript.RegisterForEventValidation(Me.GridView1.UniqueID, "Select$" & r.RowIndex)
End If
答案 2 :(得分:0)
r.UniqueID会给你类似yourGridViewID $ ctl0n ...所以试试这个
protected override void Render(HtmlTextWriter writer)
{
foreach(GridViewRow r in Gv.Rows)
{
if(r.RowType==DataControlRowType.DataRow)
{
Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl00");
}
base.Render(writer);
}