Using javascript to set the value of a hidden field, then access the value with serveride c # code

I use a html nested unordered list that is called as a dropdown. When a tag tag is clicked inside an element of the list of internal lists, it runs some javascript, which should set the value of the hidden field in the text for the link that the button was clicked on.

It seems javascript is working - I used a warning to read the value from a hidden field, but then when I try to put that value in a querystring in my asp.net C # code behind - it pulls the initial value - not the javascript value.

I assume this is because javascript is the client side, not the server side, but does anyone have an idea how I can get this working

HTML

<div class="dropDown accomodation"> <label for="accomodationList">Type of accomodation</label> <ul class="quicklinks" id="accomodationList"> <li><a href="#" title="Quicklinks" id="accomodationSelectList">All types <!--[if IE 7]><!--></a><!--<![endif]--> <!--[if lte IE 6]><table><tr><td><![endif]--> <ul id="sub" onclick="dropDownSelected(event,'accomodation');"> <li><a href="#" id="val=-1$#$All types" >All types</a></li> <li><a href="#" id="val=1$#$Villa" >Villa</a></li> <li><a href="#" id="val=2$#$Studio" >Studio</a></li> <li><a href="#" id="val=3$#$Apartment" >Apartment</a></li> <li><a class="last" href="#" id="val=4$#$Rustic Properties" >Rustic Properties</a></li> </ul> <!--[if lte IE 6]></td></tr></table></a><![endif]--> </li></ul> </div> <input type="hidden" ID="accomodationAnswer" runat="server" /> 

Javascript

  if(isChildOf(document.getElementById(parentList),document.getElementById(targ.id)) == true) { document.getElementById(parentLi).innerHTML = tname; document.getElementById(hiddenFormFieldName).Value = targ.id; alert('selected id is ' + targ.id + ' value in hidden field is ' + document.getElementById(hiddenFormFieldName).Value); } 

C # code

 String qstr = "accom=" + getValFromLiId(accomodationAnswer.Value) + "&sleeps=" + getValFromLiId(sleepsAnswer.Value) + "&nights=" + getValFromLiId(nightsAnswer.Value) + "&region=" + getValFromLiId(regionAnswer.Value) + "&price=" + Utilities.removeCurrencyFormatting(priceAnswer.Value); 
+4
source share
2 answers

I would do the following: first remove the runat='server' attribute from the hidden field:

 <input type="hidden" ID="accomodationAnswer" /> 

Now, on the server where you want to read this value, do the following:

 string accomodationAnswer = Request.Form["accomodationAnswer"]; // now use accomodationAnswer instead of accomodationAnswer.Value // in the C# code that you indicated you are using 

That should do it.

+12
source

try it

if you are using .net 4.0, then in the page header.

Language = "C #" AutoEventWireup = "true" CodeFile = "Page.cs" Inherits = "Page"

Along with this write:

 ClientIDMode="Static" 

This helps not to change the server-side management identifier at runtime.

Now

Set value in javascript as

document.getElementById ("hiddenField"). value = "Vallue";

And access to codebehind as shown below.

string hiddenVallue = hiddenField.Value.ToString ();

+5
source

All Articles