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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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/
Method 2
It will not post back to the server if you’ve got the textbox disabled.
Set a label to the value of the textbox and set a hidden field to the value as well.
We will do it by using few way’s
- Disable the
textboxafter the values sent to the server side( or enable thetextboxand send values finallydisablethetextbox)
- store the
textboxvalues to any other controls with the control(span) should be hide and get the hided control(span) values to send server side ‘
- You can use
Hidden fieldfor store text box values for send the values to server side…
- Or use
readonlyattribute instead of enable and disable attribute
Method 3
Instead of using Enable attribute, try using disabled in style when you want to disabled and change the attribute value when you want to enable.
Method 4
var inputBox = $("#<%=txtUniqueNo.ClientID%>");
inputBox.prop('readonly', true);
or
inputBox.prop('readonly', false);
now server can read value of text box
Method 5
enable before submitting through javascript/jquery and again disable from server again.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0