Welcome to the Axosoft Community, Sign in | Register | Help
in Search

Jonas Bush

...it's Blog Blog Blog!

Readonly Textboxes

Hey, I'm blogging again!

During our work on OnTime 2007, we completely rewrote the web portion from scratch, to take full advantage of the new features in ASP.Net 2.0. Doing this let us see some interesting changes in the framework. One of them is as follows:

In OnTime, you have the option of making a field readonly using field templates. For example, if you didn't want people changing the name of a defect, you could accomplish that with field templates and workflow. OnTime dynamically creates the add/edit defect page based on your field template, so if you have a readonly field, it creates code similar to the following:

TextBox tb = new TextBox();
tb.Id = "Name";
tb.ReadOnly = true;
tb.Text = "Test Defect";
Page.Form.Controls.Add(tb);

So this textbox doesn't actually exist on the page at design time. Due to a way we handle postback events, and an interesting quirk in how the TextBox control handes the LoadPostData method (note, you need Reflector 5.0 or greater to see that link), the data inside the textbox would disappear after postback. Since we needed a way to make the textbox readonly, and we couldn't use the server-side property to affect that, we ended up changing it to this:
 
tb.Attributes.Add("readonly", "readonly");

Which let us keep the textbox readonly (that's all the ReadOnly property was doing to the resultant HTML anyway), and trick ASP.Net into not tossing away our data.

Hopefully this can save some of you the headache we ran into!

(Edit: for whatever reason, that code:// link isn't showing up, so you can just cut and paste it here)

code://System.Web:2.0.0.0:b03f5f7f11d50a3a/System.Web.UI.WebControls.TextBox/LoadPostData(String,System.Collections.Specialized.NameValueCollection):Boolean 

Published Thursday, March 22, 2007 9:14 AM by Jonas
Filed under: ,

Comments

 

tboland said:

Came aross this issue a while back.  I preferred to go with add js attribute OnFocus

tb.Attributes.Add("onFocus", "this.blur()")

to produce:

<input type="text" name="FirstName" onFocus="this.blur()">

March 22, 2007 10:20 AM
Anonymous comments are disabled


© 2002 - 2007, Axosoft, LLC. All Rights Reserved. | Privacy
Bug Tracking | Defect Tracking Videos | Help Desk Software