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

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 !!! 

posted @ Thursday, May 10, 2007 9:23 AM

Print

Comments on this entry:

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

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

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

Left by butchi at 5/22/2007 6:04 AM
Gravatar
Really helpful post! :)

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

Left by Craig at 6/28/2007 7:30 AM
Gravatar
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.

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

Left by Troy at 7/2/2007 4:50 AM
Gravatar
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

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

Left by molecule at 7/6/2007 7:53 PM
Gravatar
Great !

thank you very much.

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

Left by Sammy at 7/18/2007 4:19 PM
Gravatar
Thanks Buddy. One can never have too much appreciation.

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

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

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

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

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

Left by Tian at 12/13/2007 6:35 AM
Gravatar
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

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

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

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

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

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

Left by Saurav Nimesh at 12/19/2007 5:23 PM
Gravatar
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

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

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

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

Left by B at 1/3/2008 8:32 AM
Gravatar
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.

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

Left by Hemant at 1/29/2008 8:52 PM
Gravatar
Very good article and detailed description

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

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

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

Left by Pushp at 2/10/2008 5:15 PM
Gravatar
Thanks for a helpful trip!

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

Left by jar at 2/28/2008 5:39 AM
Gravatar
thanks for this very gooooood solution....

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

Left by Magesh at 3/7/2008 8:18 PM
Gravatar
thank you for the tip..
very useful

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

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

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

Left by Kyle Woodhouse at 3/12/2008 3:35 AM
Gravatar
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.

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

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

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

Left by Gore at 5/30/2008 2:05 AM
Gravatar
Awesome it worked for me. great posting

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

Left by xxcemil at 6/6/2008 10:34 PM
Gravatar
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

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

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

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

Left by calebhc at 6/24/2008 6:49 AM
Gravatar
Thank you very much! :)

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

Left by Paulo Pereira at 7/6/2008 10:47 PM
Gravatar
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

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

Left by Alessandro at 8/7/2008 12:08 AM
Gravatar
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).

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

Left by kisha at 8/18/2008 7:13 PM
Gravatar
nice work

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

Left by Saurabh at 8/19/2008 10:42 PM
Gravatar
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.

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

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

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

Left by raz0rf1sh at 9/4/2008 6:53 AM
Gravatar
Good stuff! Helped me out today! Thank you!!!

# How i can give onclick event to asp.net text box

Left by jitu at 9/8/2008 2:44 AM
Gravatar
plz, help me

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

Left by UstesG at 9/12/2008 6:53 AM
Gravatar
add this property to your form declaration.

submitdisabledcontrols="true"

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

Left by Jeroen at 10/23/2008 4:40 PM
Gravatar
Thank you! Workaround should be integrated in the .Net framework (extra property).

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

Left by Guest at 11/19/2008 12:57 AM
Gravatar
Fantastic absolutely great !!!

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

Left by Edgar Iván at 12/5/2008 2:10 AM
Gravatar
Great! You helped me! Thank you!

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

Left by arjun balip at 1/4/2009 12:35 PM
Gravatar
it's helpfull for me

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

Left by Virendra at 1/23/2009 3:35 PM
Gravatar
Hi,
This is not true. If enableviewstate is set to false then text box does not maintain it's value.

Why it's maintaining value in your case as you have defined in design time.

Just declare the text box with readonly property

<asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" EnableViewState="false" />

Set this text box value in the page load (In isPostback section) and now click on the button which does the post back. Your textbox value is lost...


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

Left by Pavan at 1/28/2009 5:48 AM
Gravatar
Hi,

Thank you...u saved my life...
i have been working on this issue for so many hours..
ur article did the trick...

Thanks a lot..

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

Left by Biru at 2/19/2009 3:28 PM
Gravatar
Hi,
thanks for this brilliant idea.I got my solutions.This is very very easy way to handle the problem.with out java script thanks again.

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

Left by Mahesh at 3/3/2009 4:44 PM
Gravatar
Really very helpfull link yaar...

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

Left by club penguin at 3/17/2009 6:09 PM
Gravatar
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

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

Left by Nirav Doshi at 3/29/2009 2:05 PM
Gravatar
This, this was helpful! I had a problem with certain dynamically calculated values not being stored to the DB - on postback. Your solution worked great!

Thanks! :-)

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

Left by R.Dhandapani at 5/29/2009 1:05 PM
Gravatar
Thanks, this was very helpful for me.

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

Left by Willem at 6/15/2009 9:44 AM
Gravatar
Does anyone else get the idea that Microsoft touts any bugs in Visual Studio as "by design" and "to avoid malicious attacks"?

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

Left by Karan at 7/21/2009 8:52 AM
Gravatar
Its a great help. I was having this in my application and causing me headaches.

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

Left by Some Guy at 7/30/2009 3:00 PM
Gravatar
Thanks for this. I'm the CIO of a Fortune 5 corporation and we almost lost our biggest client over this very issue. If one of our developers hadn't found this page, our company would be pretty much gone by the end of this quarter.

I literally had a gun barrel in my mouth just a moment ago.

THANK YOU THANK YOU THANK YOU!

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

Left by Rajan at 8/5/2009 1:41 PM
Gravatar
Excellent...Saves lot of time...

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

Left by Balakrishna at 8/17/2009 2:11 AM
Gravatar
this post was very help full for me.
thank you very much

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

Left by web development company at 8/27/2009 9:30 AM
Gravatar
Interesting,

but I like to see an event added to textbox events something like -> ontextchanging , that fires when text is going to change not when the focouse of the control is gone llike on changed event

Thanks for writing about it

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

Left by Suresh Padaga at 9/25/2009 2:56 AM
Gravatar
Thanks alot yar i wasted almost 3 hours to resolve this prob..............

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

Left by corvette car seats at 10/31/2009 6:12 AM
Gravatar
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.

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345