Reputation: 17905
Trying to figure out how to set input's value inside JavaScript and make sure it is posted to the server in ASP.NET. It has to be disabled field too.
I tried this:
<asp:TextBox ID="DriverIdTextBox" runat="server" enabled="false" />
And then in jQuery:
$("*[id$='DriverIdTextBox']").val("JAVA");
This when posted doesn't give me "JAVA" on backend. When I enable textbox - I get this value on server just fine. I tried to enable control and disable on clientside like so:
$("*[id$='DriverIdTextBox']").val("JAVA").prop("disabled", true);
Same problem - value is not posted to server.
Basically I want to set read-only label/input whatever so user can read it but at the same time I want to post it to server when user submits form. How can I achieve this?
Upvotes: 1
Views: 7396
Reputation: 13106
You can also set the element to readonly instead of disabled to get the values posted to the server.
Upvotes: 2
Reputation: 175
You can get it's value like this
this.Request.Form[DriverIdTextBox.ClientId]
Disabled elements do not get submitted with the form,you must remove "disabled" atrribute before post back
Upvotes: 0
Reputation: 6741
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.
<asp:HiddenField ID="hdnDriverId" runat="server" />
<asp:Label ID="lblhdnDriverId" runat="server" Text="Label"></asp:Label>
this.lblhdnDriverId.Text = "";
this.hdnDriverId.Value = "";
$("*[id$='lblhdnDriverId']").val("JAVA");
$("*[id$='hdnDriverId']").val("JAVA");
Upvotes: 1