[C #. Net] Method of C #. NET application topic

Source: Internet
Author: User

1. Global topic: add the <pages stylesheettheme = "themename"/> section to <system. Web> in Web. config. The themename topic is automatically applied throughout the application.

2. Page topic: Add <% @ page Language = "C #" stylesheettheme = "themename" %> to the top of the aspx file. In this way, the themename topic is applied to the current page, or Add the following code page in aspx_cs (page_load method. stylesheettheme = "themename ";

3. Role topic: masterpage is used for implementation. isinrole ("rolename") is used to determine whether a user belongs to a specific role, and then decide the topic (page. stylesheettheme = "themename"); of course, you can use this method to apply the selected topic.

4. Apply a specific style to a control. You need to set the skinid attribute of the control, and then enter the skinid of the style defined in the skin file in skin! If you do not want a control to apply the theme style, you only need to set the enabletheming attribute of the label to false.

5. Theme contains CSS:

Optional

There are two main ways to reference theme and skin: stylesheettheme and theme. Their usage is similar, that is, the execution priority is different:

1. When defining theme on a page: The page only uses theme styles, and does not use User-Defined styles on the page.
2. When stylesheettheme is defined, the page first loads the style in the stylesheettheme attribute, and then merges the style on the page. If there is the same style definition, the style in the page is taken.
The three stylesheettheme styles are loaded first, then the styles on the page are loaded, and then the styles in theme are loaded. If there are the same styles, the subsequent styles overwrite the previously loaded styles.

Working Principle

The differences between different themes and skins are mainly the style (including font, paragraph, background, border, and so on) of the page and some images.

Traditional solutions that fully use CSS to express different themes tend to have insufficient strength in processing images and image buttons. In aspnet2.0, everything has become simple.

The topic and skin are supported by the built-in DOTNET framework2.0, The skinid attribute is added to the server control, and the theme and stylesheettheme attributes are added to the page class. The purpose is to elegantly support skin. After a topic is specified for an application, the relevant pages will automatically link CSS files and skin files located in the topic directory. CSS usage is no different from traditional usage, while the skin file works in a way similar to CSS, the server control with skinid specified automatically loads and attaches matching attributes or styles from the skin file (the most common is the imageurl attribute of image and imagebutton, in this way, the page can be in different themes) --- this is done on the server side. Because skin files are cached in the memory after they are used, the efficiency will not be affected.

Background code easily changes the website

All of the above are in the aspx file or web. apply theme in config, and it is obviously inconvenient to use the above method to implement skin in different skin websites for every user such as blog.

The following describes how to dynamically reference theme in the background code to solve the above problem. theme must be applied at the earliest stage of page request, therefore, we must write code in the page_preinit event. The code is very simple, just one sentence:

Page. theme = "...";

Here, we only need to read different theme names set by each user from the database, so that each user can have different skin easily.

The above can be considered as a waste of time. Next we will start a new production method.

Below is an example written by myself:
First, create an app_themes folder.
Then create two theme blue. Red is actually two folders.
Then we can create a different. skinfile. CSS style under different themes.
Here I have defined only one skin file, specifically how to define it for everyone! As long as you don't define the same theme, okay!

Now we can implement the background control topic.
1. In the aspx File
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "template. aspx. cs" inherits = "admin_template" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en ""Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns ="
Http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> template management </title>
<Script language = "JavaScript" type = "text/JavaScript">
<! --
Function clickit (){
VaR dom = Document. All;
VaR El = event. srcelement;
If (El. tagname = "input" & El. type. tolowercase () = "radio ")
{
For (I = 0; I <Dom. length; I ++)
{
If (DOM [I]. tagname = "input" & Dom [I]. type. tolowercase () = "radio ")
{
Dom [I]. Checked = false;
}
}
}
El. Checked = true;
}

// -->
</SCRIPT>
</Head>
<Body>
<Form ID = "f1" runat = "server">
<Div class = "inner2">
<Asp: repeater id = "repeater1" runat = "server">
<Itemtemplate>
<Ul> <li>
<br/>
<Asp: Label id = "label1" runat = "server" text = '<% # databinder. eval (container, "dataitem. templatename ") %> '> </ASP: Label> <br/>
<Asp: Label id = "label2" runat = "server" text = '<% # databinder. eval (container, "dataitem. setfolder ") %> 'visible =" false "> </ASP: Label>
<Asp: Label id = "label3" runat = "server" text = '<% # databinder. eval (container, "dataitem. cssname ") %> 'visible =" false "> </ASP: Label>
<Asp: radiobutton id = "radiobutton1" runat = "server" onclick = "clickit ();"/>
</LI> </ul>
</Itemtemplate>
</ASP: repeater>
<Div style = "text-align: center;"> <asp: button id = "savebutton1" runat = "server" text = "selected, OK! "Onclick =" savebutton1_click "/> </div> <br/>
</Div> <br/>
</Form>
</Body>
</Html>
2. In the Aspx. CS file:
// Change the website subject
Using system. Data. sqlclient;
Public partial class admin_template: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

If (session ["usernm"]! = NULL)
{
If (session ["usernm"]! = "")
{

}
Else
{
Response. Write ("<SCRIPT> alert ('Operation failed! No login or login timeout '); location. href = 'adminlogin. aspx'; </SCRIPT> ");
}
}
Else
{
Response. Write ("<SCRIPT> alert ('Operation failed! No login or login timeout '); location. href = 'adminlogin. aspx'; </SCRIPT> ");
}
If (! Ispostback)
{
Sqlconnection con = new sqlconnection ("database connection statement, I will not write ");
String sql1 = "select top 5 * from [TEMPLATE] Order by [templateid] DESC ";
Sqldataadapter da1 = new sqldataadapter (sql1, con );
Dataset ds1 = new dataset ();
Da1.fill (ds1 );
Repeater1.datasource = ds1;
Repeater1.databind ();
}
}
Protected void savebutton1_click (Object sender, eventargs E)
{
Sqlconnection con = new sqlconnection ("database connection statement, I will not write ");
Con. open ();
Int Inti = 0;
Try
{

For (INTI = 0; Inti <this. repeater1.items. Count; Inti ++)
{
Radiobutton itemcheck = (radiobutton) This. repeater1.items [Inti]. findcontrol ("radiobutton1 ");
If (itemcheck. Checked)
{
Label label2 = (Label) This. repeater1.items [Inti]. findcontrol ("label2 ");
Label label3 = (Label) This. repeater1.items [Inti]. findcontrol ("label3 ");
String SQL = "update user set template = '" + label2.text. tostring () + "', selectcss ='" + label3.text. tostring () + "'where id =" + '2'; (Note !, For multiple users, change here to where userid = "+ this. session [" userid "]. tostring ();) sqlcommand cmd = new sqlcommand (SQL, con );
Cmd. executenonquery ();
}
}
}
Catch (exception ex)
{
Throw new exception (ex. Message );
}
Response. Write ("<SCRIPT> alert ('Operation successful! '); Location. href = 'template. aspx'; </SCRIPT> ");
Con. Close ();
}
}

It seems we have to perform a test!

3. Write the following method in the. aspx. CS file of all pages on which the topic needs to be applied.
// Achieve skin replacement effect
Protected void page_preinit (Object sender, eventargs E)
{
Sqlconnection con1 = new sqlconnection ("database connection statement, I will not write ");
Con1.open ();
String sql2 = "select template from [user] Where [ID] =" + '2'; (Note !, For multiple users, change here to where userid = "+ this. session [" userid "]. tostring ();)
Sqldataadapter da = new sqldataadapter (sql2, con1 );
Dataset DS = new dataset ();
Da. Fill (DS );
Datatable dt = Ds. Tables [0];
If (Ds. Tables [0]. Rows. Count> 0)
{
Page. theme = Ds. Tables [0]. Rows [0] [0]. tostring ();
}
Else
{
Page. theme = "default ";
}
}

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.