Difference between ReadOnly and Disabled
What is the difference between ReadOnly="true" and Enabled="false" for a TextBox control?
When a textbox is in ReadOnly mode or is disabled, we cannot get the text of the textbox on page postback. (TextBox1.Text will be empty). However, in the case of ReadOnly mode, the TextBox text is sent as part of Form's postback data. The textbox value can be retrieved using Request.Form["TextBox1"] where TextBox1 is the ClientID of the TextBox. When a textbox is disabled, the TextBox text does not come as part of form postback. So Request.Form["TextBox1"] will be empty.
The above difference is useful in some scenarios. For eg, you have a TextBox with AJAX CalendarExtender. You do not want the user to enter any text in the textbox. So, you can set the TextBox to be ReadOnly. However on postback, you will not get TextBox1.Text set by calendar control. So you can use Request.Form["TextBox1"] to get the selected date. Sample code is shown below:
<asp:TextBox ID="Text1" runat="server" ReadOnly="true" /> <cc:CalendarExtender runat="server" TargetControlID="Text1"> </cc:CalendarExtender>
protected void Page_Load(object sender, EventArgs e) { Response.Write("Form: " + Request.Form["Text1"] + "<br />"); Response.Write("TB: " + Text1.Text + "<br />"); }
Category : ASP.NET WebForms


