ASP.Net LoadComplete活动页面加载之前运行? [英] ASP.Net LoadComplete event running before Page Load?

查看:85
本文介绍了ASP.Net LoadComplete活动页面加载之前运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在加载一个页面来计算当前与UTC时间偏移。这个我可以做的非常简单地通过一个JavaScript函数调用的页面的身体onload事件的一部分,如下所示:

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:

<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> 

然而,当我尝试在code使用这个值落后于在服务器端如下图

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

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 ";
        }
    }

但是偏移量为它的价值现在显示在输入框中确实可用一旦页面完全呈现。所以对我来说,这看起来好象叫作为一个机构onload事件在页面完全加载后才被调用的一部分的JavaScript。

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.

这怎么可能?

推荐答案

你在这里混淆了两个完全不同的负荷的事件。

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

首先,在服务器端code(你的情况ASP.NET)运行的的全部的。在这个循环中,有一对夫妇的事情发生,以及它们之间是加载 LoadComplete 事件。

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 Load and LoadComplete events.

在服务器完成确定的什么应该被渲染的,它开始发送的东西(HTML和JavaScript,通常情况下)的浏览器。在浏览器结束时,将触发的其他 负荷事件 - 即&LT的;身体GT; 在页面上的元素。他们命名相同,但他们是完全独立的。

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, another load event is triggered - that of the <body> element on the page. They're named the same, but they're completely independent.

要解决此问题,设置 lblText.Text 值未设置在服务器端,并改变它在JavaScript中,当你改变偏移指标。

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

服务器端:

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...";
}

客户端:

<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();">

如果你不使用ASP.NET 4,在这里您将得到在你的控件的客户端ID的广泛的控制,你应该看看jQuery的。这是一个你可以使用无限量的事情,这将在这种特定情况下做了很多更容易找到标签控件一个JavaScript库。

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 LoadComplete活动页面加载之前运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆