ASP.NET Event Validation and “Invalid Callback Or Postback Argument”
I was getting this and pulling my hair out.
My button (server-side) was pumping out a bit of javascript
Page.ClientScript.RegisterStartupScript(GetType(String),
"loginunsuccessful", "<script type=""text/javascript"">
alert('Login unsuccessful – please try again');</script>")
And this kept causing the error.
I tried using the prescribed fix: ClientScript.RegisterForEventValidation(mycontrol.uniqueid) in the prerender. Still got the error.
Here’s how I fixed it. RegisterStartupScript does the job of running at startup by emiting the js right at the foot of the page. I wrapped my JS in jQuery’s own onload code.
Page.ClientScript. RegisterStartupScript(GetType(String),
"loginunsuccessful", "<script type=""text/javascript"">
$(document).ready(function(){alert('Login unsuccessful – please try again');});
</script>")
Still the same error, I then changed to using Page.ClientScript.RegisterClientScriptBlock, which emits the JS in the midst of the html and hey presto no errors. This is without using the RegisterForEventValidation I’d like to add.
Final code:
Page.ClientScript.RegisterClientScriptBlock(GetType(String),
"loginunsuccessful", "<script type=""text/javascript"">
$(document).ready(function(){BlockAlert('Login unsuccessful – please try again');});
</script>")
Simple to fix when you know how.
This entry was posted by nerdboy on Thursday, February 5th, 2009 at 10:15 pm and is filed under Javascript, asp.net. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response below, or trackback from your own site.
No Reader Comments (Be The First?)
Leave a Reply