In ASP. Net, two methods use CSS to implement multiple interfaces, asp. netcss
Multiple Interfaces (csdn-based blogs) are implemented by dynamically loading different CSS pages ):
Method 1:
<% @ Page language = "C #" %>
<% @ Import namespace = "System. Data" %>
<Script language = "c #" runat = "server">
Public void page_load (Object obj, EventArgs e)
{
// Create a server control.
// The specified tag "LINK" initializes a new instance of this type.
HtmlGenericControl objLink = new HtmlGenericControl ("LINK ");
ObjLink. ID = ID;
ObjLink. Attributes ["rel"] = "stylesheet ";
ObjLink. Attributes ["type"] = "text/css ";
ObjLink. Attributes ["href"] = "portal.css ";
// This control does not produce any visible output. It is only used as a container for other controls. You can add, insert, or remove controls in it.
MyCSS. Controls. Add (objLink );
}
</Script>
<Html>
<Head>
<Title> c # </title>
<Asp: placeholder id = "MyCSS" runat = "server"> </asp: placeholder>
</Head>
<Body bgColor = "# ffcc66" style = "FONT: 9pt">
<Form runat = "server">
</Form>
</Body>
</Html>
========================================================== =
You can change the page by dynamically setting the styles of all controls of the same type on the page:
Method 2:
You can modify the CssClass attribute of a WEB Control to conveniently set and modify the style of the control.
However, in actual development, setting the CssClass attribute of controls one by one is very cumbersome, so this idea is not widely used.
However, the following code snippet demonstrates how to change the style of all controls of the same type on the page at one time to implement simple functions such as SKIN.
The Code is as follows:
Public void page_load (Object obj, EventArgs e)
{
If (! Page. IsPostBack ){
// Set the style for all controls on the page.
SetCSS (Page. Controls );
}
}
Private void SetCSS (System. Web. UI. ControlCollection vControls)
{
For (int I = 0; I <vControls. Count; I ++)
{
System. Web. UI. Control vControl = vControls [I];
// Obtain the control type
// You can add control types and corresponding processing methods.
String PType = vControl. GetType (). Name;
Switch (PType)
{
Case "TextBox ":
TextBox_CSS (TextBox) vControl );
Break;
Case "Button ":
// Button_CSS (Button) vControl );
Break;
Case "DataGrid ":
// DataGrid_CSS (DataGrid) vControl );
Break;
}
If (vControl. Controls. Count> 0)
SetCSS (vControl. Controls );
}
}
Private void TextBox_CSS (TextBox tb ){
Tb. CssClass = "TextBox_show ";
}
<Form runat = "server">
<Asp: textbox id = "Search1" runat = "server"/>
<Asp: textbox id = "Search2" CssClass = "INPUT" runat = "server"/>
</Form>
After running, view the page source code. You can find that the style of the text box has been changed to "TextBox_show ".
(End)