Javascript 在页面加载之前运行 ASP.Net LoadComplete 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5666037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:21:45  来源:igfitidea点击:

ASP.Net LoadComplete event running before Page Load?

javascriptasp.net

提问by Johnv2020

I need to calculate the current offset from UTC time when a page is loaded. This I can do very simply via a javascript function call as part of the page's body onload event as shown below:

我需要计算页面加载时与 UTC 时间的当前偏移量。这可以非常简单地通过 javascript 函数调用作为页面主体 onload 事件的一部分来完成,如下所示:

<script type="text/javascript" language="javascript">

    function getOffset() {
        var curUTCDate = new Date();
        var iMins = curUTCDate.getTimezoneOffset();
        return iMins;
    }
</script>

<body id="bodymain" onload="javascript:document.forms[0]['hdnOffset'].value=getOffset();">
<form id="form1" runat="server">

    <input id="hdnOffset" runat="server"/>
    <asp:Label ID="lblText" runat="server"></asp:Label> 

However when I try to use this value in the code behind as part of Page_LoadComplete on the server side the offset value has not been set as shown below

但是,当我尝试在后面的代码中将此值用作服务器端 Page_LoadComplete 的一部分时,偏移值尚未设置,如下所示

protected void Page_LoadComplete(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(hdnOffset.Value))
        {
            lblText.Text = hdnOffset.Value + " value set";
        }
        else
        {
            lblText.Text = " value not set ";
        }
    }

however the offset does become available once the Page has fully rendered as its value is now displayed in the input box. So to me this looks as if the javascript called as part of a body onload event only gets called after the Page has completely loaded.

然而,一旦页面完全呈现,偏移量就会变得可用,因为它的值现在显示在输入框中。所以对我来说,这看起来好像作为主体 onload 事件的一部分调用的 javascript 仅在页面完全加载后才被调用。

How is this even possible?

这怎么可能?

回答by Tomas Aschan

You're confusing two completely different "load" events here.

您在这里混淆了两个完全不同的“加载”事件。

First, the server side code (in your case ASP.NET) runs in its entirety. In this cycle, there are a couple of things that happen, and among them are the Loadand LoadCompleteevents.

首先,服务器端代码(在您的情况下为 ASP.NET)完整运行。在这个循环中,会发生几件事情,其中​​包括LoadLoadComplete事件。

When the server is done determining what should be rendered, it starts sending stuff (HTML and JavaScript, usually) to the browser. On the browser end, anotherloadevent is triggered - that of the <body>element on the page. They're named the same, but they're completely independent.

当服务器完成确定应该呈现什么时,它开始向浏览器发送内容(通常是 HTML 和 JavaScript)。在浏览器端,另一个load事件被触发 -<body>页面上元素的事件。它们的名字相同,但它们是完全独立的。

To fix this problem, set lblText.Textto "value not set"on the server side, and change it in JavaScript when you change the offset indicator.

要解决此问题,lblText.Text"value not set"在服务器端设置为,并在更改偏移指示器时在 JavaScript 中更改它。

Server side:

服务器端:

protected void Page_LoadComplete(object sender, EventArgs e)
{
    // Possibly even better to do this in the properties of the control...
    lblText.Text = "value not set...";
}

Client side:

客户端:

<script type="text/javascript" language="javascript">
    function setOffset() {
        var curUTCDate = new Date();
        var iMins = curUTCDate.getTimezoneOffset();
        document.forms[0]['hdnOffset'].value = iMins;
        document.getElementById('lblText').innerHtml = 'value set';
    }
</script>

<body id="bodymain" onload="javascript:setOffset();">

If you're not using ASP.NET 4, where you are given extensive control over the client-side IDs of your controls, you should take a look at jQuery. It's a javascript library that you can use for an endless amount of things, which will in this specific case make it a lot easier to find the label control.

如果您没有使用 ASP.NET 4,您可以在其中广泛控制控件的客户端 ID,您应该看看 jQuery。它是一个 javascript 库,您可以将其用于无穷无尽的事情,在这种特定情况下,这将使查找标签控件变得更加容易。

回答by Tomas McGuinness

The ASP.Net will have finished processing the page before it returns the HTML to the browser at which point the onload function is called.

ASP.Net 将在将 HTML 返回到浏览器之前完成对页面的处理,此时 onload 函数被调用。

回答by Akram Shahda

Refer to the following link to read about Page life cycle ..

请参阅以下链接以了解页面生命周期..

http://msdn.microsoft.com/en-us/library/ms178472.aspx

http://msdn.microsoft.com/en-us/library/ms178472.aspx