Yesterday I was looking at a post in ASP.NET Forums regarding the read-only property of a textbox and found it interesting to post it in my blog.
A browser is not supposed to postback the values of the disabled formfields, which means they are not successfull form-fields. A non successfull form-field cannot post the data back to the server. A detailed explanation of what is a successfull and non-successfull form fields is given in this link http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.12
In a simple page, if you are having a textbox with its ReadOnly property set to true, and set the value of the textbox using a client script on body load, and try to access the textbox value in code-behind will result in null value.
This is the sample code which results in this:
[code:html]
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function setText() {
document.getElementById("TextBox1").value = 'a different value';
}
</script>
</head>
<body onload="setText()">
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" ></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
[/code]
and in the code behind:
[code:c#]
"Arial" size="2">protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(TextBox1.Text);
}
[/code]
wont write any text to the browser. This is because, form-fields that are disabled are not posted back to the server, and asp.net take the previous page value or value stored in view state and the users changes (changes that are made through the client-script) to the form field will be lost. Check the remarks part of this link for a better understanding http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.readonly.aspx
For the problem, sure there are a couple of workarounds. One simple way is, instead of setting the ReadOnly property to true in the properties window, add Attribute (readonly) to the textbox in the page_load
[code:c#]
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes.Add("readonly","readonly");
}
[/code]
Another way, which i didnt try is to set the attribute submitdisabledcontrols to true on the form tag.
[code:html]
<form id="form1" runat="server" submitdisabledcontrols="true">
[/code]
One thing, which i found interesting after reading a couple to articles was, the same textbox (with readonly property set to true) will post the client changes to the server, if referred by Request.Form in code behind. I give the code for this :
html listing:
[code:html]
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True">TextValue</asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit" />
</form>
<script language="javascript" type="text/javascript">
function setText() {
document.getElementById("TextBox1").value = 'client-side value';
}
</script>
[/code]
and in the code:
[code:c#]
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
Response.Write(Request.Form(this.TextBox1.ClientID);
}
[/code]
E Screw