Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get text with text box when it is readonly?

Tags:

jquery

c#

asp.net

I have a text box

<asp:textbox runat="server" id="checkIn" ClientIDMode="Static" ReadOnly="true">
</asp:textbox>

The text in the text box is inputted through a Jquery DatePicker. In some code behind I am getting the text from this text box like so.

string x=checkIn.Text;

Why can I not pull the inputted date from the text box? I am guessing it is to do with the fact that it is readonly as when I remove this it works?

Can anyone help me?

like image 617
Srb1313711 Avatar asked Jan 21 '26 03:01

Srb1313711


1 Answers

In ASP.Net, if the readonly value is changed, it will revert to the original value on the postback.

You can use a wrokaround however, instead of specifying readonly declaratively, assign it as an attribute in code-behind. i.e.

instead of

<asp:textbox runat="server" id="checkIn" ReadOnly="true"...

apply this is code-behind

checkIn.Attributes.Add("readonly", "readonly");

But, the viewstate still may not work with this.

More info:

There is a subtle difference between readonly and disabled controls in HTML. The disabled ones will not be submitted with the form, however the readonly ones will. Literally, readonly is just readonly, but disabled is actually disabled.

From W3C: http://www.w3.org/TR/html401/interact/forms.html#h-17.12

(Get down to section 17.13.2 Successful controls under 17.13 Form submission)

ASP.Net however, reverts to the original value on postback if a control is declared like that i.e. if the attribute is set during init. Which is why setting the attribute later (in page load) will not affect this behavior.

like image 68
Abhitalks Avatar answered Jan 23 '26 19:01

Abhitalks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!