ASP.net Migration: ReadOnly TextBox persistance



If you ever migrated from ASP.net 1.0 to ASP.net 2.0, you might have noticed the following issue.

Imagine you've got a page like the following one:
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
 
    <script type="text/javascript">
        function populate() {
            var txtTest = document.getElementById("<%=txtTest.ClientID %>");
            txtTest.value = "test 123";
        }
    </script>
 
</head>
<body>
    <form runat="server">
    <div>
        <asp:TextBox runat="server" ID="txtTest" ReadOnly="true" Text="abc"></asp:TextBox>
        <input type="button" onclick="populate()" value="Test" />
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
    </div>
    </form>
</body>
</html>
 

Basically what we've got here is client-side code modifying the contents of a server-side based control.

The user clicks on the test button, which sets the value of the readonly textbox "txtTest" to test 123 (the test button being our input method instead of the keyboard).

If we click submit (in ASP.net 1.0) it sends the value to the server assigning/persisting our readonly textbox with the new value (client-side javascript assigned). In ASP.net 2.0 you'll find that ASP.net simply ignores the changes we made via the test button (ignoring our client-side action).

This isn't a bug though, this is purely by design - its all about security; preventing a malicious user to change readonly values. Quite a logical design since readonly values are supposed to be readonly, right?

What about legitimate reasons e.g. a datepicker?

One workaround would be to rather set the readonly value using the attribute collection of the control:
 
txtTest.Attributes.Add("readonly", "readonly");
 

What about if we've got this issue throughout our application e.g. we just migrated from ASP.net 1.0? We can always go through every page and make the necessary changes or alternatively change the default behaviour of the textbox control to act like it did in ASP.net 1.0.

Or what if we prefer the ASP.net 1.0 way of handling readonly values?

We can always simply change the behaviour of the textbox control by creating a WebControlAdapter:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls.Adapters;
using System.Web.UI.WebControls;
 
namespace CSTruter
{
    public class TextBoxAdapter : WebControlAdapter
    {
        protected override void OnInit(EventArgs e)
        {
            TextBox sender = this.Control as TextBox;
 
            if (sender.Attributes["secure"] == null)
            {
                if (sender.ReadOnly == true)
                {
                    sender.ReadOnly = false;
                    sender.Attributes.Add("readonly", "readonly");
                }
            }
            base.OnInit(e);
        }
    }
	/* Since the value of a multiline textbox doesn't persist when we create an adapter, we need to do the following */
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            TextBox sender = this.Control as TextBox;
 
            base.RenderBeginTag(writer);
            if (sender.TextMode == TextBoxMode.MultiLine)
            {
                System.Web.HttpUtility.HtmlEncode(sender.Text, writer);
            }
            else
            {
                base.RenderContents(writer);
            }
            base.RenderEndTag(writer);
 
        }
}
 

Adding the previous snippet to our solution, along with the appropriate browserfile will change the default behaviour of all the TextBoxes in our application to function like it did in ASP.net 1.0. If we want a secure readonly textbox, we can simply add an attribute named secure to the textbox, inforcing the ASP.net 2.0 model e.g.
 
<asp:TextBox runat="server" ID="txtTest" Text="abc" Secure="true"></asp:TextBox>
 








Post comment

Name *
Email
Title
Body *
Security Code
*
* Required fields

Latest Posts

Be the best stalker you can be


2011-12-13 22:33:54

Syntactic sugar (C#): Enum


2011-08-04 16:50:18

Top 5 posts

Simple WYSIWYG Editor


Creating a WYSIWYG textbox for your website is actually quite simple.
2007-02-01 12:00:00

Moving items between listboxes in ASP.net/PHP example


Move items between two listboxes in ASP.net(C#, VB.NET) and PHP
2008-06-12 17:07:43

Cross Browser Issues: Firefox Word Wrapping


Firefox word wrapping issues
2008-06-09 09:51:21

Populate a TreeView Control C#


Populate a TreeView control in a windows application.
2009-08-27 16:01:03

C# YouTube : Google API


Post on how to integrate with YouTube using the Google Data API
2011-03-12 08:37:51