检索在javascript中设置的C#中的隐藏值 [英] Retrieve a hidden value in C# that was set in javascript

查看:79
本文介绍了检索在javascript中设置的C#中的隐藏值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示的屏幕,根据一些答案,我动态构建一个单选按钮列表并在javascript中计算其值。我保存到隐藏字段中的选定值。在C#中,隐藏字段的值始终为。



这就是我所拥有的:

我的HTML - 显示3个单选按钮选项,其值在运行时以javascript为基础派生页面加载后屏幕上提供的答案



< div class =testSection>

< asp:HiddenField ID =hidAnswerrunat =server/>

< asp:HiddenField ID =hidMinrunat =server/>

< asp: HiddenField ID =hidMaxrunat =server/>

< table class =testTable>

< tbody>

< tr>

< td style =padding-left:18px>

< asp:Label ID =lblPromptrunat =server>< / ASP:标签>

< / td>

< / tr>

< tr>

< td id =rbtNoLapse>

< div id =dvnTest1title =rbTest1>

< input id =rbtNotype =radioname =Lapse/>

< label id =LblNofor =rbtNo >< / label>

< / div>

< div id =dvMintitle =rbTest1>

< input id =rbtMintype =radioname =Lapse/>

< label id =LblMinfor =rbtMin >< / label>

< / div>

< div id =dvMaxtitle =rbTest1>

< input id =rbtMaxtype =radioname =Lapse/>

< label id =LblMaxfor =rbtMax>< / label>

< / div>

< / td>

< / tr>

< / tbody>

< /表>

< / div>



在javascript中验证选择时,我得到所选项目。警报显示隐藏字段hidAnswer中正确选择的项目。



var list = document.getElementById(rbtNoLapse);

var inputs = list.getElementsByTagName(input);

var选择;





for(var i = 0; i< inputs.length; i ++ )

{

if(inputs [i] .checked)

{

selected = inputs [i] ;

休息;

}

}

如果(选择)

{

hidAnswer = selected.value;

}

alert(hidAnswer);








C#中的b $ b,从表格中获取值,对于两个隐藏字段,值= - 我不明白他们为什么没有与javascript中的警报中显示的值相同的值。



if(hidMin.Value!=0)

{

if(hidAnswer.Value = =01)

{

做点什么

}



}





我找不到我可以参考的类似场景 - 我已经搜遍了所有。

感谢任何帮助。



我尝试过:



我已经搜遍了类似的场景,但找不到一个与我相似的场景。我已经尝试了大量的建议,但没有任何作用。我显然做错了什么,有错误的语法或尝试做一些不可能的事情!感谢任何帮助。

I have a screen that gets displayed and depending on some answers, I dynamically build a radio button list and calculate its values in javascript. The selected value I save into a hidden field. In C# the value of the hidden field is always "".

This is sort of what I have:
MY HTML - shows 3 radio button options whose values are derived at run time in javascript based on answers provided on the screen once page has loaded

<div class="testSection">
<asp:HiddenField ID="hidAnswer" runat="server" />
<asp:HiddenField ID="hidMin" runat="server" />
<asp:HiddenField ID="hidMax" runat="server" />
<table class="testTable" >
<tbody>
<tr>
<td style="padding-left: 18px">
<asp:Label ID="lblPrompt" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td id="rbtNoLapse">
<div id="dvnTest1" title="rbTest1" >
<input id="rbtNo" type="radio" name="Lapse" />
<label id="LblNo" for="rbtNo" ></label>
</div>
<div id="dvMin" title="rbTest1" >
<input id="rbtMin" type="radio" name="Lapse" />
<label id="LblMin" for="rbtMin" ></label>
</div>
<div id="dvMax" title="rbTest1" >
<input id="rbtMax" type="radio" name="Lapse" />
<label id="LblMax" for="rbtMax" ></label>
</div>
</td>
</tr>
</tbody>
</table>
</div>

In javascript while validating the the selection I get the selected item. The alert is showing the correctly selected item in the hidden field hidAnswer.

var list = document.getElementById("rbtNoLapse");
var inputs = list.getElementsByTagName("input");
var selected;


for (var i = 0; i < inputs.length; i++)
{
if (inputs[i].checked)
{
selected = inputs[i];
break;
}
}
if (selected)
{
hidAnswer = selected.value;
}
alert( hidAnswer);




in C#, getting the values from the form, for both hidden fields the value = "" - I dont understand why they do not have the same values as is showing in the alert in javascript.

if (hidMin.Value != "0")
{
if (hidAnswer.Value == "01")
{
do something
}

}


I cannot find a simialr scenario that I can refer to - I have searched all over.
Any help is appreciated.

What I have tried:

I have searched all over for a similar scenario but could not find one exactly like mine. I have tried loads of suggestions but nothing works. Im obviously doing something wrong, have bad syntax or trying to do something thats just not possible ! Any help is appreciated.

推荐答案

hidAnswer = selected.value;



那个需要:


That needs to be:

document.getElementById("hidAnswer").value = selected.value;



因为它看,你只是将值赋给未声明的局部变量。


As it stands, you're just assigning the value to an un-declared local variable.


HiddenField 是一个ASP.NET 服务器控制,因此您无法通过直接引用其ID来访问它们。要在 JavaScript 中访问它们,您需要使用函数.document.getElementById 并传递控件的ID。



例如:



HiddenField is an ASP.NET Server Control, so you can't access them by directly referencing their ID. To access them in JavaScript, you would need to use the function document.getElementById and pass along the ID of the control.

For example:

document.getElementById('<%=hidAnswer.ClientID%>').value = 'set whatever value here';





请注意,我们使用的是 ClientID ,而不是 HiddenField ID ,原因是因为ASP.NET会在标记中呈现不同的ID,尤其是当您的控件嵌套在 NamingContainer 中时。所以实际ID看起来像 clt0_clt1_hidAnwser



在.NET 4.x中,引入了 ClientMode 属性以避免呈现自动生成的ID请参阅: Control.ClientIDMode属性(System.Web) .UI)| Microsoft Docs [ ^ ]



因此,如果设置 ClientMode =Static对于服务器控件,那么你可以安全地使用这样的东西来访问控件:





Note that we are using the ClientID, instead of the ID of the HiddenField, the reason for this is because ASP.NET will render a different ID in the markup especially if your control is nested within a NamingContainer. So the actual ID could look something like clt0_clt1_hidAnwser.

In .NET 4.x, The ClientMode property was introduced to avoid the autogenerated ids being rendered See: Control.ClientIDMode Property (System.Web.UI) | Microsoft Docs[^]

So if you set ClientMode= "Static" for a Server Control, then you can safely use something like this to access the control:

document.getElementById('hidAnswer').value = 'set whatever value here';


这篇关于检索在javascript中设置的C#中的隐藏值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆