Several methods of using CSS to implement personalized interface in ASP.net

Source: Internet
Author: User
Asp.net|css

Look at the old cat's article, by making the page dynamically loaded with different CSS to achieve personalized interface (such as personalized blog interface) have the following several methods

Method One:

HtmlGenericControl defines methods, properties, and events for all HTML server control elements that are not represented by a specific. NET Framework class.

<link href= "Login.css" type= "Text/css" rel= "stylesheet" >

Where the href attribute can be dynamically obtained from the user Configuration and then page Setup.

<% @page language= "C #"%>
<% @import namespace= "System.Data"%>
<script language= "C #" runat= "Server" >
public void Page_Load (Object Obj,eventargs E)
{
Create a server-side control.
The specified tag "LINK" Initializes a new instance of this class.
HtmlGenericControl objlink=new HtmlGenericControl ("LINK");
Objlink.id=id;
objlink.attributes["rel"]= "stylesheet";
objlink.attributes["type"]= "Text/css";
objlink.attributes["href"]= "login.css";

Mycss as placeholder container control, placed in

MYCSS.CONTROLS.ADD (Objlink);
}
</script>
<title>c#</title>
<asp:placeholder id= "mycss" runat= "Server" ></asp:placeholder>
<body >
<form runat= "Server" >

</form>
</body>


Method Two:


You can easily set and modify the style of a control by changing the CssClass property of the Web control.
However, in the actual development process, set the control of the CssClass property, very cumbersome, so this idea is not widely used.
But the following code snippet shows a way to change the style of all the same controls on a page at once, with simple skin features.

The code is as follows:
public void Page_Load (Object Obj,eventargs E)
{
if (! Page.IsPostBack) {
Sets 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];

Get the type of the control
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 uniformly modified to "textbox_show".




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.