热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> .Net编程>>正文内容

在Asp.Net中动态加载控件【附源码下载】

发布时间:2010年03月12日点击数: 佚名

摘要:   【源码下载
在本文中,我将向您演示在Asp.Net页面中动态加载控件的一种方法。

问题:
假设您有一个Aspx页面,其包含一个名为"Add New"的Button,而您则想让用户每次点击Button,PostBack到Server,然后创建一个用户控件的实列,并最终想动态的添加到该页面的ControlCollections中。

之所以我说用用户控件,是因为我们很容易把许多的控件组织到一个单独的控件中,添加的时候,我们只需把该用户控件添加到页面上就可以了。

设计解决方法:
我所想到的解决该问题的方法是在Aspx页面上放一个PlaceHolder容器控件,该控件可以把所有的控件动态的添加到里面。另外,我们还设计一种"Page Controller"模式,让所有操作动态添加控件的方法都放到一个名为BasePage基类中,也就意味着,如果我们的一些Aspx页面想动态的添加一些控件,我们只需从这个BasePage类继承。

最后,我们也必须考虑在一个Aspx页面上不仅仅包含一个PlaceHolder,反之,您可以把控件动态的添加到任何的地方

最终解决方法
首先在Vs 2008创建一个WebSite.
创建完之后,添加一个BasePage类,然后把下面的代码添加到其中

  1. // Holds the Session Key to store the added-controls on page 
  2.     private const string SESSION_CONTAINERS_KEY = "Controls_In_Container"
  3.      
  4.     // Holds the path to a user control 
  5.     private const string PATH_TO_CONTROL = "~/App_Controls/{0}"

该SESSION_CONTAINERS_KEY将被用做Session 的值来保存,而PATH_TO_CONTROL则定义了您的程序所要载入用户控件的虚拟路径。
现在在添加数据字典属性

  1. /// <summary> 
  2.     /// Holds a dictionary of all the containers defined on  
  3.     /// the page that are used as place holders for dynamically 
  4.     /// added controls. This property is a dictionary with the  
  5.     /// place holder's ClientID serving as the key, and as value  
  6.     /// a dictionary with the key as a UserControl's ClientID  
  7.     /// and value of the  UserControl itself. 
  8.     /// This property checks to see if there are any loaded containers 
  9.     /// in the Session variable, if not it creates a new instance 
  10.     /// of an empty Dictionary of containers 
  11.     /// </summary> 
  12.     protected Dictionary<string, Dictionary<string, UserControl>> Container 
  13.     { 
  14.         get 
  15.         { 
  16.             // Create a new instance of a dictionary 
  17.             // holding as Key the name of the Conatiner 
  18.             // and as Value another Dictionary. The inline Dictionary 
  19.             // has as Key the ClientID of the UserControl added to the page 
  20.             // and as Value the UserControl itself 
  21.             Dictionary<string, Dictionary<string, UserControl>> containers = 
  22.                 new Dictionary<string, Dictionary<string, UserControl>>(); 
  23.  
  24.             // If there are containers in the Session variable 
  25.             // load them 
  26.             if (Session[SESSION_CONTAINERS_KEY] != null
  27.                 containers = (Dictionary<string, Dictionary<string, UserControl>>)Session[SESSION_CONTAINERS_KEY]; 
  28.  
  29.             return containers; 
  30.         } 
  31.     } 

如果您读上面属性的注释,您很容易就明白这段方法的作用。它仅仅只是把添加到页面上所有用户控件的每一个PlaceHolder放到一个字典中,页面上所有的PlaceHoder都有一个数据字典,并且每一个PlaceHolder都有一个用户控件集合的数据字典。
在BasePage中我们还得需要一个创建用户控件实列的方法,我们命名为CreateControlInstance,以下是其实现:

  1. /// <summary> 
  2.     /// Creates a new instance of a Control defined 
  3.     /// by a name. This method takes as input the unique control 
  4.     /// key to set a unique ID property 
  5.     /// </summary> 
  6.     /// <param name="t"></param> 
  7.     /// <returns></returns> 
  8.     protected UserControl CreateControlInstance(string controlName, string controlKey) 
  9.     { 
  10.         // Create a new instance of the control 
  11.         UserControl newControl = 
  12.             (UserControl)LoadControl(string.Format(PATH_TO_CONTROL, controlName)); 
  13.  
  14.         // Set the ID of the new control instance 
  15.         newControl.ID = string.Format(controlKey, Guid.NewGuid().ToString()); 
  16.  
  17.         return newControl; 
  18.     } 

这个方法很简单,我们只是把传过来的controlName,controlKey进行格式转换。对于controlName,我们把*~/App_Controls/{0}*其中的{0}参数, 替换成想 *~/App_Controls/MyControl.ascx"的形式,而把该Control的ID,用Guid值替换掉*MyControl_{0}*中的{0}参数。
另一个重要的方法是 *AddControlToContainer*. 其实现如下:

  1. /// <summary> 
  2.     /// It takes as input the UserControl to add and a reference 
  3.     /// to the place holder on the page. It simply adds the passed 
  4.     /// in usercontrol and adds it to the container. 
  5.     /// </summary> 
  6.     /// <param name="c"></param> 
  7.     protected void AddControlToContainer(UserControl c, PlaceHolder phContainer) 
  8.     { 
  9.         // Define a dictionary of containers 
  10.         Dictionary<string, Dictionary<string, UserControl>> containers = null
  11.  
  12.         // If there are defined containers with usercontrols in the session, get them 
  13.         if (Session[SESSION_CONTAINERS_KEY] != null
  14.             containers = (Dictionary<string, Dictionary<string, UserControl>>)Session[SESSION_CONTAINERS_KEY]; 
  15.          
  16.         // Seems there are no pre-defined containers containg usercontrols in the Session, 
  17.         // create a new dictionary 
  18.         if (containers == null
  19.             containers = new Dictionary<string, Dictionary<string, UserControl>>(); 
  20.  
  21.         // If this is the first time we are adding a Usercontrol to a container 
  22.         // add a record for this placeholder identified by the place holder's 
  23.         // ClientID. 
  24.         if (!containers.ContainsKey(phContainer.ClientID)) 
  25.             containers.Add(phContainer.ClientID, new Dictionary<string, UserControl>()); 
  26.  
  27.         // Add to the specified container a new record 
  28.         // having as value the ClientID of the UserControl 
  29.         // and as value the UserControl itself. 
  30.         if (containers[phContainer.ClientID] != null
  31.             containers[phContainer.ClientID].Add(c.ClientID, c); 
  32.  
  33.         // Update the session variable 
  34.         Session[SESSION_CONTAINERS_KEY] = containers; 
  35.     } 

这个方法非常重要,它接受两个参数,一个是您所添加的用户控件,一个是您想把该用户控件添加到传过来的指定的PlaceHolder.首先该方法会检查在Containers容器的列表中,连同添加到其中的Controls是否保存在Sesssion变量中,在这里您是否还记得我在上面提到的SESSION_CONTAINERS_KEY常量,它就是用作Session变量的retreival key。
如果这个Session变量为空,也就意味着用户是首次添加一个用户控件,之后则创建一个Container的实列。
该代码也检查在该Container容器中是否包含以PlaceHoler的ClientID为名称的字典,如果没有则说明用户控件是首次添加到指定的PlaceHolder控件中,为此,则添加一个新的空的的记录来保存将来要添加到该PlaceHolder中的UserControl或UserControls。
随后在检查在该指定的PlaceHolder中是否包含以用户控件的ClientID为健的控件,如果没有,则添加到其PlaceHolder中。
最后。Session[SESSION_CONTAINERS_KEY]被新的或者以更新的container所更新。
最后一个重要的方法是 *LoadExistingControls* ,它负责把所有的用户控件添加到Aspx页面中

  1. /// <summary> 
  2.     /// This method takes as input the PlaceHolder 
  3.     /// where to load controls to. This allows you to  
  4.     /// load controls in any container placed on 
  5.     /// the page. It simply loops through the specific 
  6.     /// container, identified by the passed argument's 
  7.     /// ClientID, dictionary and adds every control found 
  8.     /// into the passed in place holder. 
  9.     /// </summary> 
  10.     protected void LoadExistingControls(PlaceHolder phContainer) 
  11.     { 
  12.         // If there are controls to load 
  13.         if ( 
  14.             (this.Container != null) &&  
  15.             (this.Container.Count > 0) 
  16.             ) 
  17.         { 
  18.             // If the container hasn't been intialized beofre 
  19.             if (!this.Container.ContainsKey(phContainer.ClientID)) 
  20.                 return
  21.              
  22.             // Clear all previous controls 
  23.             phContainer.Controls.Clear(); 
  24.  
  25.             // Get every KeyValuePair, extract the UserControl from the Value 
  26.             // and add it to the container passed as parameter.  
  27.             foreach (KeyValuePair<string, UserControl> value in this.Container[phContainer.ClientID]) 
  28.             { 
  29.                 phContainer.Controls.Add(value.Value); 
  30.             } 
  31.         } 
  32.     } 

该方法接收一个PlaceHolder作为其参数,并首先验证并确认在Container中是否包含相应的PlaceHolder,如果有,则循环所有指定Container中的所有记录,并把每一个用户控件添加到接收的PlaceHolder中。之后将会自动呈现在Aspx页面中。
这就是动态添加控件基本主要的方法和属性的一种解决方案。

例子
对于该例子,我添加了一个名为 *MyControl.ascx*用户控件,并把其放在站点的*~/App_Controls/* 文件夹下。该用户控件非常简单,仅仅包含输入姓名的两个文本框。
而Aspx页面,则从我们刚才的BasePage类继承来替代标准的Page类,它也提供了我们额外的一些辅助方法。
在Aspx中,包含一些的Button和我们想要动态添加控件容器的PlaceHolder。下面是首次运行的截图

有一些非常重要的事要做
首先,您应该在页面请求或者PostBack的时候,确保所有的Control是否添加到页面的ControlCollection中。
而在Aspx页面中要实现该方法,最好的时机就是在页面欲呈现的时候。代码:

  1. protected void Page_Init(object sender, EventArgs e) 
  2.     { 
  3.         // ALWAYS Load existing controls 
  4.         LoadExistingControls(this.phContainer); 
  5.     } 

它就会调用BasePage中的 *LoadExistingControls* 方法,并把您所要动态添加控件的PlaceHolder作为参数传进去,确保所有的控件都载入到页面中。
当您想要添加一个新的用户控件,您只需点击一下名为 *Add New* 的Button 下面是该Button的代码

  1. /// <summary> 
  2.     /// Initializes a new UserControl and adds it to a container 
  3.     /// on the page. 
  4.     /// </summary> 
  5.     /// <param name="sender"></param> 
  6.     /// <param name="e"></param> 
  7.     protected void btnAddNew_Click(object sender, EventArgs e) 
  8.     { 
  9.         // Create a new control instance 
  10.         UserControl c = CreateControlInstance( 
  11.             MYCONTROL, 
  12.             MYCONTROL_ID); 
  13.  
  14.         // Add the new control to the phContainer place holder 
  15.         AddControlToContainer(c, this.phContainer); 
  16.  
  17.         // Load again the already added controls 
  18.         // to the specified container. 
  19.         LoadExistingControls(this.phContainer); 
  20.     } 

该Button首先调用了BasePage中的*CreateControlInstance* 来创建了一个新的用户控件的实列,一旦该实例创建,他就会把该实列和该实列的容器(phContainer)传递到BasePage中的*AddControlToContainer*方法,最后,我们在重新载入开始我们已经添加到该PlaceHolder中的控件。

为了使该实列更有说服力,我添加了一个名为*IMyControl.cs*接口类:代码如下

  1. public interface IMyControls 
  2.     /// <summary> 
  3.     /// A simple method to save the values in the control 
  4.     /// </summary> 
  5.     void Save(); 

在其中,我们仅仅定义了一个方法,但是它却能对您的程序起到重要的作用。
我使所有的用户控件都继承这个接口,并实现它。
那好,现在当您点击页面上的*Save Data*按钮,它将会循环加载指定Container中的Controls,并调用继承自IMyControl接口的User Control的Save 方法. 代码如下:

  1. /// <summary> 
  2.     /// Process the values inside each loaded UserControl 
  3.     /// </summary> 
  4.     /// <param name="sender"></param> 
  5.     /// <param name="e"></param> 
  6.     protected void btnSave_Click(object sender, EventArgs e) 
  7.     { 
  8.         // If there are controls loaded inside the phContainer place holder 
  9.         // Every container is accessed by its ClientID 
  10.         if ( 
  11.             (this.Container[this.phContainer.ClientID] != null) && 
  12.             (this.Container[this.phContainer.ClientID].Count > 0) 
  13.             ) 
  14.         { 
  15.             // Loop through all the KeyValuePairs that constitute 
  16.             // the elements of the specific container 
  17.             foreach (KeyValuePair<string, UserControl> value in this.Container[this.phContainer.ClientID]) 
  18.             { 
  19.                 // Case the value.Value which is a UserControl 
  20.                 // into the Interface so that you can access 
  21.                 // the Save method implementation on the UserControl 
  22.                 ((IMyControls)value.Value).Save(); 
  23.             } 
  24.         } 
  25.     } 

正如你所看到的,我们使用Container属性访问我们指定的Place Holder,一旦访问成功,就开始循环其中的所有的UserControls,而后调用其中每一个实现IMyControl接口的Save 方法,您也可以实现您所需要的功能。
当您点击*Save Data* 按钮,您就会看到如下的截图2,它只是把添加到页面上的UserControl的基本信息输出到页面上。

了怎样处理不仅仅只有一个placeHolder也可能有2个或多个PlaceHolder,你可以按你需要任意添加多个。
总结
在本文中,我只是介绍了动态向Aspx页面添加控件的一种方法。您可以跟随着本文的代码和说明进行阅读,或者下载本文中代码的源代码。

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作