ASP. NET forums2.0 in-depth analysis: How ASP. NET forums achieves code separation and skin replacement

Source: Internet
Author: User
Develop a web project in Visual Studio. A web form page consists of two parts: visual elements (HTML, server controls, and static text) and the programming logic of the page. These two components are generally stored in a separate file. Visual elements are created in A. aspx file, while Code Located in a separate class file (. aspx. VB or. aspx. CS ). Or sometimes visual elements and code are created in the same file.

The familiar. aspx. CS file is not found in the web form page of ASP. NET forums, and any C # code is not found. Instead, it is a control. Where is the code ?!

The following uses login. aspx as an example to describe how ASP. NET forums achieves code separation and skin replacement:
First, let's take a look at the Running Effect of login. aspx in two skin styles.
(Theme: Default) (Theme: electricmidnight)

I just changed the default skin of ASP. NET forums, which is also login. aspx and displays two different skin styles. Let's look back at. net, regardless of the skin function, if we want to implement a login page, then we will enter the textbox of the account password and the login button in the aspx or ascx page, double-click the button in the editing area and write the code for processing the button click event. How convenient is it, most of the Code is represented by. net.

Let's take a look at the source code of login. aspx:

<% @ Import namespace = "aspnetforums. Components" %>
<% @ Register tagprefix = "Forums" namespace = "aspnetforums. Controls" assembly = "aspnetforums. Controls" %>
<% @ Register tagprefix = "MP" namespace = "metabuilders. webcontrols. masterpages" assembly = "metabuilders. webcontrols. masterpages" %>

<MP: contentcontainer runat = "server" id = "mpcontainer" masterpagefile = "~ /Themes/masterpage. ascx ">
<MP: Content ID = "maincontent" runat = "server">
<P align = "center">
<Forums: navigationmenu displaytitle = "true" id = "navigationmenu1" runat = "server"/>
<Br/>
<Br/>
<Br/>
<Forums: Login runat = "server" id = "postview1"/>
</P>
</MP: content>
</MP: contentcontainer>
Note: <MP: ***> is a third-party control designed to ensure interface consistency and extract duplicate code between pages.

From the source code, we do not see any basic elements such as textbox and button that make up the login. ASPX page effect. A line of C # code is not even found, but if you are familiar with the page control, it is not difficult to find the original ASP. net forums encapsulates the login interface as a control (the page control is not specifically introduced here, if you are still unfamiliar with the control-related knowledge, it is strongly recommended that you read the relevant information or books ). In the <forums: Login runat = "server" id = "postview1"/> control
<% @ Register tagprefix = "Forums" namespace = "aspnetforums. Controls" assembly = "aspnetforums. Controls" %>
We can know that the class corresponding to the login control should be: aspnetforums. Controls. login. In vs. net, switch to the Class View, find aspnetforums. Controls. login and go to the corresponding file:

(This figure shows you how to quickly find the file corresponding to the control)

The control shown in the Code is inherited from the skinnedforumwebcontrol class:

Public class login: skinnedforumwebcontrol {// inherits from the skinnedforumwebcontrol base class
......
}

Let's take a look at the base class skinnedforumwebcontrol.
Using system;
Using system. drawing;
Using system. collections;
Using system. Collections. Specialized;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using aspnetforums;
Using aspnetforums. components;
Using system. componentmodel;
Using system. IO;
Using system. Web. Security;
Using aspnetforums. enumerations;

Namespace aspnetforums. Controls {

[
Parsechildren (true)
]
/// <Summary>
/// The base classes of almost all controls in ASP. NET forums inherit from webcontrol and implement the inamingcontainer interface.
/// </Summary>
Public abstract class skinnedforumwebcontrol: webcontrol, inamingcontainer {

Forumcontext = forumcontext. Current;
String skinfilename = NULL;
String skinname = NULL;
String returnurl = NULL;
Forummode mode = forummode. user;

Public skinnedforumwebcontrol (){

// Skin used-if the user is anonymous, use the default system Style
//
If (forumcontext. User. isanonymous ){
Skinname = globals. skin;
}
Else {
Skinname = forumcontext. User. theme;
}

}

/// <Summary>
/// You must override this method when developing a composite server control or template Server Control.
/// Notifications use the server controls based on the synthesis to create any child controls they contain to prepare for sending back or rendering.
/// </Summary>
Protected override void createchildcontrols (){
Control skin;

// Load User Controls
Skin = loadskin ();

// Initialize the control
Initializeskin (skin );

Controls. Add (skin );
}

/// <Summary>
/// Use skinname and skinfilename to locate the path of the user control file and load the control object after the user control
/// </Summary>
/// <Returns> </returns>
Protected control loadskin (){
Control skin;
// Location of the User Control File
String skinpath = globals. getskinpath () + "/skins/" + skinfilename. trimstart ('/');
String defaultskinpath = globals. applicationpath + "/themes/default/skins/" + skinfilename. trimstart ('/');

// The skinfilename attribute must exist.
If (skinfilename = NULL)
Throw new exception ("You must specify a skin .");

// Obtain the usercontrol object from the user control file.
Try {
Skin = page. loadcontrol (skinpath );
}
Catch (filenotfoundexception ){

// If the user control file of the specified skin is not found, load the control file under the default skin
Try {
Skin = page. loadcontrol (defaultskinpath );
}
Catch (filenotfoundexception ){
Throw new exception ("critical error: the skinfile" + skinpath + "cocould not be found. The skin must exist for this control to render .");
}
}
Return skin;
}

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

/// <Summary>
/// Path of the user control file (*. ascx)
/// </Summary>
Public String skinfilename {
Get {
Return skinfilename;
}
Set {
Skinfilename = value;
}
}

/// <Summary>
/// Skin name
/// </Summary>
Protected string skinname {
Get {
Return skinname;
}
Set {
Skinname = value;
}
}

Public forummode mode {
Get {return mode ;}
Set {mode = value ;}
}

}
}

The Code shows that the base class skinnedforumwebcontrol inherits from the webcontrol class and implements the inamingcontainer interface. Since it is a custom control, it is naturally inherited from the webcontrol class. The inamingcontainer interface is implemented because the interface should be implemented when templated controls are developed to avoid name conflicts on the same page.
Skinnedforumwebcontrol has two important attributes: skinname and skinfilename, which respectively indicate the skin name and the file path of the user control. In ASP. in net forums2.0, there is a themes directory under the web directory. Each skin corresponds to a directory, such as default and javasicmidnight. Each skin folder has three folders: image, skins, and style, they are used to store the corresponding image files and user control files under the skin respectively (*. and style sheet CSS files. With these two attributes, we can know the user control file (*. ascx) real path, for example, if our skinname is default and skinfilename is skin-login.ascx, then the path of the user control is themes/default/skins/skin-login.ascx, similarly, if we change the skin style to javasicmidnight, the path to the user control will be themes/electricmidnight/skins/skin-login.ascx.

There are also two important methods: loadskin (). In this method, first find the path of the user control file based on the two attributes described above, and then use the page. loadcontrol (defaskskinpath) method to obtain the usercontrol object from the user control file. This is why the page style varies with the skin, because the user control file we load varies with the skin, as long as we set different user control file styles, the display style varies with the skin.

However, we still need to identify the page controls on the ascx page. For example, in the skin-login.ascx, we must know which input box is the account, the input box is the password, know whether the user clicked the login button. In vs. net, IDE automatically identifies these controls by ID. Double-click the buttons in IDE to add code that responds to click events. But what should we do now ?......

The base class also has an abstract initializeskin (control skin) method. All controls inherited from skinnedforumwebcontrol must override this method, because we can initialize the control in this method, in loadskin () in the method, we have passed the page. the loadcontrol (defaskskinpath) method returns a usercontrol. Now we use the control method in initializeskin (control skin. the findcontrol method searches for the specified server control in usercontrol and binds the control to data and events. Take the login control as an example. extract some code in the aspnetforums. Controls. login class as follows:

Public class login: skinnedforumwebcontrol {// inherits from the skinnedforumwebcontrol base class
String skinfilename = "Skin-Login.ascx"; // default skin file
Textbox username; // account input box
Textbox password; // password input box
Button loginbutton; // the login button.
}
Public login (): Base (){
If (skinfilename = NULL)
Skinfilename = skinfilename; // defines the default skin file
}
// Override initializeskin Initialization
Override protected void initializeskin (control skin ){

// Find the Textbox Control whose ID is username on the ascx page
Username = (textbox) skin. findcontrol ("username ");

// Find the Textbox Control whose ID is password on the ascx page
Password = (textbox) skin. findcontrol ("password ");

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

}

In the skin-login.ascx, each of our controls has an ID, for example, the ID in the username input box is username, so that we can override initializeskin (control skin, use username = (textbox) skin. findcontrol ("username"); to find controls in the corresponding user control file one by one. You can also bind data and events, such as loginbutton. Click + = new system. eventhandler (loginbutton_click) to the Click Event of the login button.
To sum up, Asp. net forums implements code separation through custom controls and dynamically loads user control files (*. aspx) to achieve skin change. When you are viewing ASP. NET forums2.0Source codeDo not be intimidated by Code such as <forums: navigationmenu displaytitle = "true" id = "navigationmenu1" runat = "server"/>, and directly switch to the Class View, find the corresponding class: aspnetforums. controls. navigationmenu, from the class code ......

If you do not understand ASP. net forums2.0 source code, if you think it is too complicated, please refer to the simulation ASP. net forums implements skin-changing controls, which may help you understand :)

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.