Simulate ASP. NET forums to implement skin-changing controls

Source: Internet
Author: User
Simulate ASP. NET forums to implement skin-changing controls

To help you understand ASP. net forums2.0 in-depth analysis of ASP. net forums implements code separation and skin change. Now let's write a login page for code separation with skin change function:

Step 1: Create a themedemo Project

Step 2: add the base class skinnedwebcontrol. CS

Using system;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. IO;

Namespace webuc. themedemo. Controls
{

[
Parsechildren (true)
]
/// <Summary>
/// Skin-changing controls
/// </Summary>
Public abstract class skinnedwebcontrol: webcontrol, inamingcontainer
{

String skinfilename = NULL;

Protected override void createchildcontrols ()
{
Control skin;

// Load the user control file
Skin = loadskin ();

// Initialize the control and bind it to the control
Initializeskin (skin );

Controls. Add (skin );
}

/// <Summary>
/// Load the user control file
/// </Summary>
/// <Returns> </returns>
Protected control loadskin ()
{
Control skin;

// The user control file is stored in the themes directory by default.
String skinpath = "themes/" + skinfilename;

// Does the user control file have been defined?
If (skinfilename = NULL)
Throw new exception ("The skinfilename attribute must be defined to specify the file path of the user control ");

// Obtain the usercontrol object from the user control file through the page. loadcontrol (defaultskinpath) Method
Try
{
Skin = page. loadcontrol (skinpath );
}
Catch (filenotfoundexception)
{
Throw new exception ("the user control file is not found! ");
}

Return skin;
}

/// <Summary>
/// Initialize the control and bind the control data
/// </Summary>
/// <Param name = "skin"> </param>
Protected abstract void initializeskin (control skin );

/// <Summary>
/// User control file path
/// </Summary>
Public String skinfilename
{
Get {return skinfilename ;}
Set {skinfilename = value ;}
}

}
}

Step 3: Create the themes directory and create two user control files: Login. ascx and login1.ascx. Different layout styles must contain the following controls:
Textbox Username
Textbox Password
Button loginbutton
Label result
Login. ascx

<P> default skin style on the login page </P>
<P> User name: <asp: textbox id = "username" runat = "server"> </ASP: textbox> </P>
<P> password: <asp: textbox id = "password" runat = "server" textmode = "password"/> </P>
<P> <asp: button id = "loginbutton" runat = "server" text = "login"/> </P>
<P> <asp: Label id = "result" runat = "server"/> </P>

Login1.ascx

<P> skin style 1 on the login page </P>
Username: <asp: textbox id = "username" runat = "server"/>
Password: <asp: textbox id = "password" runat = "server" textmode = "password"/>
<Asp: button id = "loginbutton" runat = "server" text = "login"/> <asp: Label id = "result" runat = "server"/>

Step 4: Create the login control login. CS

Using system;
Using system. Web;
Using system. Web. UI. webcontrols;

Namespace webuc. themedemo. Controls
{
/// <Summary>
/// Login control, inherited from skinnedwebcontrol
/// </Summary>
Public class login: skinnedwebcontrol
{
String skinfilename = "login. ascx"; // specify the default skin Style
Textbox username; // account input box
Textbox password; // password input box
Button loginbutton; // the login button.
Label result; // display the logon result

Public login ()
{
If (skinfilename = NULL)
Skinfilename = skinfilename;
}

/// <Summary>
/// Override initializeskin, initialize the control, and bind the control
/// </Summary>
/// <Param name = "skin"> </param>
Protected override void initializeskin (system. Web. UI. Control skin)
{
// Find the Textbox Control whose ID is username on the ascx page
Username = (textbox) skin. findcontrol ("username ");
// Bind data
Username. Text = "Demo ";

// Find the Textbox Control whose ID is password on the ascx page
Password = (textbox) skin. findcontrol ("password ");
// Bind data
Password. Attributes. Add ("value", "Demo ");

// Initialize the result Control
Result = (Label) skin. findcontrol ("result ");

// Find the login button
Loginbutton = (button) skin. findcontrol ("loginbutton ");
Loginbutton. Click + = new system. eventhandler (loginbutton_click); // bind the Click Event of the login button
}

/// <Summary>
/// Response to the logon button event
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Public void loginbutton_click (Object sender, eventargs E)
{
If (username. Text = "Demo" & password. Text = "Demo ")
Result. Text = "<font color = 'blue'> login successful! ";
Else
Result. Text = "<font color = 'red'> Logon Failed. the username and password do not match! ";
}

}
}

 

Step 5: create two aspx pages and add the two style logon controls respectively.
Login. aspx

<% @ Register tagprefix = "UC" namespace = "webuc. themedemo. controls "assembly =" themedemo "%> <HTML> 

Login1.aspx

<% @ Register tagprefix = "UC" namespace = "webuc. themedemo. controls "assembly =" themedemo "%> <HTML> 

Finally, run them separately to see the effect :)

Http://webuc.net/dotey/archive/2004/05/28/835.aspx

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.