使用JavaScript来设置隐藏字段的值,然后从服务器端的C#代码访问值 [英] Using javascript to set value of hidden field then access value from serverside c# code
问题描述
我使用的样式为下拉一个嵌套的HTML无序列表。当内部列表列表项中的一个标签被点击它引发一些JavaScript这应该设置隐藏字段的文本被点击的链接的价值。
JavaScript的似乎工作 - 我用了一个警告,要求其隐藏字段阅读的价值,但后来当我试图把该值在查询字符串在我的asp.net C#代码背后 - 它拉的初始值 - 不是的JavaScript设定值。
我想这是因为JavaScript是客户端而不是服务器端,但有没有人任何想法如何我能得到这个工作。
HTML
< DIV CLASS =下拉住宿>
<标签=accomodationList>住宿和LT类型; /标签>
< UL类=快速链接ID =accomodationList>
<李>< A HREF =#称号=快速链接ID =accomodationSelectList>各类
<! - [如果IE 7><! - - >&下; / A>&下;! - &下;![ENDIF] - GT;
<! - [如果LTE IE 6><表>< TR>< TD><![ENDIF] - >
< UL ID =亚健康的onclick =dropDownSelected(事件,'住宿');>
<李>< A HREF =#ID =VAL = -1 $#$所有类型>所有类型< / A>< /李>
<李>< A HREF =#ID =VAL = 1 $#$别墅>别墅LT; / A>< /李>
<李>< A HREF =#ID =VAL = 2 $#$工作室>工作室< / A>< /李>
<李>< A HREF =#ID =VAL = 3 $#$公寓>公寓< / A>< /李>
<李><一类=最后的href =#ID =VAL = 4 $#$仿古属性>仿古物业LT; / A>< /李>
< / UL>
<! - [如果LTE IE 6>< / TD>< / TR>< /表>< / A><![ENDIF] - >
< /李>< / UL>
< / DIV>
<输入类型=隐藏ID =accomodationAnswer=服务器/>
的JavaScript
如果(isChildOf(的document.getElementById(parentList)的document.getElementById(targ.id))==真)
{
文档。的getElementById(parentLi).innerHTML = TNAME;
的document.getElementById(hiddenFormFieldName).value的= targ.id;
警报('选择的ID'+ targ.id +'在隐藏字段值为'+的document.getElementById(hiddenFormFieldName).value的);
}
C#代码
字符串QSTR =ACCOM =+ getValFromLiId(accomodationAnswer.Value)+&放大器;睡=+ getValFromLiId(sleepsAnswer.Value)+&放大器;夜=+ getValFromLiId(nightsAnswer.Value)+&放大器;区域=+
getValFromLiId(regionAnswer.Value)+&安培;价格=+ Utilities.removeCurrencyFormatting(priceAnswer.Value);
我会做这样的:首先,删除 RUNAT =服务器
从隐藏字段属性:
<输入类型=隐藏ID =accomodationAnswer/>
现在,服务器,要读取值上,这样做:
字符串accomodationAnswer =的Request.Form [accomodationAnswer];
//现在用的,而不是accomodationAnswer.Value
//在您指定使用的是
,C#代码accomodationAnswer
这应该这样做。
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) + "®ion=" +
getValFromLiId(regionAnswer.Value) + "&price=" + Utilities.removeCurrencyFormatting(priceAnswer.Value);
I would do this: First, remove the runat='server'
attribute from the hidden field:
<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.
这篇关于使用JavaScript来设置隐藏字段的值,然后从服务器端的C#代码访问值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!