开发者

ASP.Net LoadComplete event running before Page Load?

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"&开发者_如何学Pythongt;</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

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.

How is this even possible?


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

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.

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.

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.


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.


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜