Home All Groups Group Topic Archive Search About

How to reference the masterpage body element in server code

Author
19 Dec 2005 6:30 PM
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>

Author
19 Dec 2005 7:00 PM
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.

---------------------------------------
Show quoteHide quote
"Roberto Kohler" <rkoh***@intranetslab.com> wrote in message
news:eIeF6pMBGHA.344@TK2MSFTNGP11.phx.gbl...
> 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>
>
>
Author
19 Dec 2005 7:45 PM
Phillip Williams
Hi Roberto,

I have just created a simple master page with the following code in it and
it works just fine:
<body onkeydown="catchKeyPress(window.event.keyCode,
window.event.srcElement);">

Here is the link to the test page:
http://www.webswapp.com/CodeSamples/aspnet20/test1.aspx
Show quoteHide quote
"Roberto Kohler" wrote:

> 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.
>
> ---------------------------------------
> "Roberto Kohler" <rkoh***@intranetslab.com> wrote in message
> news:eIeF6pMBGHA.344@TK2MSFTNGP11.phx.gbl...
> > 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>
> >
> >
>
>
>
Author
19 Dec 2005 8:06 PM
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


Show quoteHide quote
"Phillip Williams" <Phillip.Willi***@webswapp.com> wrote in message
news:EDD21686-8876-4858-A472-63D217A09E88@microsoft.com...
> Hi Roberto,
>
> I have just created a simple master page with the following code in it and
> it works just fine:
> <body onkeydown="catchKeyPress(window.event.keyCode,
> window.event.srcElement);">
>
> Here is the link to the test page:
> http://www.webswapp.com/CodeSamples/aspnet20/test1.aspx
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Roberto Kohler" wrote:
>
>> 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.
>>
>> ---------------------------------------
>> "Roberto Kohler" <rkoh***@intranetslab.com> wrote in message
>> news:eIeF6pMBGHA.344@TK2MSFTNGP11.phx.gbl...
>> > 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>
>> >
>> >
>>
>>
>>
Author
19 Dec 2005 8:15 PM
Phillip Williams
Ok, I got you.  I modified the sample with a new link to another sample to
demonstrate how to access a control on a master page from the content page:
http://www.webswapp.com/CodeSamples/aspnet20/test2.aspx
Show quoteHide quote
"Roberto Kohler" wrote:

> 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
>
>
> "Phillip Williams" <Phillip.Willi***@webswapp.com> wrote in message
> news:EDD21686-8876-4858-A472-63D217A09E88@microsoft.com...
> > Hi Roberto,
> >
> > I have just created a simple master page with the following code in it and
> > it works just fine:
> > <body onkeydown="catchKeyPress(window.event.keyCode,
> > window.event.srcElement);">
> >
> > Here is the link to the test page:
> > http://www.webswapp.com/CodeSamples/aspnet20/test1.aspx
> > --
> > HTH,
> > Phillip Williams
> > http://www.societopia.net
> > http://www.webswapp.com
> >
> >
> > "Roberto Kohler" wrote:
> >
> >> 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.
> >>
> >> ---------------------------------------
> >> "Roberto Kohler" <rkoh***@intranetslab.com> wrote in message
> >> news:eIeF6pMBGHA.344@TK2MSFTNGP11.phx.gbl...
> >> > 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>
> >> >
> >> >
> >>
> >>
> >>
>
>
>
Author
19 Dec 2005 8:31 PM
Roberto Kohler
Phillip,

That's it!
Thank you so very much...

Roberto