继承了母版页的子页面中触发body的onload事件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了继承了母版页的子页面中触发body的onload事件相关的知识,希望对你有一定的参考价值。
碰到有个继承了母版页的页面要加载body的onload事件,我发了一下午来解决这个问题,终于在国外某论坛上找到了解决方案
Method1:
In the master page make the body a control by giving the runat="server" attribute and a name.
<body runat="server" id="MyBody">
Next, use the following code in the Page_Load event of the page (Default.aspx or any other page which inherits from the master page).
htmlGenericControl body = (HtmlGenericControl)
Page.Master.FindControl("MyBody");
body.Attributes.Add("onload", "alert(‘hello world‘)");
And, thats it! Now the body event is only generated for a particular page.
(注:需要添加using System.Web.UI.HtmlControls;)(经验证,有效)
Method2:
Hi. It‘s a good solution! But try also my approach
1. in MasterPage create ContentPlaceHolder for just opening <body> tag:
<asp:contentplaceholder id="BodyPlaceHolder1" runat="server"><body></asp:contentplaceholder>
2. in your .aspx pages create specific content with onload event
<asp:Content ontentPlaceHolderID=BodyPlaceHolder1 runat=server>
<body onload="alert(‘hello‘);">
</asp:Content>
来自http://geekswithblogs.net/AzamSharp/archive/2006/04/25/76390.aspx
以上是关于继承了母版页的子页面中触发body的onload事件的主要内容,如果未能解决你的问题,请参考以下文章