Shining Star Services LLC

Creating a Custom Membership Provider and Membership User utilizing a Data Set Table Adapter - Step 17

by Nannette Thacker

Loading a Menu Programmatically based on Roles

In this example, we define our menu in the code in front, but do not add any items. All items are added in our codebehind, based on the logged in user's role.


Not logged in.


Logged in.

Code In Front

<%@ Control Language="VB"  AutoEventWireup="false" CodeFile="MenuSSS.ascx.vb"  Inherits="Controls_PageElements_MenuSSS" %>

<table width="100%" cellspacing="0" cellpadding="0">
    <tr>
        <td align="left">
            <asp:Menu ID="menu1" SkinID="SSSMenuSkin1" runat="server">
               <Items>
                </Items>   
            </asp:Menu>
        </td>
    </tr>
</table>

Code Behind - All menu items are defined here.

Partial Class Controls_PageElements_MenuSSS
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim isAdmin As Boolean = CType(Me.Page.Master, BaseMaster).IsAdmin
        Dim isLoggedIn As Boolean = CType(Me.Page.Master, BaseMaster).IsLoggedIn
        Dim childItemIndex As Integer = 0

        If Not Page.IsPostBack Then

            Dim mi As New MenuItem
            Dim currentmenucount As Integer = menu1.Items.Count

            mi = New MenuItem
            mi.NavigateUrl = "~/default.aspx"
            mi.Text = "Home"
            menu1.Items.AddAt(menu1.Items.Count, mi)


            If isLoggedIn Then
                mi = New MenuItem
                mi.NavigateUrl = "~/login/logout.aspx"
                mi.Text = "Log Out"
                menu1.Items.AddAt(menu1.Items.Count, mi)

                mi = New MenuItem
                mi.NavigateUrl = "~/login/passwordchange.aspx"
                mi.Text = "Password Change"
                menu1.Items.AddAt(menu1.Items.Count, mi)

            Else
                mi = New MenuItem
                mi.NavigateUrl = "~/login/login.aspx"
                mi.Text = "Log In"
                menu1.Items.AddAt(menu1.Items.Count, mi)


                mi = New MenuItem
                mi.NavigateUrl = "~/login/Register.aspx"
                mi.Text = "Register"
                menu1.Items.AddAt(menu1.Items.Count, mi)
            End If


            If isLoggedIn Then

                If isAdmin Then

                    mi = New MenuItem
                    mi.NavigateUrl = "~/Admin/Admin.aspx"
                    mi.Text = "Admin"
                    currentmenucount = menu1.Items.Count
                    menu1.Items.AddAt(currentmenucount, mi)

                End If

            End If
        End If

    End Sub

End Class

We are now ready to begin Step 18.

Steps for Creating a Custom Membership Provider and Membership User utilizing a DataSet Table Adapter:

Introduction: Creating a Custom Membership Provider and Membership User utilizing a DataSet Table Adapter.
Step 1: Creating the Project and Folders.
Step 2: Table Data Structure and Web.config for this Tutorial.
Step 3: Creating The DataSet.
Step 4: Creating the Table Adapter Methods for GetUserByLogin, GetUserByUserName, and InsertUser.
Step 5: Creating our Custom MembershipProvider Class.
Step 6: Adding Properties to Our Custom MembershipProvider Class.
Step 7: Creating Our Custom MembershipUser Class.
Step 8: Customizing our MembershipProvider Class.
Step 9: Our Custom RoleProvider Class.
Step 10: Our Master page.
Step 11: Our Cookie Handler class.
Step 12: Our Log In page.
Step 13: Our Register page.
Step 14: Our Log Out page.
Step 15: Our Change Password page.
Step 16: Our Administration page.
Step 17: Loading a Menu Programmatically based on Roles.
Step 18: Our Default page.
Download the ZIP files:
VB: ShiningStarCustomMemberProviderTutorial.zip
© Copyright 1997-2017 Shining Star Services LLC, Nannette Thacker. All Rights Reserved.