tags:

views:

6890

answers:

5

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

+2  A: 

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

Bob Dizzle
+13  A: 

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
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
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
Thanks for the clarification Alf!
mattruma
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
@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
+2  A: 

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
+1  A: 

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

Jonathan C Dickinson
A: 

I've tried with both FireFox and IE and used only Enabled = false. The content of the text box is being transmitted in the post back.

If the text box is disabled, nothing can change, so does it really matter what the content is.

Does anyone know if other browsers have a different behavior?

Daniel