In ASP. Net, two methods use CSS to implement multiple interfaces, asp. netcss

Source: Internet
Author: User

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)

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.