简体   繁体   中英

Load order / script access with site.master in asp.net

This is largely a basic question on load ordering, but after reading documentation I just wanted to get some input to further confirm this. According to MSDN here the site.master load event is going to fire after the load event of the content pages. The site I'm working on has some methods in the header of the site.master that resize a control on the contents page.

<head id="Head1" runat="server">
...
<asp:ContentPlaceHolder ID="HeaderContent" runat="server"/>
    <script type="text/javascript">
       function resizeView() {
          //some code in here.
       }
    </script>
</head>

On the aspx page there is a point where this function is assigned at the bottom of the page as part of the load event for the aspx.

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"%>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
          $(window).on('load', resizeView);
    </script>
</asp:Content>

So given the the fact the load event happens for the aspx before the site.master, would the resizeView() method be undefined at that point even if it is in the head of the site.master?

Right now that seems to be the case when I run the site.

MasterPage

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MainPage.master.cs" Inherits="MainPageScript" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <title></title>
    <script>
    function MasterPageFunction() {
        alert('I am at masterpage');
    }

</script>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

ContentPage.cs

 protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "howtocallmasterpagescript", "MasterPageFunction();", true);
        }

Result 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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