In the code behind: how to add a background attribute to the body tag.

W

_Who

In the .master file I have:
<body runat="server" id="MainBody">

I need to add a background attribute to MainBody in the .master.vb file.

But there it tells me that MainBody is not declared.

Now I'll try anything to see if I can get a hint of how to proceed. So I
try:

Dim zz As Control = Master.FindControl("MainBody")

But Master's value is nothing



I try

Dim zz As Control = FindControl("MainBody")

Seems to find it but Control does not have a background attribute to set.



So now I'm trying this NG.

Any suggestions as to how I can add that attribute???



Thanks

ps I know background is deprecated
 
L

Louis Somers

Assuming that "MainBody" is the ID of a ContentPlaceHolder on your masterpage

ContentPlaceHolders do not have any backround attribute. You should nest it
inside a DIV or so and alter that attribute instead.
 
U

Usenet User

In the .master file I have:
<body runat="server" id="MainBody">

I need to add a background attribute to MainBody in the .master.vb file.

But there it tells me that MainBody is not declared.

Now I'll try anything to see if I can get a hint of how to proceed. So I
try:

Dim zz As Control = Master.FindControl("MainBody")

But Master's value is nothing



I try

Dim zz As Control = FindControl("MainBody")

Seems to find it but Control does not have a background attribute to set.



So now I'm trying this NG.

Any suggestions as to how I can add that attribute???



Thanks

ps I know background is deprecated

Here are some solutions (sorry, I am using C#, not VB.NET):

1) Put the following into your Page_Load or Page_PreRender
(preferably) event handler method:

HtmlGenericControl body = Me.Page.FindControl( "MainBody" ) as
HtmlGenericControl;
body.Attributes["bgcolor"] = "#ffffcc";

2) To avoid looking up control by its name, declare it is explicitly
on your code-behind file by adding a protected page member variable
that has the same ID and correct object type:

protected HtmlGenericControl MainBody;

.....
and then in your event handler:

this.MainBody.Attributes["bgcolor"] = "#ccffff";


Keep in mind that you can add any custom attribute to any HTML control
this way. Also, the safer method maybe like follows, although I am not
sure why the above works as well:

this.MainBody.Attributes.Add( "myAttrib", "myValue" );

rather than

this.MainBody.Attributes["myAttrib"] = "myValue";

N-joy!
 
W

_Who

Thanks that fixed it


Usenet User said:
In the .master file I have:
<body runat="server" id="MainBody">

I need to add a background attribute to MainBody in the .master.vb file.

But there it tells me that MainBody is not declared.

Now I'll try anything to see if I can get a hint of how to proceed. So I
try:

Dim zz As Control = Master.FindControl("MainBody")

But Master's value is nothing



I try

Dim zz As Control = FindControl("MainBody")

Seems to find it but Control does not have a background attribute to set.



So now I'm trying this NG.

Any suggestions as to how I can add that attribute???



Thanks

ps I know background is deprecated

Here are some solutions (sorry, I am using C#, not VB.NET):

1) Put the following into your Page_Load or Page_PreRender
(preferably) event handler method:

HtmlGenericControl body = Me.Page.FindControl( "MainBody" ) as
HtmlGenericControl;
body.Attributes["bgcolor"] = "#ffffcc";

2) To avoid looking up control by its name, declare it is explicitly
on your code-behind file by adding a protected page member variable
that has the same ID and correct object type:

protected HtmlGenericControl MainBody;

....
and then in your event handler:

this.MainBody.Attributes["bgcolor"] = "#ccffff";


Keep in mind that you can add any custom attribute to any HTML control
this way. Also, the safer method maybe like follows, although I am not
sure why the above works as well:

this.MainBody.Attributes.Add( "myAttrib", "myValue" );

rather than

this.MainBody.Attributes["myAttrib"] = "myValue";

N-joy!
 

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,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top