Please enable Javascript to correctly display the contents on Dot Net Tricks!

Difference between disabled and read only attributes

  Author : Shailendra Chauhan
Posted On : 21 Jul 2012
Total Views : 46,215   
Updated On : 20 Jul 2012
 

Sometimes, we required to display form fields in non-editable mode. We can achieve this functionality by setting read-only or disabled attribute to form fields (textbox, label, checkbox, text area). Before using both these attributes, we should understand the key differences between read only and disabled 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 } 
Summary

In this article I try to expose the disabled and readonly attributes. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.



Free Interview Books
 
COMMENTS (0)
6 JUN
Hadoop Development cum Administration (Online)

7:00 AM-9:00AM IST

Know More
30 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

Know More
30 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

28 MAY
ASP.NET Core with Angular4 Development (Classroom)

10:30 AM-11:30 AM Weekend

27 MAY
ASP.NET MVC with Angular4 Development (Classroom)

4:00 PM-5:30 PM Weekend

20 MAY
Angular2 and Angular4 Development (Online)

08:00 AM-10:00 AM IST(+5.30 GMT) Weekend

20 MAY
NodeJS with Angular4 Development (Classroom)

5:00 PM-6:30 PM Weekend

8 MAY
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5:30 GMT) M/W/F

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

22 APR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM Weekend

10 APR
Master Class ASP.NET MVC 5 with Angular2 Plus Angular4 Development (Online)

09:00 PM - 11:00 PM IST (M/W/F)

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+