使用 javascript 设置隐藏字段的值,然后从服务器端 C# 代码访问值

2024-03-11

我正在使用一个嵌套的 html 无序列表,样式为下拉列表。当单击内部列表列表项中的 a 标记时,它会触发一些 JavaScript,该 JavaScript 应该将隐藏字段的值设置为所单击链接的文本。

javascript 似乎可以工作 - 我使用警报从隐藏字段中读取值,但是当我尝试将该值放入我的 asp.net c# 代码后面的查询字符串中时 - 它提取初始值 - 而不是 javascript 设置值。

我想这是因为 javascript 是客户端而不是服务器端,但有人知道我如何让它工作

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);

我会这样做:首先,删除runat='server'隐藏字段(主体内部)的属性:

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

现在,在您想要读取该值的服务器上,执行以下操作:

string accomodationAnswer = Request.Form["accomodationAnswer"];

// now use accomodationAnswer instead of accomodationAnswer.Value 
// in the C# code that you indicated you are using

应该可以做到这一点。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 javascript 设置隐藏字段的值,然后从服务器端 C# 代码访问值 的相关文章

随机推荐