Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp .net hidden field can't set value with jquery

Tags:

jquery

asp.net

I can't set the value of a hidden field with jquery in asp .net.

My hidden field is declared like this:

<asp:HiddenField runat="server" ID="hdnSelectedTicket" />

And this is how I set the value:

            alert(ticketID);
            $('#<%=hdnSelectedTicket.ClientID %>').val(ticketID);
            alert($('#<%=hdnSelectedTicket.ClientID %>').val());

Both alerts show the right value but when I fetch it at the server it is empty.

like image 700
Alecu Avatar asked Jun 26 '13 11:06

Alecu


People also ask

How to set value in asp hidden field using jQuery?

#1 jQuery Code to set hidden field value by IDval(); // this gives textbox value $("#myInputHidden"). val(getTxtValue); // this will set hidden field value alert(getValue); Above jQuery code will take textbox value into the variable getTxtValue and using . val() set its value to our hidden field.


2 Answers

Set ClientIDMode="Static" and then you can use $('#hdnSelectedTicket').val(ticketID); to set the value in asp hidden field

like

asp:HiddenField ID="hdnSelectedTicket" runat="server" ClientIDMode="Static"

and

$('#hdnSelectedTicket').val(ticketID);
like image 186
Shahid Iqbal Avatar answered Oct 16 '22 22:10

Shahid Iqbal


It turns out that I was putting the hidden field inside a div that was used as a model for jquery dialog. When I removed the hidden field from the div and place it somewhere else it worked.

like image 38
Alecu Avatar answered Oct 16 '22 22:10

Alecu