Reputation: 23
I need to implement some 3rd-party field validation in our site's forms. Currently, the validation of our form fields is done by using ASP.NET validators (required, regex) and I'd like to continue use of these on the fields where we won't be using the new validation. The company providing us with the new validation services gave us all of the code to integrate into our site to get the new validation functioning, and the basic implementation just requires us to call a javascript method that reads the values of specified fields and validates them via a few service calls. Also, the implementation requires that the form initially has the onsubmit="return false;"
attribute set.
The issue that I'm running into is when the validation script is attempting to submit the form, (after a post-validation function removes the form's onsubmit attribute) since event validation is turned on (not an option to turn off), the page throws the exception "Invalid postback or callback argument". Perhaps I've provided an excess of details to give context, but maybe the question boils down to: Can you submit an ASP.NET form using javascript if event validation is enabled without causing this error? And if so, how?
Upvotes: 1
Views: 9769
Reputation: 2923
Yes, you can. Something like this, and please bear with me, this is merely pseudo code.
<header>
<script type="text/javascript">
function JavaScriptSave()
{
// Validations here
if(passed)
{
__doPostBack('<%=MyButton.ClientID %>','OkToSave');
}
}
</script>
</header>
<asp:button id="MyButton" runat="server" Text="Submit Button" OnClientClick="JavaScriptSave()" />
Then on the back side:
protected void Page_Load(object sender, EventArgs e)
{
string passedInValue = Request["__EVENTARGUMENT"];
if(passedInValue = "OkToSave")
{
// perform save function here
}
...
if(IsPostBack)
return;
}
Upvotes: 0
Reputation: 34846
You can invoke a post back in your JavaScript function, like this:
<script type="text/javascript">
function DoValidation(parameter)
{
// Do calls to validation services here
// If all validation passes, then call this
if(valid) {
__doPostBack('btnSave', parameter)
}
}
</script>
<asp:Button id="btnSave" runat="server" OnClientClick="DoValidation()"
Text="Save" />
Note: btnSave
is a made up name, but it should represent the server-side control that would normally be doing the post back.
Now in your page's Page_Load
event, you can determine who initiated the post back and any data sent with it, like this:
protected void Page_Load(object sender, EventArgs e)
{
// Get the control that caused the post back (btnSave)
Request["__EVENTTARGET"];
// Get the parameter value (parameter)
string parameter = Request["__EVENTARGUMENT"];
}
Upvotes: 1