ASP.NET Fails on validating controls when they are disabled from server side by default.

Here is the scenario, I have simple form with some validators bound to them, a check box and a Submit button also a simple Javascript code that checks the checkbox value and then enables the submit button when user clicks on the checkbox. (Traditional “I agree” field or something like that) .

here is the Javascript code:

function agreeClicked()
{
var chk = document.getElementById(”chkAgree”);
var btn = document.getElementById(”btnSubmit”);
if(chk.checked)
{
btn.disabled = false;
}
else
{
btn.disabled = true;
}
}

and here is the code for checkbox:

<input type=”checkbox” onclick=”javascript:agreeClicked()” id=”chkAgree” name=”chkAgree” />

here is the asp.net button code:

<asp:Button ID=”btnSubmit” runat=”server” Text=”Submit” OnClick=”btnSubmit_Click” Enabled=”false” />

The Problem:

when you change the disable an asp.net button from server side, asp.net ignores to generate javascript validation code for the button and when you enable it manually from client side … you will not see any validation check.

The Solution:

do not disable your button from server side then try to disable it from client side when page is loading.

here is a sample code:

<script language=”javascript” type=”text/javascript” >
var btn = document.getElementById(”btnSubmit”);
btn.disabled = true;
</script>

Leave a comment

Please be polite and on topic. Your e-mail will never be published.

You must be logged in to post a comment.