On Tue, 23 Sep 2008 10:13:56 -0400, "_Who"
<Ca**********@roadrunner.comwrote:
>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!