Contact   •   Articles   •   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

The Voices of Reason


 

# ASP.NET 2.0ではEnableViewStateの仕様が変わった?

ASP.NET 2.0??EnableViewState?????????

Gabe
December 21, 2005

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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.

Gabe
December 21, 2005

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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).

Bob Archer
December 21, 2005

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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

Rick Strahl
December 21, 2005

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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.

Divya
December 26, 2005

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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

Rick Strahl
December 26, 2005

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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.

Scott
December 27, 2005

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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

dixan martinez
January 06, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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.

Scott Mitchell
January 11, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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

Scott on Writing
January 16, 2006

# ReadOnly vs. Enabled in Version 1.x and Version 2.0 for ViewState-Disabled Forms


Scott on Writing
January 23, 2006

# SubmitDisabledControls - A New Property in ASP.NET 2.0


Keith Patton
February 20, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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.

Another sneaky solution
February 27, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

This is what I use in my server controls:

if(ReadOnly) txtDate.Attributes["readonly"] = "readonly";

Don
March 06, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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.

Jared
March 16, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

Thanks. I was beginning to panick there.

Jeff Barnes - MCSD
March 16, 2006

# Links of the Week

Links of the WeekCross Page Navigation Techniques A good reference for cross page navigation techniques...

Al Ashry
March 17, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

Thank you very much for usefull information.
I was looking for a workaround.

Ralph
April 13, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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.

Jeff W. Barnes
April 23, 2006

# Links of the Week

Cross Page Navigation Techniques A good reference for cross page navigation techniques for both ASP.NET...

Martin Woolley
May 27, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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.

SAN
May 30, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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].

Martin Woolley
May 30, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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.

Aananth
June 15, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

Thank you So much. It gave me the clear solution for this read only issue in 2.0.

Marek
July 11, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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

Vishal Mahajan
July 31, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

@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

Pankaj
September 21, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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


Manjunath
October 18, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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

# DotNetSlackers: ReadOnly vs. Enabled in Version 1.x and Version 2.0 for ViewState-Disabled Forms


DaveG
November 08, 2006

# But it does work...

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 ;)

MB
December 05, 2006

# Yeah it does work but...

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).

Muhammad Atif Hussain
December 06, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

Thanks Rick.

matifhussain@hotmail.com

oa.net
December 08, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

I have the same problem, even with viewstate enabled. The whole issue is in "readonly" regardless of viewstate.

Jp
December 21, 2006

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

Hi...

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

Regards
Jp

Veer
March 01, 2007

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

Set readonly property in javascript.
For Ex: document.all("ControlName").attributes["readOnly"].value = "true";

It work fine.

vin
March 12, 2007

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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

Anonymous
April 18, 2007

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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.

MS
May 02, 2007

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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


well wisher
June 20, 2007

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

Hey guys thanx a lot ..... this blog saved lots of time for me.

ASP.NET Forums
July 16, 2007

# ASP.NET 1.1 to 2.0 migration - TextChanged event not firing! - ASP.NET Forums


Travis L. Riffle
November 21, 2007

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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

vittal
December 04, 2007

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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

Pradip Kachare
December 14, 2007

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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

ritz
March 04, 2008

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

thanks a lot budy !!

CP
April 07, 2008

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

Thanks a lot. It solved my problem too.

Piyush Trivedi
April 08, 2008

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

Thank u Very much.

Kamran Shahid
April 16, 2008

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

Thanks Rick

Paulo Pereira
July 07, 2008

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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

Brian
October 24, 2008

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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.

James
December 11, 2008

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

thanks for the suggestions, this definitely had me confused when updating a project to .net 2.0

Wolfric Elkrose
March 12, 2009

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

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

Rathinasabhapathik
March 31, 2009

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

Great work... this is working fine in my pages.

thanks :)

Vegeta
October 24, 2009

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false


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

Thank You.

Anil
August 16, 2010

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

thanks for your Help.

txtId.Attributes.Add("readonly", "true"); worked find for me..

Vijay
September 01, 2010

# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false

Thanks for the solution Divya...that was easy and quick one !
 


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