Skip to content Skip to sidebar Skip to footer

Updating A Hidden Field In Asp.net Via Javascript

I inherited some JavaScript that I was told to integrate into our ASP.NET site. I thought this would be straightforward but it's turning out to be a bit of a challenge. The code l

Solution 1:

Dunno about ASP.net, but I know in the browser DOM, a hidden field's value is value all lower case, not Value. It's the value attribute that the browser will submit, and by setting sQuestion.Value, you've failed to set its submitted value.

Solution 2:

Is that JavaScript function on the same aspx page? if not I believe that

var sQuestion = document.getElementById('<%=sQuestion.ClientID%>');

will fail.

If you use jQuery you can use:

$("[id *= 'substringOfTheASPId']").val("new value")

Solution 3:

You set sQuestion.Value instead of sQuestion.value.

I found this in an answer to a similar question.

Solution 4:

I don't believe .NET expects that value of a HiddenField to be manipulated on the clientside, so it probably just reads the ViewState value back everytime. You can use a regular hidden input and make it server control by adding the "runat" property like this:

<inputtype="hidden"id="myInputId" runat="server" />

In the code behind, you can read the value in a manner similar to what you have above. It will be of the type System.Web.UI.HtmlControls.HtmlInputHidden.

Post a Comment for "Updating A Hidden Field In Asp.net Via Javascript"