admin管理员组文章数量:1426517
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?
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?
Share Improve this question asked Jul 15, 2009 at 17:03 LoganFrederickLoganFrederick 3272 gold badges8 silver badges19 bronze badges3 Answers
Reset to default 4You have to check against the hiddenZone ClientID.
function showtime() {
zone(
document.getElementById('<%=hiddenZone.ClientID%>'),
clock
);
}
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.
You can use your existing code by replacing:
function showtime() {
zone(hiddenZone, 'clock');
}
with
function showtime() {
var elem = document.getElementById('<%= hiddenZone.ClientID %>');
// if you are using ASP.NET AJAX use:
// var elem = $get('<%= hiddenZone.ClientID %>');
zone(elem.value, 'clock');
}
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:
function zone(tz, dispID) {
var elem = document.getElementById(dispID);
// if you are using ASP.NET AJAX use:
// var elem = $get(dispID);
elem.innerHTML = tz;
}
Alternatively, if it were a public property on your page you can remove your reference to the asp:HiddenField and just use:
function showtime() {
zone(<%= MyFormsTimeZoneProperty %>, clock);
}
Put it in server side tags,
function showtime() {
zone('<%= hiddenZone.Value %>',clock);
}
本文标签: Accessing C Variable from JavascriptStack Overflow
版权声明:本文标题:Accessing C# Variable from Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745470094a2659708.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论