Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net text doesn't send value when disabled

Tags:

c#

asp.net

I have to enable and disable the text box using jQuery, which works fine. The disabled text box has value in it. But the issue I am facing is that, the disabled textbox doesn't pass value to server.When I enable it using jQuery, I see text box value in code behind (Debugging mode). Any ideas why this is happening or alternative approach to get value from disabled textbox in code behind.

HTML:

 <asp:TextBox ID="txtUniqueNo" runat="server" onkeyup = "OnChange(this)" required/>

Javascript that i use to disable in view page

var inputBox = $("#<%=txtUniqueNo.ClientID%>");
inputBox.prop('disabled', true);

Thanks

like image 713
Dave Avatar asked Dec 04 '22 06:12

Dave


1 Answers

The reason is simple, disabled inputs values aren't submitted to the server due to web-browsers submission limitation policy.

The W3 spec says that input tags that are disabled are considered invalid and should not be submitted.

Instead, use the readonly attribute:

<input type="text" readonly />

Or using jQuery:

$("#<%=txtUniqueNo.ClientID%>").attr('readonly', 'readonly');

UPDATE:

Look how to remove the readonly attribute if needed: http://jsfiddle.net/ynevet/84HrM/

like image 133
Yair Nevet Avatar answered Dec 25 '22 22:12

Yair Nevet