Contact   •   Products   •   Search

Rick Strahl's Web Log

Wind, waves, code and everything in between...
ASP.NET • C# • HTML5 • JavaScript • AngularJs

ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false


Here’s a behavior difference between ASP.NET 1.1 and 2.0: 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.

I do this a bunch in some of my applications where there are generated IDs that display on a form, but they are readonly. The value displays and is supposed to be part of the postback setup for the page. That doesn’t work any longer without some manual intervention. Since I have EnableViewState off an just about all of my pages this is biting me on everyone of them where I have ReadOnly controls.

 

Here’s a sample of the issue. Put two textboxes and a button on a form and mark the first textbox as ReadOnly:

 

<%@ Page language="c#" enableviewstate="false" Trace="true" %>

<script runat="server">

 

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!this.IsPostBack)

        {

            this.Textbox1.Text = "readonly text";

        }

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        this.lblMessage.Text = this.Textbox1.Text;

    }

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

    <title>Untitled Page</title>

</head>

<body>

<form runat="server" id="Form1">

<asp:textbox ID="Textbox1" runat="server" ReadOnly="true" ForeColor="silver"></asp:textbox>

<asp:textbox ID="Textbox2" runat="server" ReadOnly="true">Some Text</asp:textbox>

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />

<asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>

</form>

</body>

</html>

 

Now when you run this page in 2.0 you’ll see the readonly text displayed on the initial GET request that sets up the page. When you click on the Button to submit the page, though the value gets lost after the Postback.

 

If you look at the Trace though you can see that the value was actually posted back just fine – ASP.NET just didn’t pick it up and reassign it to the control.

 

If you run this same sample in ASP.NET 1.1 you’ll find that – properly I say – posts the value back to the control.

 

This has broken a bit of code for me in very subtle ways. As I’m going over the West Wind Web Store today I’m finding a handful of odd errors caused by this and the only way to find them is to run into them. <g>

 

The workaround is easy enough though:

 

this.TextBox1.Text = Request[this.TextBox1.UniqueID];

 

in the Page_Load() does the trick.

 

So I wonder if this is by some sort of design that this has changed or if this is a bug?

 

Anyway, it’s reported here:

http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=60bb1325-9dc3-48e9-a32f-e211ea345862

Make Donation


Feedback for this Post

 
# ASP.NET 2.0ではEnableViewStateの仕様が変わった?
by The road to C# master trapemiya December 20, 2005 @ 6:17pm
ASP.NET 2.0??EnableViewState?????????
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Gabe December 21, 2005 @ 4:53am
This bug has bit me in the arse as well. Fortunatly I only had to implement the workaround in one place.

Off topic, but what I hate is if you have option strict set to true in your web.config (vb), any pages that have grids or other controls where you use container.dataitem will fail to compile (even if you "ctype" them). You need to set strict to false in the page directive, but then your code behinde is not strict either. This was NOT the behavior in 1.1. I have yet to find a way to set strict to true and use container.dataitem.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Gabe December 21, 2005 @ 5:17am
In regards to the rant in my last post, I finally figure it out...

ctype(container.dataitem,datarow)("field")

I was constrating on the type of the field, not the dataitem object (Duh).
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Bob Archer December 21, 2005 @ 5:38am
I don't know Rick. It seems to me that if you disable viewstate it should not populate the controls value on the postback. Does'nt load viewstate run at page init?

We actually had a similar problem in 1.1 where we had a disabled textbox field on the form that we updated client side, then we used the value of that to store in the db.

Well, it turns out that value was not being reflected in the control.value properly. In order to get this to work we have to enable the textbox client side on save so the value would be updated.

BOb
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Rick Strahl December 21, 2005 @ 11:06am
Bob,

But a TextBox posts back. It DOESN'T REQUIRE ViewState. The value is in the POST buffer - ASP.NET just fails to assign it back to the textbox. I don't see any logical reason why it shouldn't reassign the value or a use case where you wouldn't want the value to be reassigned.

Disabled textboxes are different, because disabled textBoxes don't post back and those don't work unless ViewState is on.

This exactly the scenario WHY I use ReadOnly. I want the value to display but I also want it to post back so that it can be auto-databound and saved along with the other values.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Divya December 26, 2005 @ 1:27am
Folks,

Instead of setting Readonly to true, we can achieve the same behaviour by setting the attribute contentEditable to false and the postback also happens comfortably. So just substitute Readonly by contentEditable (setting it to false ofcourse)

Thanks,
Divya
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Rick Strahl December 26, 2005 @ 11:32pm
Divya, that's a good suggestion. Gotta try that out. Does that work with non-IE browsers?

Still I think that READONLY should work in this scenario especially since HTTP/HTML posts the value back.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Scott December 27, 2005 @ 10:35am
That's interesting, I hadn't noticed that change. I'm betting it's for security though - in a readonly field, you're not expecting user input, but it's theoretically possible that someone could hack it and send you different text. They probably made the change to ignore post data for disabled fields to prevent this type of hack.
However, I did a quick check on a hidden field and it doesn't do this (which I wouldn't have expected, but it's the same principle as above). So you could possibly use hidden fields to store the data too, but this does seem like something they shouldn't have added...
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by dixan martinez January 06, 2006 @ 5:35pm
Another similar problem I'm facing is having a readonly control just to avoid user modifications, but changing control's value via client-side code (javascript). When the page is posted asp.net 2.0 ignores the new value set by the client side-code. In asp.net 1.x this isn't a problem.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Scott Mitchell January 11, 2006 @ 9:30am
The reason is for security, as described in the docs:

"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."
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.readonly.aspx
# ReadOnly vs. Enabled in Version 1.x and Version 2.0 for ViewState-Disabled Forms
by Scott on Writing January 16, 2006 @ 4:41am
# SubmitDisabledControls - A New Property in ASP.NET 2.0
by Scott on Writing January 23, 2006 @ 5:33am
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Keith Patton February 20, 2006 @ 12:14pm
A very easy Javascript solution is to add onfocus="this.onblur();" to any text box. This makes it read only without having to worry about the readonly and enabled attributes.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Another sneaky solution February 27, 2006 @ 3:45am
This is what I use in my server controls:

if(ReadOnly) txtDate.Attributes["readonly"] = "readonly";
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Don March 06, 2006 @ 11:07am
thank you so much. I had a pop up calendar that was trying to set date values to read only textbox's. in v1.1 it worked fine, and in 2.0 it didnt. I had no idea what the problem was as, the value was getting posted back to the page. thanks for this blog.. it ended hours of headache.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Jared March 16, 2006 @ 12:15am
Thanks. I was beginning to panick there.
# Links of the Week
by Jeff Barnes - MCSD March 16, 2006 @ 10:26am
Links of the WeekCross Page Navigation Techniques A good reference for cross page navigation techniques...
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Al Ashry March 17, 2006 @ 9:58pm
Thank you very much for usefull information.
I was looking for a workaround.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Ralph April 13, 2006 @ 3:04am
Sorry what is the end solution to this. I have a calendar that populates a readonly text box via client side script, but then can't get the value form that textbox on postback because the textbox is readonly.
# Links of the Week
by Jeff W. Barnes April 23, 2006 @ 6:35am
Cross Page Navigation Techniques A good reference for cross page navigation techniques for both ASP.NET...
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Martin Woolley May 27, 2006 @ 8:41am
Just like to add to the thanks already receved, I had a few places where the user would select something from a pop-up search box and the value was displayed in a rad only text box. Worked fine in 1.1.
You saved me an awful lot of work.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by SAN May 30, 2006 @ 12:29am
I have a pop up calendar that is trying to set date values to read only textbox's. I am working on 2.0v it is not working. I have no idea what the problem, the value is getting posted back to the page. But when i am try to get the value of this ReadOny textbox, it shows empty value...please give me the solution immidietaly[SAN].
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Martin Woolley May 30, 2006 @ 5:24am
To SAN and Ralph,
End solution for me was to set the control to ReadOnly=false then in the page_load add the line
tbStart.Attributes.Add("contentEditable", "false");

Works just like it did in 1.1.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Aananth June 15, 2006 @ 12:47pm
Thank you So much. It gave me the clear solution for this read only issue in 2.0.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Marek July 11, 2006 @ 2:44pm
Unfortunately contentEditable is not a good option if you care for old and non-IE browsers. The option does nothing on Firefox.

IMHO, it's just stupid for .NET 2.0 to change a behavior that's been around for ages (readonly text input controls have always posted back since the time HTML was created). This is just a headache.

My solution is a recursive function that iterates through all controls in a page and whenever it finds a TextBox with the ReadOnly property set, it reads the value from Request.Params

Public Sub SetReadonlyTextBoxes(ByVal Page As Control)
For Each ctrl As Control In Page.Controls
If TypeOf ctrl Is TextBox Then
If CType(ctrl, TextBox).ReadOnly Then
CType(ctrl, TextBox).Text = HttpContext.Current.Request.Params(CType(ctrl, TextBox).UniqueID)
End If
Else
If ctrl.Controls.Count > 0 Then
SetReadonlyTextBoxes(ctrl)
End If
End If
Next
End Sub
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Vishal Mahajan July 31, 2006 @ 9:58am
@Martin Woolley

I have tried the same but it does not works...
is n't there any othe solution except javascript...

thanx in advance

vishal
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Pankaj September 21, 2006 @ 6:22am
Hey Marek
Thnx 4 the piece of code and it works wonders...i was able 2 produce the report 4 a specified date range... hey Vishal use the code supplied by Marek...it works...no need of javascript at all...if you make the text box readonly in Framework 2.0 you are still able to get the vlues using the above code....

Thnx again Marek....
# DotNetSlackers: SubmitDisabledControls - A New Property in ASP.NET 2.0
by DotNetSlackers Latest ASP.NET News October 12, 2006 @ 4:02pm
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Manjunath October 18, 2006 @ 6:09am
Thank you all guys

with this job done txtFor.Attributes.Add("contentEditable", "false")!!!!
# DotNetSlackers: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by DotNetSlackers Latest ASP.NET News October 25, 2006 @ 6:45pm
# DotNetSlackers: ReadOnly vs. Enabled in Version 1.x and Version 2.0 for ViewState-Disabled Forms
by DotNetSlackers Latest ASP.NET News November 02, 2006 @ 2:21am
# But it does work...
by DaveG November 08, 2006 @ 6:40am
How odd, it works fine if you add the Read Only attribute in code:

txtStartDate.Attributes.Add("readonly", "true")

But not if you type:

<asp:textbox id="txtStartDate" readonly="true"...

So what is the point of changing the behaviour if you type in readonly="true" in the textbox!?

Come on Microsoft!! If your user community find a bug deal with it honestly instead of giving it purpose ;)
# Yeah it does work but...
by MB December 05, 2006 @ 4:00pm
Yeah, it works perfectly if you add the Read Only attribute in the code like above and that's what I did to solve my problem. However, in five years or whenever .NET 3.0 comes out I'll probably get screwed again on this one (or possibly 2.1).
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Muhammad Atif Hussain December 06, 2006 @ 3:10am
Thanks Rick.

matifhussain@hotmail.com
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by oa.net December 08, 2006 @ 4:45pm
I have the same problem, even with viewstate enabled. The whole issue is in "readonly" regardless of viewstate.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Jp December 21, 2006 @ 3:59am
Hi...

Thnaks for giving solution for Read only text box in dotnet 2005

Regards
Jp
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Veer March 01, 2007 @ 9:10pm
Set readonly property in javascript.
For Ex: document.all("ControlName").attributes["readOnly"].value = "true";

It work fine.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by vin March 12, 2007 @ 3:33am
Hi all,
Is here any solution when i am using Mozilla Firefox.I have set the contenteditable option to false but it donsnt work in Firefox can u plz suggest some answer........
Thanks vin
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Anonymous April 18, 2007 @ 11:29am
This whole article seems like a good reason NOT to blindly disable ViewState in asp.net

Disable it for individual controls where it baloons out of control like GridViews, not on simple Textboxes.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by MS May 02, 2007 @ 12:17pm
Place the following in the Page_Load event.

this.Page.Form.SubmitDisabledControls = true;

You can keep the ReadOnly property set to true and still retrieve the postback value for the control.

Works like a charm.

Thanks,
MS
# и снова об asp:textbox readonly не сохраняет viewstate
by ASP.NET | Обсуждения @ GotDotNet.ru May 21, 2007 @ 12:58am
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by well wisher June 20, 2007 @ 12:17am
Hey guys thanx a lot ..... this blog saved lots of time for me.
# ASP.NET 1.1 to 2.0 migration - TextChanged event not firing! - ASP.NET Forums
by ASP.NET Forums July 16, 2007 @ 2:47pm
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Travis L. Riffle November 21, 2007 @ 9:41am
I ran into this while using the AJAX Control Toolkit Calendar Extender with a text box. The user changed the date on the calendar, but I did not want them to hand write a date. I retreived this date from the text box and used the DateTimeFormat provider to parse out the date and then I have a DateTime to send to the database.

In short, this works for me as well:

txtWhatever.Attributes.Add("readonly", "true");

I used it on page load and on databind grid event and both worked fine.


Travis
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by vittal December 04, 2007 @ 4:49am
hi this.TextBox1.Text = Request[this.TextBox1.UniqueID]; wokre for me But


in my code i have textchanged event for TextBox1 which is not working i.e Event is not Working

Plz hlep in resolving the problem

thanks
vittal
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Pradip Kachare December 14, 2007 @ 12:10am
This scared me, never imangined something so simple that would work in 1.1 would stop working in 2.0, Thanks rick, Request[this.TextBox1.UniqueID]; worked well in my machine
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by ritz March 04, 2008 @ 7:32am
thanks a lot budy !!
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by CP April 07, 2008 @ 5:44am
Thanks a lot. It solved my problem too.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Piyush Trivedi April 08, 2008 @ 10:49pm
Thank u Very much.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Kamran Shahid April 16, 2008 @ 4:50am
Thanks Rick
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Paulo Pereira July 07, 2008 @ 3:59am
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 ReadOnly behavior change when EnableViewState is false
by Brian October 24, 2008 @ 10:18am
Thanks, Rick.

this.TextBox1.Text = Request[this.TextBox1.UniqueID]; is just what I needed to fix my problem.

How would I go about implementing this to other controls such as radiobutton, checkbox and dropdownlist??

Thanks.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by James December 11, 2008 @ 9:02am
thanks for the suggestions, this definitely had me confused when updating a project to .net 2.0
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Wolfric Elkrose March 12, 2009 @ 5:30pm
Thanks Rick, I've always relied on your errors :D cause mine seems to match pretty much almost every problem you have run into.

This one is a good one, though I think the uniqueid is a smart way to keep the textboxes under control. I ran into this one today using a calendar control to update a textbox with Ajax, which of course causes the issue.

Not really an issue, just that the front end actually reverts to it's natural defaults, that's nothing new to javascript or .Net gurus. But like you said, the updated information is there, you have to manually call it.

Thanks again for your dedicated posts of problems you have run into, it's a real treat to be able to see steady resources from other developers.

Wolf
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Rathinasabhapathik March 31, 2009 @ 6:24am
Great work... this is working fine in my pages.

thanks :)
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Vegeta October 24, 2009 @ 10:37am

TextBox1.Attributes.Add("readonly", "true"); worked for me too.

Thank You.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Anil August 16, 2010 @ 12:14am
thanks for your Help.

txtId.Attributes.Add("readonly", "true"); worked find for me..
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
by Vijay September 01, 2010 @ 3:19am
Thanks for the solution Divya...that was easy and quick one !
 


West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2014