CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Darrell Norton's Blog [MVP]

Fill in description here...

Is it possible to set the value of a read-only textbox client-side and access the result server-side?

I coworker of mine is having trouble accessing the value of a read-only textbox set via client-side script:

If you are like me you want to use as much client-side power as you can.  Part of communicating the results of this power to the ASP.NET code on the server often involves populating an from client script so that you can read the corresponding object on the server.  Often, though, we don't want the user to be able to interact with the textbox on the client.  If we don't want them to see it then you can set the CSS properties (style= or class= or by a selector in the CSS script block) to hide or not display the textbox.  That's easy enough.  But what if you want the user to see the textbox but not interact with it?  You'd think that setting the enabled attribute on the element would be the thing, right?  Well, sure, that looks like it works.  The textbox shows and your client-side script can interact with it.  What's the catch? None if you initially populate the textbox's text property from the code behind.  Then it all is good.  But if you start with an empty textbox, populate it on the client, and then attempt to read the value on a postback you will get String.Empty no matter what is actually in the textbox.

Anyone have a reason or a workaround?



Comments

MH said:

The solution is to simply set the enabled attribute to "true" and hide it via CSS. Add a disabled html element (ie., no
runat="server") to your page. Then your client-sde code simply writes to both text boxes.
# November 14, 2003 1:52 AM

Rob said:

yeah, if it's disabled, it won't post (html spec). if it's readonly it should post. also, it'll need the name attribute set (id isn't enough).
# November 14, 2003 3:24 AM

Nicole Calinoiu said:

For Rob:

According the the HTML spec, readonly elements _may_ be successful (i.e.: submitted with the form), but they aren't required to be. That's why I'd suspect a browser-dependency issue if the element is not disabled.
# November 14, 2003 3:56 AM

Ben Evans said:

Here is a tip... when he sets the value of the read-only textbox he should also make a HIDDEN input with the same value and name. The Server-Side scripts can then access that hidden field, which the client cannot change anyways.
# November 14, 2003 4:28 AM

Mark Bonafe said:

Ben has stolen my thunder. That's exactly what I have done in the past, Ben. Works great.
# November 14, 2003 7:55 AM

Rob said:

The term used was "enable," so it didn't sound like he was aware of readonly.

Are you aware of any current browser which doesn't post readonly input? I'm not.

In which case, the hidden input tricks are unnecessary kludge work, when readonly would work.

# November 19, 2003 4:24 AM

Ian said:

Thanks, guys. I had the same problem, and found that changing my TextBox from

tb.Enabled=false;

to

tb.Readonly=true;

mean that my client script-assigned value was available at the server (IE6)
# January 26, 2004 9:34 AM
Check out Devlicio.us!