Changing the background color

G

Glenn Hanna

Hi,

Can someone tell me how to change the background color of a web page though
C#? I've tried various things that I have found on the web written in VB
but I haven't had much luck with them.

Thank you,
Glenn
 
S

Steven Cheng[MSFT]

Hi Glenn,

Thank you for posting.

As for the programmatically change ASP.NET webform page's background color
question, I think we can mark the <body> element in the form as
runat="server", then we can use code to manipulate the <body> element's
style attributes in page's code behind. Also, are you using ASP.NET 1.1 or
2.0, for asp.net 1.1 since it is not using the partial class to compile
aspx and code behind, we still need to manually declare the control member
in the codebehind page class(if we mark the <body> as runat="server").
Below is the code we need to modify in ASP.NET 2.0 page to programmatically
change the page's background:

for ASP.NET 2.0
=========in aspx ========
<body id="mybody" runat="server">

=======in codebehind=====
protected void Page_Load(object sender, EventArgs e)
{
mybody.Style[HtmlTextWriterStyle.BackgroundColor] = "#787878";
}

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Glenn Hanna

Hi Steve,

I'm still on ASP 1.1, I have 2.0 here but I am concentrating on getting this
project done for the end of May before I convert over. Can you post how to
do this with 1.1?

Thank you,
Glenn
 
J

Juan T. Llibre

<html>
<head>
<script language="C#" runat="server">
void SubmitBtn_Click(object Source, EventArgs e) {
Body.Attributes["bgcolor"] = ColorSelect.Value;
}
</script>
</head>

<body id=Body runat=server>

<h3><font face="Verdana">HtmlGenericControl Sample</font></h3>
<form runat=server>
<p>
Select a background color for the page: <p>
<select id="ColorSelect" runat="server">
<option>White</option>
<option>Wheat</option>
<option>Gainsboro</option>
<option>LemonChiffon</option>
</select>
<input type="submit" runat="server" Value="Apply" OnServerClick="SubmitBtn_Click">
</form>
</body>
</html>
 
G

Glenn Hanna

What I think I need is how to manually declare the control member. I tried
doing this:

protected System.Web.UI.WebControls.Style body;

But I guess its not a "Style" because it didn't work. I'm not sure what to
call it.

Thank you,
Glenn


Juan T. Llibre said:
<html>
<head>
<script language="C#" runat="server">
void SubmitBtn_Click(object Source, EventArgs e) {
Body.Attributes["bgcolor"] = ColorSelect.Value;
}
</script>
</head>

<body id=Body runat=server>

<h3><font face="Verdana">HtmlGenericControl Sample</font></h3>
<form runat=server>
<p>
Select a background color for the page: <p>
<select id="ColorSelect" runat="server">
<option>White</option>
<option>Wheat</option>
<option>Gainsboro</option>
<option>LemonChiffon</option>
</select>
<input type="submit" runat="server" Value="Apply" OnServerClick="SubmitBtn_Click">
</form>
</body>
</html>





Glenn Hanna said:
Hi,

Can someone tell me how to change the background color of a web page though
C#? I've tried various things that I have found on the web written in VB
but I haven't had much luck with them.

Thank you,
Glenn
 
J

Juan T. Llibre

body is a Generic HTMLControl

Try:

protected GenericHtmlControl Body;





Glenn Hanna said:
What I think I need is how to manually declare the control member. I tried
doing this:

protected System.Web.UI.WebControls.Style body;

But I guess its not a "Style" because it didn't work. I'm not sure what to
call it.

Thank you,
Glenn


Juan T. Llibre said:
<html>
<head>
<script language="C#" runat="server">
void SubmitBtn_Click(object Source, EventArgs e) {
Body.Attributes["bgcolor"] = ColorSelect.Value;
}
</script>
</head>

<body id=Body runat=server>

<h3><font face="Verdana">HtmlGenericControl Sample</font></h3>
<form runat=server>
<p>
Select a background color for the page: <p>
<select id="ColorSelect" runat="server">
<option>White</option>
<option>Wheat</option>
<option>Gainsboro</option>
<option>LemonChiffon</option>
</select>
<input type="submit" runat="server" Value="Apply" OnServerClick="SubmitBtn_Click">
</form>
</body>
</html>





Glenn Hanna said:
Hi,

Can someone tell me how to change the background color of a web page though
C#? I've tried various things that I have found on the web written in VB
but I haven't had much luck with them.

Thank you,
Glenn
 
G

Glenn Hanna

I got it to work like this:

ASP Code:
<body runat="server" id="body">

Code Behind:
protected System.Web.UI.HtmlControls.HtmlGenericControl body;
...
string strColor =
System.Configuration.ConfigurationSettings.AppSettings["bgColor"];
body.Attributes.Add("bgColor", strColor);

Thank you for your help,
Glenn
 
J

Juan T. Llibre

re:
Thank you for your help

You're more than welcome, Glenn.
I'm glad you're up and running again.




Glenn Hanna said:
I got it to work like this:

ASP Code:
<body runat="server" id="body">

Code Behind:
protected System.Web.UI.HtmlControls.HtmlGenericControl body;
...
string strColor =
System.Configuration.ConfigurationSettings.AppSettings["bgColor"];
body.Attributes.Add("bgColor", strColor);

Thank you for your help,
Glenn

Glenn Hanna said:
Hi,

Can someone tell me how to change the background color of a web page though
C#? I've tried various things that I have found on the web written in VB
but I haven't had much luck with them.

Thank you,
Glenn
 

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,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top