How to reference the masterpage body element in server code

R

Roberto Kohler

How do I reference the masterpage body element in ASP.NET 2.0 server code ?

I tried the following, but I get the error "hgcMasterBody is not a member of
System.Web.UI.MasterPage"

In the Master Page:
<body id="hgcMasterBody" runat="server">
.....
</body>

In the ASPX page:
<%@ MasterType virtualpath="MyMasterPage.master" %>
.....
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Master.hgcMasterBody.Attributes("bgcolor") = "lightblue"
End Sub
</script>
 
R

Roberto Kohler

I added a @ MasterType directive to the ASPX page:
<%@ MasterType VirtualPath="~/MasterPage.master" %>

and now I get the error:
Compiler Error Message: BC30390: 'ASP.masterpage_master.Protected Dim
WithEvents hgcMasterBody As System.Web.UI.HtmlControls.HtmlGenericControl'
is not accessible in this context because it is 'Protected'.

Any help would be greatly appreciated.
 
R

Roberto Kohler

Hi Phillip,

Thanks for replying.

Your solution works fine if all the aspx pages that use that master-page
want to use the same event handler.

The problem that I am trying to solve is how to set the body onkeydown event
handler from an aspx file that uses the master-page. That is why I'm trying
to figure out how to reference the masterpage body element from another aspx
file.

For example, I could choose to set the onkeydown event handler to null from
some pages, and to catchKeyPress() from other pages. Another example is, if
for some strange reason I would need to set the body background color to one
color from some aspx pages and to a different color from other aspx pages.

Roberto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top