Say you have a master page and content page scenario, and from the content page you want to interact with DOM elements described from the master page (for example, the main body element). This is how I do it:
First, add the following attributes to your body element so that it is included in the server-side runtime and can be interacted with from there:
<body id="mainbody" runat="Server">
...notice how the body element now has an id and has the runat="server" attribute set.
Second, from code within the content page (I'm doing it in Page_Load) add code like this:
protected void Page_Load(object sender, EventArgs e)
{
HtmlGenericControl ctrl = (HtmlGenericControl)this.Master.FindControl("mainbody");
ctrl.Attributes.Add("onload", "alert('hello');");
return;
}The key here is to now grab the body element, add some script to it, and then let it all run as normal. When I run the solution, the content page is loaded, its Page_Load called, the script injected into the body element (of the master page) and I see "hello".
Recommended reading:


