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