简体   繁体   English

从JavaScript访问C#变量?

[英]Accessing C# Variable from Javascript?

In my MS SQL table, I read in an "time_zone" for a city from a record. 在我的MS SQL表中,我从记录中读取城市的“ time_zone”。

I then want to use this time zone in a Javascript function to create a digital clock for that city. 然后,我想在Javascript函数中使用该时区为该城市创建一个数字时钟。

Currently I am trying to set the time_zone variable from 目前,我正在尝试从设置time_zone变量

Here's a code snippet from Default.aspx: 这是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: 我在Default.aspx.cs中拥有的唯一C#是:

hiddenZone.Value=timeZone;

I've checked and timeZone has the correct value read in from the database. 我已经检查过并且timeZone具有从数据库中读取的正确值。

The error message I receive from the JS in the "showtime" function is: "hiddenZone is undefined" 我在“ showtime”函数中从JS收到的错误消息是:“ hiddenZone未定义”

How can I get the "timeZone" C# variable into my Javascript and use it for the function? 如何将“ timeZone” C#变量放入Javascript并将其用于函数?

You have to check against the hiddenZone ClientID. 您必须对照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. ASP.NET会为其在HTML页面上创建的对象创建一个新名称,因此它们将与您指定的ID不同。

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: 另外,如果它是页面上的公共属性,则可以删除对asp:HiddenField的引用,而只需使用:

function showtime() {
    zone(<%= MyFormsTimeZoneProperty %>, clock);
}

Put it in server side tags, 将其放在服务器端标签中,

        function showtime() {
             zone('<%= hiddenZone.Value %>',clock);
        }

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

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