Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a screen that gets displayed and depending on some answers, I dynamically build a radio button list and calculate its values in javascript. The selected value I save into a hidden field. In C# the value of the hidden field is always "".

This is sort of what I have:
MY HTML - shows 3 radio button options whose values are derived at run time in javascript based on answers provided on the screen once page has loaded

<div class="testSection">
<asp:HiddenField ID="hidAnswer" runat="server" />
<asp:HiddenField ID="hidMin" runat="server" />
<asp:HiddenField ID="hidMax" runat="server" />
<table class="testTable" >
<tbody>
<tr>
<td style="padding-left: 18px">
<asp:Label ID="lblPrompt" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td id="rbtNoLapse">
<div id="dvnTest1" title="rbTest1" >
<input id="rbtNo" type="radio" name="Lapse" />
<label id="LblNo" for="rbtNo" ></label>
</div>
<div id="dvMin" title="rbTest1" >
<input id="rbtMin" type="radio" name="Lapse" />
<label id="LblMin" for="rbtMin" ></label>
</div>
<div id="dvMax" title="rbTest1" >
<input id="rbtMax" type="radio" name="Lapse" />
<label id="LblMax" for="rbtMax" ></label>
</div>
</td>
</tr>
</tbody>
</table>
</div>

In javascript while validating the the selection I get the selected item. The alert is showing the correctly selected item in the hidden field hidAnswer.

var list = document.getElementById("rbtNoLapse");
var inputs = list.getElementsByTagName("input");
var selected;


for (var i = 0; i < inputs.length; i++)
{
if (inputs[i].checked)
{
selected = inputs[i];
break;
}
}
if (selected)
{
hidAnswer = selected.value;
}
alert( hidAnswer);




in C#, getting the values from the form, for both hidden fields the value = "" - I dont understand why they do not have the same values as is showing in the alert in javascript.

if (hidMin.Value != "0")
{
if (hidAnswer.Value == "01")
{
do something
}

}


I cannot find a simialr scenario that I can refer to - I have searched all over.
Any help is appreciated.

What I have tried:

I have searched all over for a similar scenario but could not find one exactly like mine. I have tried loads of suggestions but nothing works. Im obviously doing something wrong, have bad syntax or trying to do something thats just not possible ! Any help is appreciated.
Posted
Updated 11-Sep-18 9:10am
Comments
F-ES Sitecore 11-Sep-18 11:37am    
I goolged "c# asp.net set hidden field value" and found quite a few articles\questions that cover this scenario with code that shows how you update a hidden field

https://www.codeproject.com/Questions/445390/How-can-i-set-value-to-asp-hidden-field-using-java

Note the "ClientID" bit in the solution above, you might need that depending on other things like where the controls are, if you are using master pages etc.

hidAnswer = selected.value;

That needs to be:
document.getElementById("hidAnswer").value = selected.value;

As it stands, you're just assigning the value to an un-declared local variable.
 
Share this answer
 
HiddenField is an ASP.NET Server Control, so you can't access them by directly referencing their ID. To access them in JavaScript, you would need to use the function document.getElementById and pass along the ID of the control.

For example:

JavaScript
document.getElementById('<%=hidAnswer.ClientID%>').value = 'set whatever value here';


Note that we are using the ClientID, instead of the ID of the HiddenField, the reason for this is because ASP.NET will render a different ID in the markup especially if your control is nested within a NamingContainer. So the actual ID could look something like clt0_clt1_hidAnwser.

In .NET 4.x, The ClientMode property was introduced to avoid the autogenerated ids being rendered See: Control.ClientIDMode Property (System.Web.UI) | Microsoft Docs[^]

So if you set ClientMode= "Static" for a Server Control, then you can safely use something like this to access the control:

JavaScript
document.getElementById('hidAnswer').value = 'set whatever value here';
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900