简体   繁体   English

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

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

I am using a nested html unordered list styled as a drop down.我正在使用样式为下拉列表的嵌套 html 无序列表。 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.当点击内部列表列表项中的 a 标签时,它会触发一些 javascript,它应该将隐藏字段的值设置为被点击的链接的文本。

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. javascript 似乎工作 - 我使用警报从隐藏字段中读取值但是然后当我尝试将该值放入我的 asp.net c# 代码中的查询字符串中时 - 它提取初始值 - 而不是 javascript 设置值.

I guess this is because the javascript is client side not server side but has anyone any idea how i can get this working我想这是因为 javascript 是客户端而不是服务器端,但有人知道我如何让它工作

HTML 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 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 C# 代码

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

I would do this: First, remove the runat='server' attribute from the hidden field (inside body):我会这样做:首先,从隐藏字段(体内)中删除runat='server'属性:

<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.那应该这样做。

Try this尝试这个

if you are using .net 4.0 then in page header.如果您使用的是 .net 4.0,则在页眉中。

Language="C#" AutoEventWireup="true" CodeFile="Page.cs" Inherits="Page" 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它有助于在运行时不更改服务器端控件 ID

Now现在

Set the value in javascript as将 javascript 中的值设置为

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

And access in codebehind like below.并在代码隐藏中访问,如下所示。

string hiddenVallue=hiddenField.Value.ToString();字符串 hiddenValue=hiddenField.Value.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在javascript中设置asp.net隐藏字段并访问c#代码后面的值 - how to set asp.net hidden field in javascript and access the value in c# code behind 尝试使用从Javascript到C#的隐藏字段发送值 - Trying to send value using Hidden Field from Javascript to C# 尝试使用隐藏字段将值从ASP.NET JavaScript脚本传递给我的C#代码隐藏函数 - Trying to pass value from ASP.NET JavaScript script to my C# Code Behind function using Hidden Field 如何从 asp.net 中的 java 脚本代码(客户端)访问隐藏字段值 c# - How to access hidden field value from java script code(Client side) in asp.net c# 我想从C#到我的JavaScript隐藏字段值 - I want hidden field value from c# to my javascript Ajax调用C#代码后失去隐藏字段的值 - loosing hidden field value after ajax call to c# code 试图将值从ASP.NET JavaScript脚本传递到我的C#隐藏字段 - Trying to pass value from ASP.NET JavaScript script to my C# Hidden Field 如果其可见性设置为false,则访问隐藏字段值(使用C#) - Access hidden fields value if its Visibility set to false(using C#) 如何使用Javascript从C#代码中设置的div内的span元素获取值 - How to get the value from span element inside div that are set from C# code using Javascript 从C#中的IList访问字段值 - Access the field value from IList in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM