Sunday, December 12, 2010

Behaviour of disabled controls in Asp.net

How to disable controls:
When we are developing web applications we need to disable/enable controls on the form. Controls which supports disabled are BUTTON, INPUT, OPTGROUP, OPTION, SELECT, and TEXTAREA.
Please do not try to disable a control which doesn’t support this attribute it may have weird behaviour.
We can set attribute disabled="disabled" OR $('#ctrlID').attr('disabled','disabled') OR
document.getElementById(' ctrlID'').setAttribute("disabled",'disabled');
We can enable controls by removing disabled from the elements.
What will change in behaviour of controls when you disable them?
If you are working with dynamic web application and you are disabling/enabling controls then you should remember some key effect of disabling control.
1) Disabled controls will not be posted when you submit form.
Because of this if you have changed value in disabled control dynamically by JavaScript. Then new value will not be posted back on the server and your server control will behave like normal html control.
Is there any way to submit disabled control?
In Asp.Net server side form control have one property called submitdisabledcontrols.
Setting submitdisabledcontrols="true" will submit disabled control values on the server.
 
Never disable hidden field controls.
Even though we set disabled hidden fields will not be submitted. So please keep in mind that you are not disabling hidden fields by knowing or unknowingly. Otherwise your may behave weird.