简体   繁体   English

ASP.NET hiddenField使用ClientID获得价值,就像使用javascript使用母版页一样

[英]ASP.NET hiddenField get value using ClientID as using master pages using javascript

I'm using master pages in .NET and I've discovered that I can't use ID's to retrieve values because .NET adds in it's own id values. 我在.NET中使用母版页,但我发现我无法使用ID来检索值,因为.NET会添加其自身的ID值。 See this very helpful article! 看到这篇非常有帮助的文章! Can't get JQuery to work in Master Page 无法使JQuery在母版页中工作

So I went through and added classes to all the relevant fields, however I ran into a problem when it came to my hidden field <asp:HiddenField ID="softwareSelected" value="" runat="server" /> 因此,我仔细研究并向所有相关字段添加了类,但是当我遇到隐藏字段<asp:HiddenField ID="softwareSelected" value="" runat="server" />时,我遇到了一个问题

I can't add a class into it, and after more Googling I saw that people fix it by calling clientID . 我无法在其中添加一个类,经过更多谷歌搜索后,我看到人们通过调用clientID对其进行修复。

So I tried this: 所以我尝试了这个:

var myHidden = document.getElementById('<%= softwareSelected.ClientID %>');
console.log(myHidden.value);
alert(myHidden.value);

Using this answer: ASP.NET set hiddenfield a value in Javascript 使用此答案: ASP.NET在Javascript中设置hiddenfield一个值

However it isn't returning any value, just an empty alert and console.log. 但是,它不返回任何值,而只是返回一个空警报和console.log。

Could someone point me in the right direction? 有人可以指出我正确的方向吗? This is driving me nuts! 这真让我发疯!

EDIT 编辑

I set this higher up in my code: 我在代码中对此进行了设置:

<asp:HiddenField ID="softwareSelected" ClientIDMode="static" value="test" runat="server" />

By putting the ClientIDMode to static I'm getting back undefined 通过将ClientIDMode设置为静态,我可以返回undefined

Then I try and overwrite this value like this: 然后,我尝试像这样覆盖此值:

var myHidden = document.getElementById('<%= softwareSelected.ClientID %>').value;
console.log(myHidden.value);
 myHidden.value = "tester123";
 console.log(myHidden.value);

But I still get undefined :/ 但是我仍然无法undefined :/

I tested that and worked for me. 我测试了一下并为我工作。 I think your javascript codes are in an external file. 我认为您的JavaScript代码位于外部文件中。 If this is you must know that 如果是这样,您必须知道

<%= softwareSelected.ClientID %>

Only works in you .aspx file. 仅适用于您的.aspx文件。

And if you js scripts are in .aspx file please test this: 如果您的js脚本位于.aspx文件中,请对此进行测试:

var myHidden = document.getElementById('JsContent_ softwareSelected').value; 

If this does not work you must search some where else to you problem. 如果这不起作用,则必须搜索其他问题所在。

ASPX ASPX

<asp:HiddenField ID="softwareSelected" ClientIDMode="static" value="Hello" runat="server" />

JS JS

var myHidden = document.getElementById('softwareSelected');
console.log(myHidden.value);
alert(myHidden.value);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM