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

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.

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

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);

Method 2

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.

Method 3

Depending when you are reading the value on the server side, it might not be updated on the control yet – essentially if you are doing it in a change event handler, and the control that raises the change event gets updated before the hidden control, then calling hdnSelectedTicket.Value can still return the old value.

The easiest way to get around this issue is to cheat and get it straight from the Form collection:

var ticketId = Request.Form[hdnSelectedTicket.UniqueID];


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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x