Pages

Friday 11 January 2013

Difference between disabled and read only attributes

Disabled attribute

  1. Disabled form fields or elements values don’t post to the server for processing.
  2. Disabled form fields or elements don’t get focus.
  3. Disabled form fields or elements are skipped while tab navigation.
  4. Some browsers (Like IE) provide default style (Gray out or emboss text) for disabled form fields or elements.

Read Only Attribute

  1. Read Only form fields or elements values post to the server for processing.
  2. Read Only form fields or elements get focus.
  3. Read Only form fields or elements are included while tab navigation.
  4. Some browsers do not provide default style for Read-Only form fields or elements.

Note

  1. We can’t set readonly attribute to all the form fields or elements. The <SELECT> , <OPTION> , and<BUTTON> elements do not have readonly attributes while they can have disabled attributes.
  2. In asp.net, when you changed the value of ReadOnly Textbox with the help of javascript or jquery then on server side you will get the oldest value not the changed value. To get changed value from ReadOnly TextBox, make the TextBox Read-Only from code-behind by adding readonly attribute to TextBox like as txtReadonly.Attibutes.Add("readonly","readonly");

Set Disabled Attribute

ASP.NET TextBox
 <asp:TextBox ID="txtDisabled" Enabled="false" runat="server"></asp:TextBox> 
HTML Input
<input name="txtDisabled" type="text" id="txtDisabled" disabled="disabled"/> 

Set Read Only Attribute

ASP.NET TextBox
 <asp:TextBox ID="txtReadonly" ReadOnly="true" runat="server"></asp:TextBox> 
HTML Input
<input name="txtReadonly" type="text" id="txtReadonly" readonly="readonly"/> 

CSS For read only and disabled attribute input element

Since some browser don’t provide default style for disabled element. Hence to display disabled elements on all browsers in same look and feel, we can use below css.
 input[disabled]{background-color:#F0F0F0; color:#303030;} 
Similarly we can change the look and feel of readonly element by using below css.
 input[readonly]{background-color:#F0F0F0 ;color:#303030 } 

No comments:

Post a Comment