I have a usercontrol in ASP.Net featuring a button that I use to postback. This button also has some Javascript validation that is done before it processes the event.
Today I tried to postback to another URL by setting the PostBackURL property of the button. But it didn't work and the page kept posting back to itself. So I did some investigation and found that.
- Post back to another URL is implemented through Javascript in ASP.Net.
If I keep the call to my validation function
OnClientClick="return validate()" Then the post back does not occur.
- If I remove the call to the validation function, the postback works fine.
This is how the button markup looks with Validation on.
<input type="submit" name="ctl00$cphMain$pra1$btnSubmit" value="Submit" onclick="return validate();WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$cphMain$pra1$btnSubmit", "", false, "", "Result.aspx", false, false))" id="ctl00_cphMain_pra1_btnSubmit" style="width:80px;" />
Without validation
<input type="submit" name="ctl00$cphMain$pra1$btnSubmit" value="Submit" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$cphMain$pra1$btnSubmit", "", false, "", "Result.aspx", false, false))" id="ctl00_cphMain_pra1_btnSubmit" style="width:80px;" />
You'll notice that the call to 'return validate()' is missing, and that is making all the difference.
How can I get this to work?
I have a usercontrol in ASP.Net featuring a button that I use to postback. This button also has some Javascript validation that is done before it processes the event.
Today I tried to postback to another URL by setting the PostBackURL property of the button. But it didn't work and the page kept posting back to itself. So I did some investigation and found that.
- Post back to another URL is implemented through Javascript in ASP.Net.
If I keep the call to my validation function
OnClientClick="return validate()" Then the post back does not occur.
- If I remove the call to the validation function, the postback works fine.
This is how the button markup looks with Validation on.
<input type="submit" name="ctl00$cphMain$pra1$btnSubmit" value="Submit" onclick="return validate();WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$cphMain$pra1$btnSubmit", "", false, "", "Result.aspx", false, false))" id="ctl00_cphMain_pra1_btnSubmit" style="width:80px;" />
Without validation
<input type="submit" name="ctl00$cphMain$pra1$btnSubmit" value="Submit" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$cphMain$pra1$btnSubmit", "", false, "", "Result.aspx", false, false))" id="ctl00_cphMain_pra1_btnSubmit" style="width:80px;" />
You'll notice that the call to 'return validate()' is missing, and that is making all the difference.
How can I get this to work?
Share Improve this question asked Jul 20, 2009 at 12:26 Cyril GuptaCyril Gupta 13.7k11 gold badges69 silver badges92 bronze badges2 Answers
Reset to default 13Replace
OnClientClick="return validate();"
with
OnClientClick="if (!validate()) { return false; }"
its because the return
prevents WebForm_DoPostBackWithOptions
from executing, regardless of the result of the validation.
you could try (without quotes):
"!validate() ? return false : "
that way it would end up as:
!validate() ? return false : WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$cphMain$pra1$btnSubmit", "", false, "", "Result.aspx", false, false))
a crude hack, but thats webforms for you, one big hack on top of another.