"ASP.net Forums2.0 In-depth analysis" of the asp.net forums is how to implement code separation and exchange

Source: Internet
Author: User
Tags abstract bind visual studio
Asp.net| code separation develop Web projects in Visual Studio, Web Forms pages are made up of two parts: visual elements (HTML, server controls, and static text) and programming logic for the page. Typically, these two components are stored separately in a separate file. Visual elements are created in an. aspx file, and the code is in a separate class file (. aspx.vb or. aspx.cs). Or you can sometimes create visual elements and code in the same file.

In the ASP.net forums Web Forms page, we did not find any of our familiar. aspx.cs files, nor any C # code, replaced by a control, where is the code?!

The following will take Login.aspx as an example to detail how ASP.net forums implements code separation and skin change:
First, let's look at how the Login.aspx works under two skin styles.
(Theme:default) (Theme:electricmidnight)

Just changed the default skin of asp.net forums, also login.aspx, shows two different skin styles. First of all, in the vs.net, regardless of the change of skin function, if we want to implement a landing page, then we in the ASPX or Ascx page will enter the account password textbox, login button drag, in the Edit area double click button, Write on the button click event Processing code, how convenient, most of the code by Vs.net for us to complete.

Let's look at Login.aspx's source code:


<%@ 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:***>, this is a third-party control, the purpose is to ensure the consistency of the interface, extraction of duplicate code between pages.

From the source we do not see any form of login.aspx page effect of the TextBox, button and other basic elements. You don't even find a single line of C # code, but if you're familiar with the page controls, it's not hard to find out that the interface in ASP.net forums is encapsulated for control (this is not a special description of the page control, if you are unfamiliar with the knowledge of the control, it is highly recommended that you refer to The original landing interface is implemented in the <forums:login runat= "server" id= "PostView1"/> Control, from
<%@ Register tagprefix= "Forums" namespace= "Aspnetforums.controls" assembly= "Aspnetforums.controls"%>
We know that the class for the Login control should be: AspNetForums.Controls.Login, in vs.net, switch to Class View, locate AspNetForums.Controls.Login, and go to the corresponding file:

(This diagram tells you how to quickly find the corresponding file for the control)
 
The control that you see from your code inherits from the Skinnedforumwebcontrol class:

public class Login:skinnedforumwebcontrol {//inherits from Skinnedforumwebcontrol base class
......
}

Let's look at the base class Skinnedforumwebcontrol first.
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>
Almost asp.net the base class of all controls in forums, inherits from WebControl, and implements INamingContainer interface
</summary>
Public abstract class Skinnedforumwebcontrol:webcontrol, INamingContainer {

Forumcontext forumcontext = forumcontext.current;
string skinfilename = null;
string skinname = null;
string returnurl = null;
Forummode mode = Forummode.user;


Public Skinnedforumwebcontrol () {

Used Skin--if it is an anonymous user, use the system default style
//
if (forumContext.User.IsAnonymous) {
Skinname = Globals.skin;
}
else {
Skinname = ForumContext.User.Theme;
}

}



<summary>
You must override this method when you are developing a composite server control or a template server control.
Notifies you to create any child controls that they contain by using a composite implementation-based server control to prepare for postback or rendering.
</summary>
protected override void CreateChildControls () {
Control skin;

Load User Control
Skin = Loadskin ();

Initializing controls
Initializeskin (skin);

Controls.Add (skin);
}


<summary>
Find the path to the user control file through Skinname and Skinfilename, and then load the control object
</summary>
<returns></returns>
Protected control Loadskin () {
Control skin;
Where the user control files are located
String skinpath = Globals.getskinpath () + "/skins/" + skinfilename.trimstart ('/');
String Defaultskinpath = Globals.applicationpath + "/themes/default/skins/" + skinfilename.trimstart ('/');

Must have Skinfilename attribute
if (skinfilename = null)
throw new Exception ("You must specify a skin.");

Gets the UserControl object from the user control file.
try {
Skin = Page.LoadControl (Skinpath);
}
catch (FileNotFoundException) {

If the user control file for the specified skin is not found, mount the control file under the default skin
try {
Skin = Page.LoadControl (Defaultskinpath);
}
catch (FileNotFoundException) {
throw new Exception ("Critical error:the skinfile" + Skinpath + "could not be found. The skin must exist to render.
}
}
return skin;
}

<summary>
Initializing the control and binding the control data
</summary>
<param name= "Skin" ></param>
protected abstract void Initializeskin (control skin);

<summary>
User control file (*.ascx) path
</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;}
}

}
}
 
As can be seen from the code, the base class Skinnedforumwebcontrol inherits from the WebControl class and implements the INamingContainer interface. Since it's a custom control, it's natural to inherit from the WebControl class. The INamingContainer interface is implemented because the interface should be implemented to avoid naming conflicts on the same page when developing templated controls.
There are two important properties in Skinnedforumwebcontrol: Skinname and Skinfilename, which represent the skin name and the user control file path, respectively. In ASP.net Forums2.0, there is a themes directory under the Web directory, each skin corresponds to a directory, such as default, Electricmidnight, three folders under each Skin folder: image, skins, and style, Each is used to store the corresponding picture files, user control files (*.ascx), and style sheet CSS files under the skin. With these two properties, we can know the true path of the user control file (*.ascx), for example, if our skinname is Default,skinfilename 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 electricmidnight, then the path to the user control will be themes/electricmidnight/skins/skin-login.ascx.

There are two other important methods, one is Loadskin (), in which the path to the user control file is first identified based on the two properties described above, and then the Page.LoadControl (Defaultskinpath) method is used to obtain from the user control file UserControl object. This is why the skin is different, the page style is different, because with the skin different, we load the user control file is also different, we simply set the user control file style is not the same, you can display different styles of skin as well.
 


But that's not enough, we also need to identify page controls in the Ascx page. For example, in Skin-login.ascx, we must know which input box is the account number, the input box is a password, know whether the user clicked the login button. Back in the vs.net, the IDE automatically helps us identify these controls by ID, and by double-clicking the button in the IDE, it's easy to add the code that responds to the Click event. But what do we do now? ......

There is also an abstract initializeskin (control skin) method in the base class, and all controls that inherit from Skinnedforumwebcontrol must override the method because we can initialize the control in this method. In the Loadskin () method We have returned a UserControl through the Page.LoadControl (Defaultskinpath) method, and now we are in Initializeskin (control skin) The Control.findcontrol method is used to search for the specified server control in UserControl and bind the controls to data and events. Or take the login control as an example, extract some of the code from the AspNetForums.Controls.Login class as follows:

public class Login:skinnedforumwebcontrol {//inherits from Skinnedforumwebcontrol base class
String skinfilename = "Skin-login.ascx"; Default skin file
TextBox username; Account Entry Box
TextBox password; Password input Box
Button Loginbutton; Landing button
}
Public Login (): base () {
if (skinfilename = null)
Skinfilename = Skinfilename; To define a default skin file
}
overriding Initializeskin initialization
Override protected void Initializeskin (control skin) {

Find a TextBox control with ID username in an ascx page
Username = (TextBox) skin. FindControl ("username");

Find a TextBox control with ID password in an 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 Login button click event
Loginbutton.text = resourcemanager.getstring ("Loginsmall_button");

}

In Skin-login.ascx, each of our controls has an ID, for example, the ID of the user name input box is username, so that we can rewrite the Initializeskin (control skin) by using the username = ( TextBox) skin. FindControl ("username"); This method finds the controls in the corresponding user control file. The Click event that binds the login button is also available for binding data and events, such as Loginbutton.click + = new System.EventHandler (Loginbutton_click).
To sum up, asp.net forums is a custom control to implement code separation, and by dynamically loading the user control file (*.aspx) in the control to achieve the skin-changing function. When you look at ASP.net Forums2.0 source code, never be <forums:navigationmenu displaytitle= "true" id= "Navigationmenu1" runat= "Server" /> Such code is intimidated, directly switch to Class View, find the corresponding class: AspNetForums.Controls.NavigationMenu, from the class code ...

If you do not understand, you can study the source of ASP.net Forums2.0, if you find it too complex, please see the simulation asp.net forums implementation can change the skin of the article example, 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.