Hi everybody,I faced one interview question in interview that ,

Where should you write the code(for properties like back ground color etc) which applies for every page of your website when application of your website starts? and please give the example also.

Please give the answer.

Thank you,
Aravind

Dani AI

Generated

As noted by , the canonical place for site-wide presentation rules is an external CSS file; for ASP.NET Web Forms the usual pattern is to reference that stylesheet from a single master/layout file so every content page inherits the same look. and were correct to point out master pages (ASP.NET 2.0+) or a common base Page class (ASP.NET 1.1) as the place to centralize that reference.

To make a button change visible on every page, persist the choice (cookie for anonymous users, session or DB for logged-in users) and have the master/base page read that value early in the page lifecycle and apply a theme or swap the stylesheet. Example (WebForms-friendly C#):

/* on button click: set cookie */
HttpCookie c = new HttpCookie("siteTheme", "dark");
c.Expires = DateTime.Now.AddYears(1);
c.HttpOnly = true;
Response.Cookies.Add(c);
Response.Redirect(Request.RawUrl);
/* in Master Page code-behind, Page_PreInit or OnInit: add the CSS chosen */
HttpCookie cookie = Request.Cookies["siteTheme"];
if (cookie != null && !string.IsNullOrEmpty(cookie.Value))
{
    var css = new System.Web.UI.HtmlControls.HtmlLink();
    css.Href = ResolveUrl("~/Content/" + cookie.Value + ".css");
    css.Attributes["rel"] = "stylesheet";
    css.Attributes["type"] = "text/css";
    Page.Header.Controls.Add(css);
}

Alternative: use ASP.NET Themes (App_Themes) and set Page.Theme in PreInit based on the persisted value. For Bootstrap-based sites, prefer toggling a body class or swapping a single Bootstrap theme CSS file rather than rewriting many rules. Important cautions: set sensible cookie lifetimes and flags (HttpOnly, Secure), validate cookie values before use, and remember a Redirect or client-side refresh is usually needed so the master/layout sees the new value on the next render.

Recommended Answers

All 8 Replies

seperate CSS file :) it's much more organized hehe.. give example?

* {
background-color: #000000
}

in something.css

If you're developing in ASP.NET, you'd use the .css file ProgrammersTalk mentioned on a master page, and have all your content pages use the same master, so as to ensure that the style is employed on all of your pages.

Thanks for your valuable answers.

Is there any thing like if click a button something should happen in all of web pages(like change of background color).If Yes Where should I place the code and how?

Please answer.

Thank you,
Aravind.

If it were me, and mind you I'm still quite new to ASP, I'd put it in the master page and store the change in a cookie or session variable. The master page is for material (including layout) that will be rendered on every page, and you just specify parts of it wherein the content will vary by page. Do some research on how to use them. :icon_wink:

HI Infarction,

Could you please just elaborate and give example.

thanks
Aravind

Could you please just elaborate and give example.

No. Look it up yourself.

Arvind you should try using a search engine called google!!

Try this site Download that tool. Start learning from it.

If your application is written in ASP.Net 2.0 , you can create a master page that will be the template for all pages inside the web site.

Or if it is asp.net 1.1, you can create a base class using the PageTemplate design pattern and all other pages will extend the base class to achive common look and feel, etc.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.