Accessing C# Variable from Javascript? - Stack Overflow

In my MS SQL table, I read in an "time_zone" for a city from a record.I then want to use this

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 badges
Add a ment  | 

3 Answers 3

Reset to default 4

You 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

相关推荐

  • Accessing C# Variable from Javascript? - Stack Overflow

    In my MS SQL table, I read in an "time_zone" for a city from a record.I then want to use this

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信