Srb1313711
Srb1313711

Reputation: 2047

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

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?

Upvotes: 2

Views: 21753

Answers (9)

Muhammad Awais
Muhammad Awais

Reputation: 4502

I had the same issue with jQuery, however fixed with a trick:

 $('#txtBoxID').removeAttr('readonly');
/// Do your stuff here..

 $('#txtBoxID').add('readonly');

;)

Upvotes: 0

Arm0geddon
Arm0geddon

Reputation: 486

You can actually get the value of a readonly textbox if it is within the form.

var text = Page.Request.Form[TextBox.UniqueID];

Sorry this response is a few years late!

Upvotes: 1

Chinh Vo Wili
Chinh Vo Wili

Reputation: 85

Please use:

Request.Form[txtText.UniqueID]

Upvotes: 0

Aravind Rengasamy
Aravind Rengasamy

Reputation: 169

Apply in asp.cs Page : AttrName.Attributes.Add("readonly", "readonly");

Upvotes: 0

Peter
Peter

Reputation: 17

You can also leave the readOnly attribute off in the asp:TextBox control and use jquery $('#textbox').attr('readOnly','readOnly').

Upvotes: 1

Kapil
Kapil

Reputation: 191

Write the following code in your code behind file, for asp.net .cs or class file and for vb in .vb file.

You will have to write it on page load event, depending on if you want it before postback or after or irrespective of both...

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

Upvotes: 2

Abhitalks
Abhitalks

Reputation: 28417

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.

Upvotes: 11

शेखर
शेखर

Reputation: 17614

Try to make readonly to false before posting the values through jQuery or Java-Script. It should work.

 $('#tbox').removeAttr('readonly');

Suppose I have a asp:button then on client click I need to call a function which remove the read-only attribute from the text-box.

<asp:Button ID="MessageButton" runat="server" Text="Hellow"
     OnClientClick="return changeAttribute()" />
<asp:textbox runat="server" id="checkIn" ClientIDMode="Static" ReadOnly="true">
</asp:textbox>

and change attribute function in javascript as follows

function changeAttribute()
{
      $('#checkIn').removeAttr('readonly');
      return true;
}

Upvotes: 1

Aditya Singh
Aditya Singh

Reputation: 9612

Use HTML input tag instead of asp:textbox

Use this:-

<input type="text" readonly="readonly" runat="server" id="checkIn" clientidmode="Static"/>

Instead of this:-

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

Upvotes: 2

Related Questions