Dynamically load the components of the user control!

zhaozj2021-02-16  109

The purpose of our control is to put it in the page. According to different conditions, we can change the loaded user control!

In fact, the principle is the principle of MasterPage. This masterPage will be available in VS2005, but can now be implemented.

Use in my project:

MasterPage is four classes, where the container is dynamically load user controls. This feature will be everywhere in VS2005.

The following is a brief introduction to the dynamic loading of the leaves assembly:

We only need to change the address of the user component to dynamically change the loaded user components.

Dynamically change SkinPath in the program.

The component source code is as follows:

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 System.ComponentModel; using System Using system.Web.security; using region.components

Namespace region.controls {

[Parsechildren] ///

/// Page components, get /// Public Abstract Class SkinnedControl: WebControl, InamingContainer from User Components

String SkinFileName = NULL; STRING SKINNAME = NULL; STRING SKINPATH;

// String ReturnURL = NULL; ///

/// Add sub-component /// protected override void createChildControls () {if (SkinPath == Null || SkinPath == "|| SkinPath. Trim () == "") Return; Control Skin; Skin = Loadskin (); Initializeskin (SKIN); Controls.Add (Skin);

///

/// Get Touchboard component /// /// Component protected control loadskin () {Control Skin; Try {Skin = Page.loadControl (SkinPath) } Catch (filenotfoundexception) {throw new exception ("Not found: [" SkinPath "].");} Return Skin;

///

/// Initialization setup plate /// /// Component protected abstract void initializeskin; /// /// User component name /// public string skinfilename {get {returnofinfilename;} set {skinfilename = value;}}

///

/// Component name /// protected string skinname {get {return skinname;} set {skinname = value;}}

///

/// User component relative path and component name /// public string SkinPath {get {returnpidi.com;} set {skinpath = value; skinfilename = value.trimstart ('/'); }

}

For example, one of my applications:

Switch (Context.pageID) {CASE "1_1": base.skinpath = "~ / themes / signmanager / termlist.ascx"; break; case "1_1_1": base.skinpath = "~ / themes / signmanager / terminal.ascx" Break; Case "1_2": base.skinpath = "~ / themes / signmanager / memberlist.ascx"; Break; Case "1_2_1": base.skinpath = "~ / themes / signManager / membedit.ascx"; break; case "2_1": base.skinpath = "~ / themes / signmanager / cardsend.ascx"; Break; Case "3_1": base.skinpath = "~ / themes / signmanager / membernoEdit.ascx"; break; case "4_1": Base.skinpath = "~ / theme / acountedit.ascx"; Break; default: base.skinpath = "";

Based on the development development, we can save a lot of repetitiveness on the leaves. Simply quote the component in the place where the leaves is repeated

such as:

转载请注明原文地址:https://www.9cbs.com/read-10346.html

New Post(0)