将DNN模块作为UserControl的容器


DNN模块一般情况不能作为一个独立的系统,但模块开发却要求模块之间低藕合,像HTML模块还行,但稍微复杂点的模块,比如新闻系统(二十四画生的永日新闻系统),它就为了实现各功能,设了很多模块,但实际上在 DesktopModules 却只有一个文件夹YongRi - NewsArticles,这是因为作为一个独立完成的新闻系统要求高内聚,所以虽然定义了好几个模块,但编程上,由于内聚性,只能算一个模块。

所以,我想了个办法,在模块的入口UserControl制作成一个容器UserControl,即加载本模块内其他UserControl的UserControl,它具体要加载哪个模块就在 Setting页中设置。

Seting.ascx关键代码:

        #region Base Method Implementations
        
public override void LoadSettings()
        {
            
try 
            {
                
if(!Page.IsPostBack)
                {
                    BindCategories();

                    
if (!Null.IsNull(TabModuleSettings["ControlKey"]))
                    {
                        
string ControlKey = (string)TabModuleSettings["ControlKey"];
                        ListItem listItem 
= ddlControlKey.Items.FindByValue(ControlKey);
                        
if (listItem != null)
                        {
                            listItem.Selected 
= true;
                        }
                    }

容器UserControl关键代码

        string ControlKey;

        
private void Page_Init()
        {
            InjectControl();
        }
                
        
protected void Page_Load(object sender, EventArgs e)
        {

        }

        
protected void InjectControl()
        {
            
if (!Null.IsNull(Settings["ControlKey"]))
            {
                ControlKey 
= (string)Settings["ControlKey"];
            }

            
if (!string.IsNullOrEmpty(this.ControlKey))
            {
                
string strControlPath = this.ResolveUrl("~/DesktopModules/CrossArticle/" + ControlKey);
                
//Control ctl = this.LoadControl(strControlPath);
                
//this.Controls.Add(ctl);

                PortalModuleBase portalModuleBase 
= (PortalModuleBase)this.LoadControl(strControlPath);

                
if (portalModuleBase != null)
                {
                    portalModuleBase.ModuleConfiguration 
= base.ModuleConfiguration;

                   
// set the control ID to the resource file name ( ie. controlname.ascx = controlname )
                   
// this is necessary for the Localization in PageBase
                    portalModuleBase.ID = Path.GetFileNameWithoutExtension(strControlPath);

                    
this.Controls.Add((Control)portalModuleBase);
                    ((Control)portalModuleBase).DataBind();
                }
            }
        }
原文地址:https://www.cnblogs.com/shiningrise/p/847048.html