Tag Archives: asp.net

Inject onload javascript function for asp.net aspx pages with master page

1. Add reference to HtmlControls on top of your cs file

using System.Web.UI.HtmlControls;

2. In your Page_Load function of the .cs file add

((HtmlGenericControl) this.Page.Master.FindControl(“PageBody”)).Attributes.Add(“onload”, “load()”);

3. Make sure your master page BODY has the the matching id from above. in this case: ID=”PageBody” runat=”server”

4. In your aspx file, add you load function. something like:

document.getElementById in asp.net with Master Pages

Using JavaScript to locate controls in ASP.net pages that are linked to master pages can be tricky.  This is because the ASP.net reassigns control IDs dynamically and the names change.

A simple solution is to use asp.net inline Tags.

For Example:

Instead for using:

document.getElementById(“Label1″)

try using:

document.getElementById(“<%=Label1.ClientID%>”)