Darrell Norton's Blog [MVP]

Sponsors

The Lounge

News

  • Darrell Norton pic

    MVP logo

    View Darrell Norton's profile on LinkedIn

    Currently Reading:

    weewar.com

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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?


Posted 11-14-2003 5:37 AM by Darrell Norton

[Advertisement]

Comments

Nicole Calinoiu wrote re: Is it possible to set the value of a read-only textbox client-side and access the result server-side?
on 11-14-2003 1:31 AM
Cannot reproduce. Is it only read-only, or is it disabled as well? Also, in which browser(s) are you seeing the problem?
MH wrote re: Is it possible to set the value of a read-only textbox client-side and access the result server-side?
on 11-14-2003 1:52 AM
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.
Rob wrote re: Is it possible to set the value of a read-only textbox client-side and access the result server-side?
on 11-14-2003 3:24 AM
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).
Nicole Calinoiu wrote re: Is it possible to set the value of a read-only textbox client-side and access the result server-side?
on 11-14-2003 3:56 AM
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.
Ben Evans wrote re: Is it possible to set the value of a read-only textbox client-side and access the result server-side?
on 11-14-2003 4:28 AM
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.
Mark Bonafe wrote re: Is it possible to set the value of a read-only textbox client-side and access the result server-side?
on 11-14-2003 7:55 AM
Ben has stolen my thunder. That's exactly what I have done in the past, Ben. Works great.
Rob wrote re: Is it possible to set the value of a read-only textbox client-side and access the result server-side?
on 11-19-2003 4:24 AM
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.

Ian wrote re: Is it possible to set the value of a read-only textbox client-side and access the result server-side?
on 01-26-2004 9:34 AM
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)