In this article I will explain how to get readonly textbox value in codebehind file. I am writing this article to resolve these issues:
• ASP.NET Ready Only TextBox lose client side changes, values across post back?• Why Readonly Text box values are empty in code behind ?• Issue in Retrieving textbox value when readonly = true in codebehind.?
Use the ReadOnly property to specify whether the contents of the TextBox control can be changed. Setting this property to true will prevent users from entering a value or changing the existing value. Note that the user of the TextBox control cannot change this property; only the developer can.The Text value of a TextBox control with the ReadOnly property set to true is sent to the server when a postback occurs, but the server does no processing for a read-only text box. This prevents a malicious user from changing a Text value that is read-only. The value of the Text property is preserved in the view state between postbacks unless modified by server-side code. This property cannot be set by themes or style sheet themes.
Microsoft's reply to a bug report related to topic.
If you have a control on a page that is marked Read-Only and EnableViewState is set to false on the Page, the ReadOnly value will no longer post back in ASP.NET 2.0 - the value gets lost. This even though the value is actually returned in the POST buffer. This behavior is also different than 1.1 which (correctly I say) posted back the READONLY value.
Readonly textbox value is not sent back during postback , the page can remember these values if they are specified directly in the markup portion of the ASP.NET page in the textbox’s control's declarative syntax or if they are set programmatically and ViewState is enabled for the control and page. If ViewState is disabled and the control value is specified programmatically, it will be lost on postback because the actual value in the disabled TextBox won't be sent back to the server during postback.Workaround this issue Instead of making the textbox readonly using properties option. Add the code:
In codebehind which will still make the textbox as readonly and will also retain the value after each postback.Consider the following test code to clear that.
var textBox1 = document.getElementById('<%= TextBox1.ClientID %>');
textBox1.value = 'Test Text1';
var textBox2 = document.getElementById('<%= TextBox2.ClientID %>');
textBox2.value = 'Test Text2';
<form id="form1" runat="server">
Textbox set as readonly in .aspx
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" ></asp:TextBox><input
id="btnAddText1" type="button" value="Add text in this textbox" onclick="addText1();" />
Textbox set as readonly in .aspx.cs ( codebehind)
<asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox><input
id="btnAddText2" type="button" value="Add text in this textbox" onclick="addText2();" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" /> <br />
After postback value in Textbox set as readonly in .aspx :<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
After postback value in Textbox set as readonly in .aspx.cs ( codebehind) :<asp:Label ID="Label2" runat="server" Text=""></asp:Label><br />
public partial class how_to_get_readonly_textbox_value_in_codebehind : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void btnSubmit_Click(object sender, EventArgs e)
Label1.Text = TextBox1.Text;
Label2.Text = TextBox2.Text;
Another good explanation I found by Andrew Hare atWhy does a read-only textbox does not return any data in ASP.NET?
There is a little bit of strangeness when it comes to the ASP.NET Readonly property and the readonly attribute of an HTML input element. Rather than setting the Readonly property of the web control try simply adding the HTML attribute to the control like this:textBox.Attributes.Add("readonly", "readonly");This will make the control read-only in the client's browser yet still allow you to retrieve the value of the input when it posts back to the server.
Do let me know your feedback,comments.