Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using javascript to set value of hidden field then access value from serverside c# code

Tags:

javascript

c#

I am using a nested html unordered list styled as a drop down. When the a tag within the inner lists list item is clicked it trigger some javascript which is supposed to set the value of a hidden field to the text for the link that was clicked.

The javascript seems to work - I used an alert to read the value from the hidden field but then when I try to put that value in the querystring in my asp.net c# code behind - it pulls the initial value - not the javascript set value.

I guess this is because the javascript is client side not server side but has anyone any 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);
like image 755
mancmanomyst Avatar asked Dec 02 '22 08:12

mancmanomyst


2 Answers

I would do this: First, remove the runat='server' attribute from the hidden field (inside body):

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

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

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.

like image 78
Jason Bunting Avatar answered Dec 03 '22 21:12

Jason Bunting


Try this

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

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

Alongwith this write:

ClientIDMode="Static"

It helps in not changing the server side control id at runtime

Now

Set the value in javascript as

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

And access in codebehind like below.

string hiddenVallue=hiddenField.Value.ToString();

like image 44
Raj Avatar answered Dec 03 '22 20:12

Raj