碰到有个继承了母版页的页面要加载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 >
</asp:Content>
来自http://geekswithblogs.net/AzamSharp/archive/2006/04/25/76390.aspx