katit
katit

Reputation: 17905

Set value of disabled input from jQuery for ASP.NET

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

Answers (3)

Mike Cheel
Mike Cheel

Reputation: 13106

You can also set the element to readonly instead of disabled to get the values posted to the server.

Upvotes: 2

Raymond
Raymond

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

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

Related Questions