Skip to content
March 19, 2008 / kiranpatils

Changing bgcolor of Page/Master Page dynamically+ASP.NET

Hi, one of my colleague was struggling that how to set  value of Page’s background color in ASP.NET Code??

Problem:

Actually we want to set a page’s background color on Page_Load. and value changes everytime..how can i achieve it?

Solution: 

Nothing magic in it just two lines will do the same:

HtmlGenericControl bodytag = (HtmlGenericControl)Page.Master.FindControl(“body”);
bodytag.Style.Add(“background-color”,”#000999″);

for it my Master Page has body tag declared like this:

 <body ID=”body” runat=”server”/>

That’s it..

One Comment

Leave a Comment
  1. Praveem / Jul 25 2012 2:49 pm

    The event should be Page_LoadComplete Event when one want to change the color of master page from content page

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: