DNN: Using multiple web user controls in one module and showing different controls in different pages

a 夏天 提交于 2019-12-03 07:00:58

Astro,

Option 1:

You need to go to Host > Extensions > Edit your extesion > Expand Module definition and click on add control.

Here you have to select your ascx control and provide key as any string. Let's say you provided key test, you selected user control and selected control type as view and saved it.

Now from view you can use following code to navigate to the newly added control: DotNetNuke.Common.Globals.NavigateUrl(TabId,"test","mid="+ModuleID);

This will redirect the page and load your page with test.ascx.

You can use this kind of option when you want to show view.ascx by default and want to switch view when on some action and show test.ascx. Disadvantage here is, when you switch to test.ascx, all other modules added to page will not be visible.

Option 2:

You have to create a new definition in module. For that go to Host > Extensions > Edit Your module > Expand Module Definitoins > Click on add and add new definition. Once definition is added, you can add your test.ascx and view control to the definition without any key.

Once above is done, if you delete and add your module to page again, it will show two modules added in the page. These are two definitions. Look at the blog module definition for example of how multiple definitions works.

This option is used when you want to show multiple view control at the same time from the same module.

I hope this helps. Let me know if you have any more questions.

A little late to the party here, but if I understand you correctly, you want to have a module with different views. To add to Prashant's methods, here are 2 options I often use;

1.) Multiview

<asp:MultiView ID="myMView" runat="server" ActiveViewIndex="0">
    <asp:View ID="ViewOne" runat="server">

      ...Content 1 here...

    </asp:View>
    <asp:View ID="ViewTwo" runat="server">

      ...Content 2 here...

    </asp:View>  
</asp:MultiView>

In code behind you can set the active view based on some condition

if(someCondition)
  myMView.ActiveViewIndex = 0;
else
  myMView.ActiveViewIndex = 1;

2.)Placeholder. This is my favorite as it allows me to separate each view and its code in its own control. You only need to register one control (the Master control) with DNN. You can have 10s, 100s, 1000s of child controls and they don't need to be registered with DNN as they will be contained inside MasterControl.ascx placeholder. In the MasterControl.ascx, add

<asp:PlaceHolder ID="myPholder"  runat="server"></asp:PlaceHolder>

Follow Prashant's instruction in method 1 and register the MasterControl with DNN. In the code behind, add the following,

string childControl;
switch (condition)
{
    case "condition1":
        childControl = ControlPath + Child1.ascx";
        break;
    case "condition2":
        childControl = ControlPath + Child2.ascx";
        break;      
    ...more conditions...
}
PortalModuleBase objModule = (PortalModuleBase)this.LoadControl(childControl);
if ((objModule != null))
{
    myPholder.Controls.Clear();
    objModule.ModuleConfiguration = this.ModuleConfiguration;
    myPholder.Controls.Add(objModule);
}

Just a different way of doing things. Good luck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!