Hi there!

Hi there!
My name is Michael Ivanov and i'm a Senior SDE/Software architect.
My CV - here. You can find articles and other blog posts on the main page. I also develop some free and commercial software - complete list is here.

Thursday, June 3, 2010

your javascript and validators' javascript

I was creating a small web-page the day before. There was a button like this:
I wanted confirmation box appeared before button would postback the page to the server so in Page_load i added such row:
btnComplete.Attributes.Add("OnClick", "return confirm('" + ConfirmTextWarning + "');");
After that, everything was working fine. A bit later i added RequiredFieldValidator to validate a textbox on the page. The button's "Causes validation" button was set to true, but there was no validation on click of the button. The confirmation box was shown but if i click yes - page was send to the server without any validation. When i checked the page source in browser, i found there this element:
<input type="submit" name="btnComplete" value="Complete" onclick="return confirm('some text');WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("btnComplete", "", true, "", "", false, false))" id="btnComplete" />
As you can see, call of the validator's javascript function is added to the onclick event of the button, but it was not call due the return keyword in confirmation box call. I changed row in the Page_load method to
btnComplete.Attributes.Add("OnClick", "confirm('" + ConfirmTextWarning + "');");
and output became
<input type="submit" name="btnComplete" value="Complete" onclick="confirm('some text');WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("btnComplete", "", true, "", "", false, false))" id="btnComplete" />
And it worked fine:) Thank you if you read it, it was my last post before vacations.

No comments: