Asp.net maintains the idea of implementing classes for dynamically loaded controls

Source: Internet
Author: User
In the Asp.net program design, I personally think that it is very troublesome to dynamically load controls and maintain their statuses. Not only do I need to create this control at a specific page processing stage, you also need to re-create the control and specify its event handler every time the page is loaded. This maintenance workload is very large, if multiple controls on a page are dynamically created.

Similarly, in the C/S structure program design, it becomes very simple. We only need to create a control once, and the control will always be in that place unless it is displayed and deleted.

The reason for this is that the architecture of C/S is different from that of B/S. C/S is a single creation interface, and the B/S structure is created multiple times. The B/S structure is based on a non-persistent connection network. Therefore, she needs to maintain the status of each interaction and create an Interface Based on the status information. Although a considerable part of the Work in Asp.net has been hidden and well done, I think there is still a need for improvement in dynamic controls.

 

Let's see how we traditionally process dynamic controls.

Private button_clicked (....)

{

//.... Do something

Button BTN = new button ();

Placeholder1.controls. Add (BTN );

BTN. Click + = ....;

BTN. Text = "…"

 

Viewstatue ["btncreated"] = true; // sets the flag to be created for the control.

//...

}

In the page_load event of the page

Page_load (...)

{

If (viewstatue ["btncreated"]! = NULL)

{

Button BTN = new button ();

// Repeat the steps in the button_clicked event

}

}

 

There may be only one or two similar controls, but if you need to dynamically create many different types of controls, the locations of the controls are distributed in various places on the page, it will become very complicated and difficult to grasp. Preliminary

 

I personally think that maintaining the status of dynamically created controls can be fully implemented by the page base class. This class monitors Dynamically Loaded controls, when the page is re-loaded, create the control and add it to the appropriate place, so that developers feel that the control is created once, you do not need to create a new one each time the page is loaded.

 

The initial idea for implementing this base class is as follows:

 

Public class persistancedynamiccontrol: system. Web. UI. Page

{

// Stores the list of dynamically created controls. Of course, this is only a demonstration. viewstate should be used in actual applications to save this list.

Object [] dynamiccontrolslist;

 

// Add dynamically created controls to the page

// This function is the core part of the Implementation. It can use reflection to read and save the status information (including location information) of the created dynamic control, this allows you to create these controls when you create them on the next page.

Public addcontrol (control parentcontrol, control dynamiccontrol)

 

// This function is used to create controls based on the list of saved dynamic controls and place them in a proper location. This function can be called in the oninit function.

Private createdynamiccontrols ()

Public override oninit ()

{

Createdynamiccontrols ();

}

 

// The indexer returns a reference to the dynamically created control.

Object dynamiccontrols [String controlname]

}

 

 

 

Then, the created page only needs to inherit this base class, use the addcontrol () function to add dynamically created controls, and use (classtype) base. use dynamiccontrols ["classname"] to obtain reference to dynamically created controls. Once a dynamic control is added to the page, it can be accessed all the time, regardless of the number of page post back times. In this way, it is a bit similar to the C/S structure.

 

This is just an immature idea of my own and has not yet been implemented. You are welcome to criticize it.

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.