64

What's the difference between the Enabled and the ReadOnly-properties of an asp:TextBox control?

Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
Alf
  • 1,518
  • 3
  • 17
  • 21

7 Answers7

89

If a control is disabled it cannot be edited and its content is excluded when the form is submitted.

If a control is readonly it cannot be edited, but its content (if any) is still included with the submission.

Adam Bellaire
  • 108,003
  • 19
  • 148
  • 163
  • 20
    This isn't necessarily true ... depending on the version of .NET, if the readonly value is changed, it will revert to the original value on the postback. You need to do something like TextBox1.Attributes.Add("readonly", "true"); to avoid this. – mattruma Oct 06 '08 at 13:44
  • 8
    That should be TextBox1.Attributes.Add("readonly", "readonly"), but yes if you want the viewstate to work then you can't use either of ReadOnly or Enabled. – Alf Oct 06 '08 at 13:50
  • If a control is readonly it cannot be edited, but its content (if any) *may* be included with the submission. See: http://www.w3.org/TR/html4/interact/forms.html#h-17.12 "Read-only elements may be successful." This means that a browser might decide not to post back the value of a readonly input box and that would be perfectly correct. – Anthony Apr 02 '10 at 12:44
  • @Anthony: That's not how I read that specification. Whether or not a control is successful depends on many factors, one of which is whether the control is disabled. (See the section immediately below what you cited). For them to say that "Read-only elements *must* be successful" would be wrong, because for example it could be a read-only select box with no selection made. – Adam Bellaire Apr 02 '10 at 14:12
  • True. But there is one thing I would like to add. If the textbox control is inside an "Update panel", the value for the field which is "Disabled" will be available on form submission. – Arun Banik Mar 24 '14 at 09:16
11

Another behaviour is that readonly = 'true' controls will fire events like click, buton Enabled = False controls will not.

rodrigocl
  • 111
  • 1
  • 3
6

Readonly will not "grayout" the textbox and will still submit the value on a postback.

Bob Dizzle
  • 1,173
  • 1
  • 8
  • 18
4

Think about it from the browser's point of view. For readonly the browser will send in a variable/value pair. For disabled, it won't.

Run this, then look at the URL after you hit submit:

<html>
<form action=foo.html method=get>
<input name=dis type=text disabled value="dis">
<input name=read type=text readonly value="read">
<input name=normal type=text value="normal">
<input type=submit>
</form>
</html>
Corey Trager
  • 22,649
  • 18
  • 83
  • 121
3

Readonly will allow the user to copy text from it. Disabled will not.

Jonathan C Dickinson
  • 7,181
  • 4
  • 35
  • 46
3

Readonly textbox in Asp.net

<asp:TextBox ID="t" runat="server" Style="margin-left: 20px; margin-top: 24px;"
Width="335px" Height="41px" ReadOnly="true"></asp:TextBox>
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
kavitha Reddy
  • 3,303
  • 24
  • 14
1

I have a child aspx form that does an address lookup server side. The values from the child aspx page are then passed back to the parent textboxes via javascript client side.

Although you can see the textboxes have been changed neither ReadOnly or Enabled would allow the values to be posted back in the parent form.

Guy
  • 11
  • 1