码迷,mamicode.com
首页 > 其他好文 > 详细

【转】继承了母版页的子页面中触发body的onload事件

时间:2017-04-04 20:42:02      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:from   open   blog   tar   method   system   control   net   font   

碰到有个继承了母版页的页面要加载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事件

标签:from   open   blog   tar   method   system   control   net   font   

原文地址:http://www.cnblogs.com/hellocode2015/p/6665985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!