当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Attribute.Add on MasterPage <body> tag

问题描述:

I'm attempting to put some javascript into the body tag of my masterpage using the following techinique:

<body id="pgBody" runat="server">

//Property to get the body tage in MasterPage.cs

public HtmlGenericControl BodyTag

{

get

{

return this.pgBody;

}

}

//In my content page PageLoad()

MasterPage mstrPg = this.Master as MasterPage;

if (mstrPg != null)

{

mstrPg.BodyTag.Attributes.Add("onload", "LoadMap()");

mstrPg.BodyTag.Attributes.Add("onunload", "UnloadMap()");

}

Everything works as expected when running on my local machine (using the Visual Studio Development Server), but when I deploy it out to a production IIS 6 server the attributes do not get added to the body tag when running the page.

Any ideas on what would cause this?

网友答案:

dont know exactly why your code is not working. and one thing I would recommend using window.onload event instead of body.onload.

just try following:

In the Masterpage markup file, put this before closing body tag

<asp:PlaceHolder runat="server" ID="ScriptsPlaceHolder" Visible="false">
    <script type="text/javascript">
        window.onload = function () { LoadMap(); };
        window.onunload = function () { UnloadMap(); };
    </script>
</asp:PlaceHolder>

Masterpage codebehind file

public PlaceHolder ScriptsPlaceHolderControl
{
    get { return this.ScriptsPlaceHolder; }
}

In your content page

SiteMaster mstrPg = this.Master as SiteMaster;
if (mstrPg != null)
{
    mstrPg.ScriptsPlaceHolderControl.Visible = true;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: