[Original] using CSS for page skin replacement

Source: Internet
Author: User
It is often seen that some websites use dropdownlist, menus, or buttons to change the page style (skin replacement) of the entire website ). Specifically, Google summarized some information in this regard and summarized the above functions can be roughly divided into two categories:
1. The entire website uses a CSS file to ensure a style. You can use CSS files of different styles for skin replacement. Implementation in this way
It is relatively simple.
2. usecontrol with the same background code (business logic and functions) for different styles and la S ). Implement through redirection page
Page skin replacement. This method is complicated and will be discussed later.

First look at the first method: Switch the CSS file skin replacement.
1. If you need to specify the referenced CSS file on a normal page, you must use the <link> Control in

<Head>

<LINK rel = stylesheet type = text/CSS href?mycss.css/>
</Head>

If you need to switch the CSS, you can no longer fix this <link>. You can use the background code to dynamically change the properties of this control.
You can use the <asp: placeholder> control instead. The placeholder control can be understood as a control container, just like a transparent package.
Now the page looks like this. We put an empty placeholder in it to replace link.
<Asp: placeholder id = "mycss" runat = "server"/>
</Head>

Define a function in the background code to install things in placeholder. Here, the CSS file public void changeskin (string cssfile) is installed)
{
// Generate a new htmlgenericcontrol control, which is a Link Control
Htmlgenericcontrol objlink = new htmlgenericcontrol ("Link ");
// Define the attributes of the link
Objlink. ID = ID;
Objlink. attributes ["rel"] = "stylesheet ";
Objlink. attributes ["type"] = "text/CSS ";
Objlink. attributes ["href"] = cssfile;
// Add the Link Control to the placeholder Control
Mycss. Controls. Add (objlink );
}

Here, you can put two buttons on the page and press the button to switch between different CSS files.

2. Some websites can save the selected style and keep the selected style for the next visit.
For this type, you can use the cookie of the client to retain the selected style.

 

Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
If (! Page. ispostback)
{
// Determine whether the client browser supports cookies
If (request. browser. Cookies = false)
Label1.text = "the browser does not support cookies! ";
Else
Label1.text = "the browser supports cookies! ";
// Whether skin information is saved on the client
If (request. Cookies ["skin"] = NULL)
{
Changeskin ("default.css ");
Label2.text = "cookie is empty, default CSS ";
}
Else
{
// Load different CSS style sheet files based on the cookie information retained by the client
Switch (request. Cookies ["skin"]. value)
{
Case "red": changeskin ("mycss2.css ");
Label2.text = "CSS in cookie is red ";
Break;
Case "blue": changeskin ("mycss.css ");
Label2.text = "CSS in cookie is blue ";
Break;
Default: changeskin ("default.css ");
// Break;
}
}


}


When you press the button to select a style, you need to keep the information in the cookie.

Public void setcookie (string style)
{

Httpcookie mycookie = new httpcookie ("skin ");
Mycookie. value = style;
Response. Cookies. Add (mycookie );

}

 

Public void btclick (Object SRC, eventargs E)
{
Setcookie ("red ");

Changeskin ("mycss2.css ");
Label2.text = "set to red now ";

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.