简体   繁体   中英

asp.net text doesn't send value when disabled

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

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/

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.

ref from :

We will do it by using few way's

  • Disable the textbox after the values sent to the server side( or enable the textbox and send values finally disable the textbox )

  • store the textbox values 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 field for store text box values for send the values to server side...

  • Or use readonly attribute instead of enable and disable attribute

与其使用 Enable 属性,不如尝试在要禁用时在样式中使用 disabled 并在要启用时更改属性值。

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

or

inputBox.prop('readonly', false);

now server can read value of text box

在通过 javascript/jquery 提交之前启用,然后再次从服务器禁用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM