首页 > 其他 > 详细

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

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!