I have a gridview control and in my original build I set grid attribute:
AutoGenerateSelectButton="True"
Which was nice and enabled me to do postbacks when a row in my gridview was selected. However, I wasn't happy as it really didn't act like a nice list and I wanted the user to be able to click anywhere in the row to get it selected rather than have to select 'the' select button. So I looked at the underlying code, found the function which was being called by the select button and added it to the RowDataBound event:
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 + "')");
}
}
Awesome, so then I went to remove the 'select' button and now I am getting the error
Invali开发者_如何学God 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.
Ok, so I researched on the web and found that I needed to register the event (yes the javascript being called is still there) so I added this code:
<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>
But I am still getting the same error. How do I register the event correctly so that I can remove the select button? Thanks.
Solution in 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);
}
You must register the control and the eventArgs:
If r.RowType = DataControlRowType.DataRow Then
Page.ClientScript.RegisterForEventValidation(Me.GridView1.UniqueID, "Select$" & r.RowIndex)
End If
r.UniqueID will give you something like yourGridViewID$ctl0n... So Try this
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);
}
精彩评论