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);
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745470094a4629092.html
评论列表(0条)