An ASP.NET Blog
I work for Microsoft and help people and businesses make better use of technolgy to realize their full potential. The opinions mentioned herein are solely mine and do not reflect those of my employer.

ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Thursday, May 10, 2007 9:23 AM

Well, this has been under discussion for sometime and I knew this was a known issue although it was a design change in ASP.NET 2.0.

I am talking about the TextBox which has the Read Only property assigned true not retaining the values or client side changes getting ignored across postbacks.  There are certain blogs / articles which talk about it but for the benefit of those who get stuck with this, I am giving herebelow the steps to reproduce and the resolution for the same.

ASP.NET 2.0 had a design change by which a an <asp:TextBox> control if marked with its ReadOnly property as true, would ignore client side changes and would lose the same across postback.  So if you tried modifying the text box value or add a value to the text box using Javascript you wouldnt be able to retireve the value in the code behind or simply the value will be lost across postback.

Try doing this

Declare a TextBox

<asp:TextBox ID="TextBox1" runat="server" Text="Sample Text" ReadOnly="true" />

Add the following Button

<asp:Button ID="Button1" runat="Server" Text="Submit" OnClick="Button1_Click"  />

The purpose of the above button is to invoke a code behind method, the click event where we can do a Response.Write of the TextBox value.

So, in the code behind, add the following method

    protected void Button1_Click(object sender, EventArgs e)
    {
              Response.Write(TextBox1.Text);
    }

then add the following button

<input type="Button" name="Button2" onclick="changevalue()" />

The purpose of the input type button is to execute a client side javascript.

The client side script is as follows:-

 <script language="javascript">
        function changevalue()
        {
        this.form1.TextBox1.value = "Modified Sample Text";
       
        }
   
    </script>

Place the above script before the </head> tag in your HTML Source.

Now, if you click on the input type button you will see that the TextBox value changes to "Modified Sample Text". 

However, when you click on the button "Submit" which is an <asp:Button>, a server control, you will see that the text that it writes to the browser is again "Sample Text".  You will also see that the TextBox has the value reset to "Sample Text".

This behaviour is new in ASP.NET 2.0 and if you are migrating your ASP.NET 1.x applications you may find this a little annoying / worrying since the values aren't retained.

This is independent of whether you set the EnableViewState property for the TextBox to true or false.  In fact the EnableViewState property for a TextBox doesnt make a difference since the values in a TextBox are maintained and retrieved from the Form's Collection and not from the ViewState.

However, there is a work around for the same.  Instead of setting the "Read Only=true" property in the design you can enable the ReadOnly property of the TextBox through the Attributes collection in the code behind.  To do that, remove the ReadOnly property from the TextBox declaration above.  Then, in the code behind file, within the Page_Load add the following line of code:-

TextBox1.Attributes.Add("readonly", "readonly");

Now you will notice that when you run the page, the client side changes you make in the TextBox (via the Javascript) is retained across postback.

This behaviour is by design in ASP.NET 2.0 and it has been designed with the idea that a ReadOnly TextBox shouldnt be modified in the client side by a malicious code.

For more information check http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.readonly.aspx

Cheers !!! 


Feedback

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Kudos! Although I haven't had this problem personally, I was thinking the same solution while reading about the problem. 5/10/2007 10:40 AM | Shadowin

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Really helpful post! :) 5/22/2007 6:04 AM | butchi

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Absolutely brilliant. I've been messing around with this problem all afternoon, migrating a .NET 1.1 app, and was at the point of giving up and letting the use edit the text when I didn't want them to.

Thanks for the article, and it was even the first site that came up when I finally googled the problem. 6/28/2007 7:30 AM | Craig

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Excellent!!!
Thank you!!

I searched for quite a while on this and this was the only post I found that both explained the problem and gave a simple solution.

thank you again
t 7/2/2007 4:50 AM | Troy

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Great !

thank you very much. 7/6/2007 7:53 PM | molecule

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Thanks Buddy. One can never have too much appreciation. 7/18/2007 4:19 PM | Sammy

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Excellent post.
Saved me likely 2 hours of Googling for an answer! 9/18/2007 8:50 AM | Dan Bishop

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Dear,
You have given the exact solution which is very rare on net.
Thank u very much.May ALLAH enhance your knowledge. 12/13/2007 12:23 AM | Tahir

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Thanks a lot for putting me out of my misery. I, too, had to port a .NET 1.1 app to 2.0 and ran up against this problem. The app consisted of 100's of pages, and in order to patch them all quickly I devised the following solution which someone else might find useful:

Create a global class in your App_Code folder, e.g. Net2Util.cs with the following content:

////////////////////////////////////////
using System.Web.UI;
using System.Web.UI.WebControls;

//use your own namespace!
namespace MABS.App_Code
{
public class Net2Util
{
public static void clearReadOnlyTextboxesForNet2(Page pg)
{
foreach (Control c in pg.Form.Controls)
{
if (c.GetType().ToString() == "System.Web.UI.WebControls.TextBox" && ((TextBox)c).ReadOnly)
{
((TextBox)c).ReadOnly = false;
((TextBox)c).Attributes.Add("readonly", "readonly");
}
}
}
}
}
////////////////////////////////////////

Then you can call this from any page's Page_Load as follows:

///////////////////////////////////////
protected void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
MABS.App_Code.Net2Util.clearReadOnlyTextboxesForNet2(this);
}
}
///////////////////////////////////////

Hope this helps!
Tian 12/13/2007 6:35 AM | Tian

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

thanks for this brilliant idea.I got my solutions.This is very easy way to handle the problem.thanks again. 12/17/2007 9:14 PM | Gp

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

DUDE!!!! YOU SAVED MY CLIENT, MY COMPANY AND MY JOB!!!!!! LOTSA LOVE N BEST WISHES FOR SHARING THIS WONDERFUL BIT OF KNOWLEDGE! 12/18/2007 10:53 PM | DEBADEEP

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Thanks a lot man ..... i cant tell, how usefull this is. i cant find any article on this issue anywhere ...and it resolved most of my miseries thanks a lot yaar
12/19/2007 5:23 PM | Saurav Nimesh

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

If read only text box does't have viewstate how is it attaining the originally set value upon post back? 12/23/2007 7:23 PM | N.S.A.Sharma Pendyala

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

That was very useful. That works for text boxes. But what if you have a HiddenField whose value is changed by client script? I've noticed that those lose value just like a readonly textbox. 1/3/2008 8:32 AM | B

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Very good article and detailed description 1/29/2008 8:52 PM | Hemant

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Damn, I was doing SQL Profile Queries, million break points, etc and was going nuts. THANKS SO MUCH!! I just wish I googled earlier! 2/6/2008 3:15 PM | Spartan

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Thanks for a helpful trip! 2/10/2008 5:15 PM | Pushp

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Excellent... 2/20/2008 8:38 PM | Avijit

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

thanks for this very gooooood solution.... 2/28/2008 5:39 AM | jar

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

thank you for the tip..
very useful 3/7/2008 8:18 PM | Magesh

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

it was good. used the subject above in my site
and it really looks very meaningful
Thanks 3/9/2008 8:01 PM | praveen

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Thank you soooo much! I am working on upgrading an application from VS2003 to VS2008 and have been struggling with this for 2 days. I finally "googled" on the right text and found this article. It was exactly what I needed. 3/12/2008 3:35 AM | Kyle Woodhouse

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Grazie 5/5/2008 4:29 AM | Alex

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

css textboxt ınput (textfield) style - examples - -
http://www.css-lessons.ucoz.com/textbox-css-examples.htm 5/23/2008 3:56 PM | chester..

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Awesome it worked for me. great posting 5/30/2008 2:05 AM | Gore

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

HI i need your help i really want to create my own website/web page but i dont know how to go about doing it so can you please help me out
6/6/2008 10:34 PM | xxcemil

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

What about using the image control? do you have a workaround for this? 6/24/2008 2:14 AM | jacksoft

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Thank you very much! :) 6/24/2008 6:49 AM | calebhc

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

just one question! imagine that i have an important field/ textbox that is read-only.

Now if i go, in run-time, for example, with ie developer toolbar and select that specific textbox and delete the read only property making that textbox editable, and after this, insert new data i can read the new and inconsistent data in server !?

Am i missing something ?

Thanks

7/6/2008 10:47 PM | Paulo Pereira

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

why are you copying the work around provided by http://aspadvice.com/blogs/joteke/archive/2006/04/12/16409.aspx ( dates back to 2006 )

Surely you have done your own homework on google and his blog post is the first post on this topic. Really hard to miss :

http://www.google.com/search?hl=en&q=asp.net+readonly+textbox+postback&btnG=Google+Search

Not very nice is it ? At a minimum, you should provide a link back to his post (crediting him).

8/7/2008 12:08 AM | Alessandro

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

nice work 8/18/2008 7:13 PM | kisha

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Good observation and nice workaround. Thanks.
Alternative to above solution.
Change control type to input type=text and set readonly=readonly at the design time.
This need not be a better solution, just another workaround. 8/19/2008 10:42 PM | Saurabh

# re: ASP.NET 2.0 TextBox Ready Only losing client side changes, values across postback?

Thank you!! I was looking for a solution and this is perfect!! 8/26/2008 7:59 AM | Roman

Post a comment





 

Please add 5 and 1 and type the answer here: