Ehsan Mahpour’s Weblog

ASP.NET

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

by mahpour on Oct.18, 2006, under ASP.NET

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 more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...