How can i override, or extend, the standard WebForms WebForm_OnSubmit
JavaScript function?
I am using HTML5 input types in an ASP.net WebForms web-site. The user-agent (e.g. Chrome, IE, Firefox) already correctly handles data sanitization, alternate UI, etc.
Normally, when a user clicks an <input type="submit">
button, the User-Agent will halt the submit, and show UI to indicate to the user that their input is going to be invalid:
The problem with WebForms is that it does not use a Submit button. Instead, everything can be done through a JavaScript postback, rather than submitting the form:
<asp:LinkButton ID="bbOK" Text="Save User" runat="server" OnClick="bbOK_Click"></asp:LinkButton>
Which, when rendered into client HTML, becomes a call to the JavaScript:
WebForm_DoPostBackWithOptions(...)
With the really long form being an Anchor tag:
<a id="Really_Long_Thing_ctl00_bbOK"
href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContent$VistaToolbar$ctl00$bbOK", "", true, "", "", false, true))'>
Save User
</a>
This means that the user-agent never gives the user a prompt that an entered element is invalid.
Even though the form
is not being "submitted", it does still trigger an onsubmit
event. I can manually trigger the HTML5 form validation using something like:
isValid = form.checkValidity();
Ideally i would hook that event in my ASP.NET WebForms site:
Site.master
<form runat="server" onsubmit="return CheckFormValidation(this)">
<script type="text/javascript">
function CheckFormValidation(sender)
{
//Is the html5 form validation method supported?
if (sender.checkValidity)
return sender.checkValidity();
//If the form doesn't support validation, then we just assume true
return true;
}
</script>
Except that the WebForms infrastructure deletes my onsubmit
method handler, and replaces it with its own:
<form id="ctl01" onsubmit="javascript:return WebForm_OnSubmit();" action="UserProperties.aspx?userGuid=00112233-5544-4666-7777-8899aabbccdd" method="post">
So my own submit call is ignored. It appears that delving into WebForm_OnSubmit
leads down a rabbit hole of ASP.NET WebForms validation infrastructure:
function WebForm_OnSubmit()
{
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false)
return false;
return true;
}
which calls the function:
var Page_ValidationActive = false;
function ValidatorOnSubmit()
{
if (Page_ValidationActive) {
return ValidatorCommonOnSubmit();
}
else
{
return true;
}
}
which calls, which calls, which uses, which checks.
Just want HTML5 form validation with WebForms
The issue is that WebForms has a huge infrastructure for checking a form before submitting, and displaying errors to users through a standardized mechanism (and entire infrastructure that i do not use). It takes over the onsubmit
event of forms, and it doesn't (necessarily) use an <input type="submit">
in the process.
What can i do to convince ASP.NET WebForms web-site to let the User-Agent validate the form?
submit
event would have not been fired anyway - it only fires when the user submits a form (soformElement.submit()
does not trigger it, as far as I remember) and a form submission (not initiated by JavaScript) requires the form to have atype=submit
(input
orbutton
) anywhere. – Multiplicity