从 Javascript 访问 C# 变量?

发布于 2024-07-27 00:05:48 字数 891 浏览 0 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兮子 2024-08-03 00:05:48

您必须检查hiddenZone ClientID。

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

ASP.NET 为其在 HTML 页面上创建的对象创建一个新名称,因此它们不会与您指定的 id 相同。

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.

淡写薰衣草的香 2024-08-03 00:05:48

您可以通过替换: 来使用现有代码,

function showtime() {
    zone(hiddenZone, 'clock');
}

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');
}

也不确定您的“区域”函数的作用。 但是,如果您也遇到困难,这样的事情可能会为您指明正确的方向:

function zone(tz, dispID) {
    var elem = document.getElementById(dispID);
    // if you are using ASP.NET AJAX use:
    // var elem = $get(dispID);

    elem.innerHTML = tz;
}

或者,如果它是页面上的公共属性,您可以删除对 asp:HiddenField 的引用,然后使用:

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

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);
}
我一向站在原地 2024-08-03 00:05:48

将其放在服务器端标签中,

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

Put it in server side tags,

        function showtime() {
             zone('<%= hiddenZone.Value %>',clock);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文