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:
The Voices of Reason
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
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
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
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
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
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
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
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
"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
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
if(ReadOnly) txtDate.Attributes["readonly"] = "readonly";
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
I was looking for a workaround.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
You saved me an awful lot of work.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
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
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
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
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
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....
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
with this job done txtFor.Attributes.Add("contentEditable", "false")!!!!
# But it does work...
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...
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
matifhussain@hotmail.com
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
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
For Ex: document.all("ControlName").attributes["readOnly"].value = "true";
It work fine.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
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
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
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
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
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
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
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
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
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
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
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
thanks :)
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
TextBox1.Attributes.Add("readonly", "true"); worked for me too.
Thank You.
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
txtId.Attributes.Add("readonly", "true"); worked find for me..
# re: ASP.NET 2.0 ReadOnly behavior change when EnableViewState is false
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.