Changing the background color

  • Thread starter Thread starter Glenn Hanna
  • Start date Start date
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
 
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.)
 
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
 
<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>




Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
 
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>




Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
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
 
body is a Generic HTMLControl

Try:

protected GenericHtmlControl Body;




Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
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>




Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
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
 
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
 
re:
Thank you for your help

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



Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
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

Back
Top