"ASP.NET ForumS2.0 In-depth Analysis" ASP.NET Forums How to implement code separation and change

xiaoxiao2021-03-06  41

Develop web projects in Visual Studio, the web form page consists of two parts: visual elements (HTML, Server Controls, and Static Texts) and programming logic for this page. These two components are typically stored in a separate file. Visual elements are created in a .aspx file, and the code is located in a separate class file (.aspx.vb or .aspx.cs). Or sometimes create visual elements and code in the same file.

And there is no familiar .aspx.cs file we are familiar with the .aspx.cs file in the web form page of ASP.NET Forum, and have not found any C # code. Instead, it is a control, where is the code? !

The following will be detailed as an example of login.aspx to see how ASP.NET Forum is code separation and change skin: First we look at Login.aspx running effect under two skin styles (Theme: electricmidnight)

Just change the default skin of ASP.NET Forum, which is also login.aspx, which shows two different skin styles. Let me recall the VS.NET, let go of the skin function, if we want to implement a landing page, then we will enter the textbox of the account password in the ASPX or ASCX page, and then drag the button, double-click Button in the editing area. What is the code that is handled by Button Click on the event, most of which is done by vs.net for us.

Let's 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"%>




Note: Which is , this is a third-party control, its purpose is to ensure the consistency of the interface, extract the duplication code between the page. From the source code we didn't see any basic elements such as TextBox, Button, such as the login.aspx page effect. There is no difference between a line of C # code, but if you are more familiar with the page control, it is more difficult to find the original ASP.NET Forum, which will be launched in the interface. For the control (the page control is not specifically introduced here, if you are also related to control related knowledge Unfamiliar, I strongly recommend that you check the relevant information or books). It turns out that the implementation of the landing interface is in the control, from

<% @ 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 class view, find aspnetforums.controls.login and go to the corresponding file:

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

The control seen from the code is inherited from the SkinnedforumWebControl class:

Public class login: SkinnedforumWebControl {// Inherit from SkinnedforumWebControl

......

}

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 aspnetforum;

Using aspnetforums.components;

Using system.componentmodel;

Using system.io;

Using system.web.security;

Using aspnetforums.enumertions;

Namespace aspnetforums.controls {

[

Parsechildren

True)

]

///

/// Almost all controls in ASP.NET Forums inherit from WebControl and implement inamingContainer interface ///

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 () {

// Use the skin - if it is an anonymous user, use the system default style ///

IF (forumContext.user.isanonymous) {skinname = globals.skin;

}

Else {

Skinname = forumContext.user.Theme;

}

}

///

// / When developing a composite server control or template server control, you must rewrite this method. /// Notifys the use of the composite-based server control to create any sub-controls they contain to prepare for the return or presentation. ///

Protected Override Void CreateChildControls () {

Control Skin;

// Load user control

Skin = loadingskin ();

// Initialization control

Initializeskin (SKIN);

Controls.add (skin);

}

///

/// Find the path to the user control file via Skinname and SkinFileName, load the Control object after the user control ///// ///

protected control loadingskin () {

Control Skin;

// The location where the user control file is located

String SkinPath = Globals.getskinPath () "/ Skins /" SkinFileName.trimstart ('/');

String defaultskinpath = globals.applicationpath "/ themes / default / skins /" SKINFILENAME.TRIMSTART ('/');

// There must be SkinFileName properties

IF (SKINFILENAME ==

NULL)

"You Must Specify A Skin.");

// Get UserControl objects from the user control file.

Try {

Skin = page.loadControl (SkinPath);

}

Catch (filenotfoundexception) {

/ / If you do not find the user control file specified for the skin, load 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 for this Control to Render.");

}

}

Return Skin;

}

///

/// Initialization control, and bind control data /// ///

Protected Abstract Void Initializeskin; Control Skin;

///

/// User Control File (* .ascx) Path ///

Public string skinfilename {

Get {

Return SkinFileName;

}

SET {

Skinfilename = value;}

}

///

/// Skin name ///

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 is a custom control, it is naturally inherited from the WebControl class. The reason why INAMINGCONTAINER interface is implemented because the interface should be implemented to avoid naming conflicts on the same page.

There are two important properties in SkinnedforumWebControl: Skinname and SkinFileName indicate the skin name and user control file path, respectively. In ASP.NET Forums 2.0, there is an theme directory in the web directory, each skin corresponding to a directory, such as DEFAULT, ELECTRICMIGHT, three folders under each skin folder: Image, Skins and Style, separately Store corresponding picture files under this skin, user control file (* .ascx), and style table CSS file. Through these two properties, we can know the real path of the user control file (* .ascx), for example, our SkinName is default, skinFileName is SKIN-Login.ascx, then the path of the user control is themes / default / skins / Skin- Login.ascx, the same reason, if we replace the skin style into electricmidnight, then the path to the user control will be themes / electricmidnight / skins / skin-login.ascx.

There are also two important methods, one is loadskin (), in which the path is first identified by the two properties described above, and then pass the Page.LoadControl (DefaultskinPath) method, from the user control file Get the UserControl object. This is why the skin is different, and the page style is different. Different.

But light is 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 an account, that input box is a password, know if the user clicks the login button. Recall the vs.net, IDE automatically helps us identify these controls according to ID, and double-click the button in the IDE, how easy it can be added directly to the code to click on the event, how convenient. But what should we now? ......

There is also an abstract initializeskin (control Skin) method in the base class. All controls inherited from SkinnedforumWebControl must override this method, because we can initialize the control in this method, we have passed Page, LoadControl (DefaultskinPath) Method Returns a UserControl, now we are searching for specified server controls in UserControl in Initializeskin (Control Skin), and binds data and events for controls in UserControl. Or use the login control as an example, part of the code in the aspnetforumus.controls.login class is as follows: public class login: SkinnedforumWebControl {

// Inherit 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;

// Log in button

}

Public login ():

Base () {

IF (SKINFILENAME ==

NULL)

Skinfilename = SkinFileName;

/ / Define the default skin file

}

// initialize rewriting InitializeSkin override protected void InitializeSkin (Control skin) {// find ascx page ID is the username textbox control username = (TextBox) skin.FindControl ( "username"); // Find the page ID is a password ascx the textbox control 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 for the landing button loginbutton.text = ResourceManager.getstring ("Loginsmall_Button");}

In skin-login.ascx, each of our controls has an ID. For example, the ID of the username input box is UserName, so we can use username when we override Initializeskin (Control Skin). Skin.FindControl ("UserName"); this method is to find a control in the user control file. Binding data and events, such as: loginbutton.click = new system.eventhandler (LoginButton_Click), Bind the Click event of the login button.

In summary, ASP.NET Forum is implemented by custom controls, and dynamically load user control files (* .aspx) in the control. When you are watching the ASP.NET Forums 2.0 source code, don't be scared by directly switches to Class view, find the corresponding class: aspnetforums.controls.navigationMenu, from class code ... If you are not very understanding, you can study the source code of ASP.NET ForumS2.0, if you feel too complicated, please see simulation ASP.NET Forums implements examples of controls that can be changed to the article, perhaps you can help you understand :)