DNN: Using multiple web user controls in one modul

2020-06-04 03:08发布

问题:

I'm a DotNetNuke newbie. Please be gentle. I'm using the "DotNetNuke 6 Compiled Module" template to build my module. I already have View.ascx control in the project and have added another control called test.ascx.

My question is: how do I show different different views in different pages I add the module to. (if that is possible at all) e.g Show View.ascx on say the default.aspx page and then on the default2.aspx page show the test.ascx user control?

If this is not possible does it mean I need different visual studio projects for each ascx control. Surely not.

回答1:

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.



回答2:

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.