DotNetNuke and the Tab Module Drop Down

Every build something you’re certain you need, finish it, and then realize there’s a better way, but you love the solution so much you can just trash it?  That’s the origin of this post.  I’m sure I’ll need this later so I don’t want to just delete but I can’t leave it in my project because then I’ll forget why it’s there and shouldn’t be and then try to use and create an unmitigated coding mess.

I know you developers have been there.

But I can’t just throw it away, so here it is for all to enjoy.

Ever needed to use the Tab Module drop down that DotNetNuke uses to put modules on a page?  I did.

Start with a user control and put this code on the presentation page

<asp:Label runat="server" ID="lblErr" ForeColor="White"></asp:Label>
<asp:UpdatePanel runat="server" ID="upTabs" ChildrenAsTriggers="true">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddlTabs" EventName="SelectedIndexChanged" />
    </Triggers>
    <ContentTemplate>
        <asp:Label runat="server" ID="lblModErr"></asp:Label>
        <asp:DropDownList runat="server" ID="ddlTabs" AutoPostBack="true">
        </asp:DropDownList>
        <asp:DropDownList runat="server" ID="ddlModules">
        </asp:DropDownList>
    </ContentTemplate>
</asp:UpdatePanel>

And this code belongs in the code behind

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
  If Not Page.IsPostBack Then
    ddlTabs.DataSource = TabController.GetPortalTabs(Me.PortalId, -1, True, Localization.GetString("DefaultPagesListItem.Text", LocalResourceFile), True, False, False, True, True)
    ddlTabs.DataValueField = "TabID"
    ddlTabs.DataTextField = "IndentedTabName"
    ddlTabs.DataBind()
  End If
Catch exc As Exception    'Module failed to load
  ProcessModuleLoadException(Me, exc)
End Try
End Sub

Protected Sub ddlTabs_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTabs.SelectedIndexChanged
Try
  ddlModules.Items.Clear()

  Dim objModules As New ModuleController
  Dim tabModules As Dictionary(Of Integer, ModuleInfo) = objModules.GetTabModules(ddlTabs.SelectedValue)
  Dim result As New Dictionary(Of String, Integer)

  For Each tabModule In tabModules.Values
    ddlModules.Items.Add(New ListItem(tabModule.ModuleTitle, tabModule.ModuleID))
  Next
Catch ex As Exception
  lblModErr.Text = ex.Message
End Try
End Sub

Simple and yet no one else seems to have it on one page.  Now, at long last, well, not long last, but here it is!

Leave a Reply

Your email address will not be published. Required fields are marked *