从 Javascript 访问 C# 变量?
在我的 MS SQL 表中,我从记录中读取了城市的“time_zone”。
然后我想在 Javascript 函数中使用这个时区为该城市创建一个数字时钟。
目前,我正在尝试设置 time_zone 变量,
这是来自 Default.aspx 的代码片段:
function showtime() {
zone(hiddenZone,clock);
}
window.onload = showtime;
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<span id="clock"></span>
<asp:HiddenField ID="hiddenZone" runat="server" />
<asp:Label Text="" ID="lblXml" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
我在 Default.aspx.cs 中唯一的 C# 是:
hiddenZone.Value=timeZone;
我已经检查过,timeZone 具有从数据库读取的正确值。
我在“showtime”函数中从 JS 收到的错误消息是:“hiddenZone 未定义”
如何将“timeZone”C# 变量放入我的 Javascript 中并将其用于该函数?
In my MS SQL table, I read in an "time_zone" for a city from a record.
I then want to use this time zone in a Javascript function to create a digital clock for that city.
Currently I am trying to set the time_zone variable from
Here's a code snippet from Default.aspx:
function showtime() {
zone(hiddenZone,clock);
}
window.onload = showtime;
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<span id="clock"></span>
<asp:HiddenField ID="hiddenZone" runat="server" />
<asp:Label Text="" ID="lblXml" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
The only C# I have in my Default.aspx.cs for is:
hiddenZone.Value=timeZone;
I've checked and timeZone has the correct value read in from the database.
The error message I receive from the JS in the "showtime" function is: "hiddenZone is undefined"
How can I get the "timeZone" C# variable into my Javascript and use it for the function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须检查hiddenZone ClientID。
ASP.NET 为其在 HTML 页面上创建的对象创建一个新名称,因此它们不会与您指定的 id 相同。
You have to check against the hiddenZone ClientID.
ASP.NET creates a new names for objects it creates on the HTML page, so they won't be the same as what you specify the id to be.
您可以通过替换: 来使用现有代码,
我
也不确定您的“区域”函数的作用。 但是,如果您也遇到困难,这样的事情可能会为您指明正确的方向:
或者,如果它是页面上的公共属性,您可以删除对 asp:HiddenField 的引用,然后使用:
You can use your existing code by replacing:
with
I'm not sure what your 'zone' function does either. But something like this may point you in the right direction if you are also having difficulties with it as well:
Alternatively, if it were a public property on your page you can remove your reference to the asp:HiddenField and just use:
将其放在服务器端标签中,
Put it in server side tags,