Link to home
Start Free TrialLog in
Avatar of EDW_Gideon
EDW_GideonFlag for United States of America

asked on

Simplifying MasterPage SelectedIndex for Datalist

I have a datalist on my MasterPage as a menu element, within the masterpage there is a "SetPage" sub that determines which menu item is selected.  The current state of this sub is such that if I want to add additional menu options the sub will get significantly more complex and I know there has to be a simple answer to this...  So I'm posting what I have here to see what you all think...  This datalist is populated on the fly and has the potential to have as many as 8 options and as few as 2.  Session.Contents("page") is populated in the load sub of each page.  Thanks in advance!

  Public Sub SetPage()
    Dim dt As DataTable
    Dim dr As DataRow
    Dim i As Integer

    dt = New DataTable()
    dr = dt.NewRow()

    Try
      dt.Columns.Add("DisplayName")
      dt.Columns.Add("NavigateURL")
      dr.Item("DisplayName") = "Login"
      dr.Item("NavigateURL") = ConfigurationManager.AppSettings("Login")
      dt.Rows.Add(dr)
      dr = dt.NewRow()
      i = 0

      If Session.Contents("Create") = "Y" Then
        Session("DataSource") = "TDDD"
        If Session.Contents("UserSystem") = "TD16" Then
          dr.Item("DisplayName") = "Build User TD16"
        ElseIf Session.Contents("UserSystem") = "TD21" Then
          dr.Item("DisplayName") = "Build User TD21"
        ElseIf Session.Contents("UserSystem") = "TD25" Then
          dr.Item("DisplayName") = "Build User UDM"
          'dr.Item("DisplayName") = "Build User TD25"
        ElseIf Session.Contents("UserSystem") = "TD26" Then
          dr.Item("DisplayName") = "Build User TD26"
        ElseIf Session.Contents("UserSystem") = "TD27" Then
          dr.Item("DisplayName") = "Build User TD27"
        ElseIf Session.Contents("UserSystem") = "TD28" Then
          dr.Item("DisplayName") = "Build User TD28"
        ElseIf Session.Contents("UserSystem") = "TD29" Then
          dr.Item("DisplayName") = "Build User TD29"
        ElseIf Session.Contents("UserSystem") = "TD68" Then
          dr.Item("DisplayName") = "Build User UDM"
        Else
          'Access_PL.Visible = True
          Return
        End If

        dr.Item("NavigateURL") = ConfigurationManager.AppSettings("Create_User")
        dt.Rows.Add(dr)
        dr = dt.NewRow()
        i = i + 1

      End If
      If Session.Contents("Access") = "Y" Then

        dr.Item("DisplayName") = "User Access"
        dr.Item("NavigateURL") = ConfigurationManager.AppSettings("Access")
        dt.Rows.Add(dr)
        dr = dt.NewRow()
        i = i + 1

      End If
      If Session.Contents("Password") = "Y" Then

        dr.Item("DisplayName") = "User Password"
        dr.Item("NavigateURL") = ConfigurationManager.AppSettings("Password")
        dt.Rows.Add(dr)
        dr = dt.NewRow()

        i = i + 1

      End If
      If Session.Contents("Maintain") = "Y" Then

        dr.Item("DisplayName") = "User Maintenance"
        dr.Item("NavigateURL") = ConfigurationManager.AppSettings("Maintain")
        dt.Rows.Add(dr)
        dr = dt.NewRow()
        i = i + 1

      End If

      dlMainMenu.DataSource = dt
      dlMainMenu.DataBind()

      If Session.Contents("page") = 0 Then
        dlMainMenu.SelectedIndex = "0"

      ElseIf Session.Contents("page") = 1 Then
        dlMainMenu.SelectedIndex = 1

      ElseIf Session.Contents("page") = 2 Then
        If Session.Contents("Create") = "Y" Then

          dlMainMenu.SelectedIndex = 2
        Else

          dlMainMenu.SelectedIndex = 1
        End If


      ElseIf Session.Contents("page") = 3 Then
        If Session.Contents("Create") = "N" And Session.Contents("Access") = "N" Then
          dlMainMenu.SelectedIndex = 1
        ElseIf Session.Contents("Create") = "Y" And Session.Contents("Access") = "N" Then
          dlMainMenu.SelectedIndex = 2
        ElseIf Session.Contents("Create") = "N" And Session.Contents("Access") = "Y" Then
          dlMainMenu.SelectedIndex = 2
        Else
          dlMainMenu.SelectedIndex = 3
        End If

      ElseIf Session.Contents("page") = 4 Then
        If Session.Contents("Create") = "N" And Session.Contents("Access") = "N" And Session.Contents("Password") = "N" Then
          dlMainMenu.SelectedIndex = 1
        ElseIf Session.Contents("Create") = "Y" And Session.Contents("Access") = "N" And Session.Contents("Password") = "N" Then
          dlMainMenu.SelectedIndex = 2
        ElseIf Session.Contents("Create") = "N" And Session.Contents("Access") = "Y" And Session.Contents("Password") = "N" Then
          dlMainMenu.SelectedIndex = 2
        ElseIf Session.Contents("Create") = "N" And Session.Contents("Access") = "N" And Session.Contents("Password") = "Y" Then
          dlMainMenu.SelectedIndex = 2
        ElseIf Session.Contents("Create") = "Y" And Session.Contents("Access") = "Y" And Session.Contents("Password") = "N" Then
          dlMainMenu.SelectedIndex = 3
        ElseIf Session.Contents("Create") = "Y" And Session.Contents("Access") = "N" And Session.Contents("Password") = "Y" Then
          dlMainMenu.SelectedIndex = 3
        ElseIf Session.Contents("Create") = "N" And Session.Contents("Access") = "Y" And Session.Contents("Password") = "Y" Then
          dlMainMenu.SelectedIndex = 3
        Else
          dlMainMenu.SelectedIndex = 4
        End If

      End If

    Catch ex As Exception
      Throw ex

    End Try

  End Sub

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Let's tackle one thing at a time, and hopefully arrive at something more dynamic.

  
        If Session.Contents("UserSystem") = "TD16" Then
          dr.Item("DisplayName") = "Build User TD16"
        ElseIf Session.Contents("UserSystem") = "TD21" Then
          dr.Item("DisplayName") = "Build User TD21"
        ElseIf Session.Contents("UserSystem") = "TD25" Then
          dr.Item("DisplayName") = "Build User UDM"
          'dr.Item("DisplayName") = "Build User TD25"
        ElseIf Session.Contents("UserSystem") = "TD26" Then
          dr.Item("DisplayName") = "Build User TD26"
        ElseIf Session.Contents("UserSystem") = "TD27" Then
          dr.Item("DisplayName") = "Build User TD27"
        ElseIf Session.Contents("UserSystem") = "TD28" Then
          dr.Item("DisplayName") = "Build User TD28"
        ElseIf Session.Contents("UserSystem") = "TD29" Then
          dr.Item("DisplayName") = "Build User TD29"
        ElseIf Session.Contents("UserSystem") = "TD68" Then
          dr.Item("DisplayName") = "Build User UDM"
        Else
          'Access_PL.Visible = True
          Return
        End If

Open in new window


Suggestion:  build a lookup dictionary

Dim lookupDictionary = New Dictionary(Of String, String)() From { 
	{"TD16", "Build User TD16"}, _
	{"TD21", "Build User TD21"}, _
	{"TD25", "Build User UDM"}, _
	{"TD26", "Build User TD26"}, _
	{"TD27", "Build User TD27"}, _
	{"TD28", "Build User TD28"}, _
	{"TD29", "Build User TD29"}, _
	{"TD68", "Build User UDM"} _
}

Dim userSystem As String = Session.Contents("UserSystem")

dr("DisplayName") = IIf(lookupDictionary.ContainsKey(userSystem)), lookupDictionary(userSystem), "Unknown system")

Open in new window


By the way, the Item property is the default property for the System.Data.DataRow class, so you don't need to specify it.

How to: Declare and Call a Default Property in Visual Basic
http://msdn.microsoft.com/en-us/library/se6z814t.aspx
Avatar of EDW_Gideon

ASKER

Sorry I should have specified in more detail.  I appreciate the simplification above but how can we simplfy the Selected Index portion below?  It currently only allows for 4 options and if I add more it's going to get out of control...  There has to be a simpler way I just can't seem to think of it.

      If Session.Contents("page") = 0 Then
        dlMainMenu.SelectedIndex = "0"
      ElseIf Session.Contents("page") = 1 Then
        dlMainMenu.SelectedIndex = 1
      ElseIf Session.Contents("page") = 2 Then
        If Session.Contents("Create") = "Y" Then
          dlMainMenu.SelectedIndex = 2
        Else
          dlMainMenu.SelectedIndex = 1
        End If

      ElseIf Session.Contents("page") = 3 Then
        If Session.Contents("Create") = "N" And Session.Contents("Access") = "N" Then
          dlMainMenu.SelectedIndex = 1
        ElseIf Session.Contents("Create") = "Y" And Session.Contents("Access") = "N" Then
          dlMainMenu.SelectedIndex = 2
        ElseIf Session.Contents("Create") = "N" And Session.Contents("Access") = "Y" Then
          dlMainMenu.SelectedIndex = 2
        Else
          dlMainMenu.SelectedIndex = 3
        End If

      ElseIf Session.Contents("page") = 4 Then
        If Session.Contents("Create") = "N" And Session.Contents("Access") = "N" And Session.Contents("Password") = "N" Then
          dlMainMenu.SelectedIndex = 1
        ElseIf Session.Contents("Create") = "Y" And Session.Contents("Access") = "N" And Session.Contents("Password") = "N" Then
          dlMainMenu.SelectedIndex = 2
        ElseIf Session.Contents("Create") = "N" And Session.Contents("Access") = "Y" And Session.Contents("Password") = "N" Then
          dlMainMenu.SelectedIndex = 2
        ElseIf Session.Contents("Create") = "N" And Session.Contents("Access") = "N" And Session.Contents("Password") = "Y" Then
          dlMainMenu.SelectedIndex = 2
        ElseIf Session.Contents("Create") = "Y" And Session.Contents("Access") = "Y" And Session.Contents("Password") = "N" Then
          dlMainMenu.SelectedIndex = 3
        ElseIf Session.Contents("Create") = "Y" And Session.Contents("Access") = "N" And Session.Contents("Password") = "Y" Then
          dlMainMenu.SelectedIndex = 3
        ElseIf Session.Contents("Create") = "N" And Session.Contents("Access") = "Y" And Session.Contents("Password") = "Y" Then
          dlMainMenu.SelectedIndex = 3
        Else
          dlMainMenu.SelectedIndex = 4
        End If

      End If

Open in new window

It is difficult for me to find a map between the "business rules", and the menu.  Can you describe what that section of code does?
Sorry about that...  It identifies the datalist tab that was clicked to change the context/color of the button to show it is the current "tab" selected.  Each page is assigned w/ a "page" val within Session (Shown below).  The code referenced above assigns the datalist value's SelectedIndex to determine, based upon the granted access, which tab has been clicked.  The access is assigned from a table when the user logs-in, if the user doesn't have access to a given page the routine above changes the SelectedIndex so that when a new tab is selected from the datalist the highlight matches what the user has selected.

One of the Page_Load routines:
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
      Session("page") = 4
    End If
  End Sub

Open in new window


Login Routing:
Protected Sub Login_click() Handles Login_click.click
    sDataSource = "TDDD" 'tdpid
    Session("DataSource") = "TDDD"
    If TeradataLIST.SelectedValue = 0 Then
      Session("DataSource") = "TD16"
    ElseIf TeradataLIST.SelectedValue = 1 Then
      Session("DataSource") = "TD21"
    ElseIf TeradataLIST.SelectedValue = 2 Then
      sDataSource = "TDDP" 'tdpid
      Session("DataSource") = "TDDP" 'Session("DataSource") = "TD25"
    ElseIf TeradataLIST.SelectedValue = 3 Then
      Session("DataSource") = "TD26"
    ElseIf TeradataLIST.SelectedValue = 4 Then
      Session("DataSource") = "TD27"
    ElseIf TeradataLIST.SelectedValue = 5 Then
      Session("DataSource") = "TD28"
    ElseIf TeradataLIST.SelectedValue = 6 Then
      Session("DataSource") = "TD29"
    ElseIf TeradataLIST.SelectedValue = 7 Then
      Session("DataSource") = "TD68"
    Else
      Access_PL.Visible = True
      Return
    End If

    uid = db_user_id.Text.ToString.ToUpper

    If uid = "USERID1" Or uid = "USERID2" Or uid = "USERID3" Or uid = "USERID4" Then
      rssql = "Select password from passtbl;"
      rsPASSTHRU = rsEDWDBA_PASSTHRU("passthru")
      silent_pswd = rsPASSTHRU  
    End If

    stringBuilder.DataSource = sDataSource
    stringBuilder.UserId = db_user_id.Text.ToString()
    stringBuilder.Password = db_pwd.Text.ToString()
    stringBuilder.PersistSecurityInfo = True
    stringBuilder.SessionMode = "ANSI"

    SelStmt = "Select * from USERACCESSTBL;"

    SelStmt = SqlQuery.ToString()
    Dim command As DbCommand = con.CreateCommand
    command.CommandText = SelStmt
    command.CommandType = CommandType.Text

    Try
      con.Open()

      Dim reader As DbDataReader = command.ExecuteReader()

      If reader.Read Then
        Session("Create") = reader("USER_CREATE_PANEL")
        Session("Access") = reader("USER_ACCESS_PANEL")
        Session("Password") = reader("USER_PASSWORD_PANEL")
        Session("Maintain") = reader("USER_MAINT_PANEL")
        Session("UserSystem") = reader("USER_SYSTEM")
        Session("AdminLogin") = "MYUSRSTBL" 

        Dim MyMasterPage As MasterPage = DirectCast(Page.Master, MasterPage)

        MyMasterPage.SetPage()
        If Session("Create") = "Y" Then
          strURL = ConfigurationManager.AppSettings("Create_User")
          Server.Transfer(strURL, True)
        ElseIf Session("Access") = "Y" Then
          strURL = ConfigurationManager.AppSettings("Access")
          Server.Transfer(strURL, True)
        ElseIf Session("Password") = "Y" Then
          strURL = ConfigurationManager.AppSettings("Password")
          Server.Transfer(strURL, True)
        ElseIf Session("Maintain") = "Y" Then
          strURL = ConfigurationManager.AppSettings("Maintain")
          Server.Transfer(strURL, True)
        End If 
      Else

        Access_PL.Visible = True
      End If
    Catch ex As Exception
      con.Close()
      Access_PL.Visible = True
      Access_Error.Text = ex.Message.ToString


      Return
    Finally
      con.Close()
      Access_PL.Visible = True
    End Try


End Sub

Open in new window


I want to add 4 or so more pages that users could have access to but with the way the tab selection is handled the code would get exponentionally more complex to the point it's crazy.  I started trying to add one more page and the code for just one more tab virtually doubled that which is being used now, because you have to account for each possibility and then assign the selected index for each potential access combination.  I hope this is helpful, let me know if I've answered your question.
Here is the results of my simplification exercise, also trying to find a pattern.

________________________

Simplification Process
________________________

1. Strip out all the noise, leaving the evaluation conditions



page = 0
    index=0

page = 1
    index=1
   
page = 2
 Create = Y
  index=2
 Create = N
  index=1

page = 3
 Create = N And Access = N
  index=1
 Create = Y And Access = N
  index=2
 Create = N And Access = Y
  index=2
 Create = Y And Access = Y
  index=3

page = 4
 Create = N And Access = N And Password = N
  index=1
 Create = Y And Access = N And Password = N
  index=2
 Create = N And Access = Y And Password = N
  index=2
 Create = N And Access = N And Password = Y
  index=2
 Create = Y And Access = Y And Password = N
  index=3
 Create = Y And Access = N And Password = Y
  index=3
 Create = N And Access = Y And Password = Y
  index=3
 Create = Y And Access = Y And Password = Y
  index=4

2. Rearrange the conditions to minimize the evaluation matrix


 
index=0
 page = 0
 
index=1
 page = 1
 page = 2 and Create = N
 page = 3 and Create = N And Access = N
 page = 4 and Create = N And Access = N And Password = N
 
index=2
 page = 2 and Create = Y
 page = 3 and Create = Y And Access = N
 page = 3 and Create = N And Access = Y
 page = 4 and Create = Y And Access = N And Password = N
 page = 4 and Create = N And Access = Y And Password = N
 page = 4 and Create = N And Access = N And Password = Y
 
index=3
 page = 3 and Create = Y And Access = Y
 page = 4 and Create = Y And Access = Y And Password = N
 page = 4 and Create = Y And Access = N And Password = Y
 page = 4 and Create = N And Access = Y And Password = Y

index=4
 page = 4 and Create = Y And Access = Y And Password = Y
 
Attached is an Excel spreadsheet with the matrix from step 2.
Evaluation-Matrix.xls
TheLearnedOne,

Thank you for this discussion it has assisted me in finding the solution, I accidentally excluded a portion of the code that would've likely provided you with more insight on how the datalist is being created.  Within the sub that we're simplifying the datalist is created:

    Dim dt As DataTable
    Dim dr As DataRow
    Dim i As Integer

    dt = New DataTable()
    dr = dt.NewRow()

    Try
      dt.Columns.Add("DisplayName")
      dt.Columns.Add("NavigateURL")
      dr.Item("DisplayName") = "Login"
      dr.Item("NavigateURL") = ConfigurationManager.AppSettings("Login")
      dt.Rows.Add(dr)
      dr = dt.NewRow()
      i = 0

Open in new window


I'm going to add a new column "index" that I believe (still walking through this mentally...) will allow me to make this determination w/o all the logic.  I'm changing the above to:

    Dim dt As DataTable
    Dim dr As DataRow
    Dim i As Integer

    dt = New DataTable()
    dr = dt.NewRow()

    Try
      dt.Columns.Add("DisplayName")
      dt.Columns.Add("NavigateURL")
      dt.Columns.Add("IndexVal")  ' ** NEW ** '
      dr.Item("DisplayName") = "Login"
      dr.Item("NavigateURL") = ConfigurationManager.AppSettings("Login")
      dr.Item("IndexVal") = "0" ' ** NEW ** '
      dt.Rows.Add(dr)
      dr = dt.NewRow()
      i = 0

Open in new window


Each of the "Session.Contents" checks will be assigned a static val so each time the page is reloaded I can check that val and know what tab is supposed to be highlighted and I don't have to go through the logic of what access is granted a second time.  I don't know why I didn't think of this before... (Still learning...)  This discussion got me to this point, I'm accepting your previous post.  Do you agree w/ my train of thought?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
By the way, that was an exercise for me, not for you.  You just got a chance to hear my thoughts.  I love your idea a lot better than mine, but you could see the better place.  I didn't have a wide enough view of your problem space.
Thanks again...  I've taken over this project and am learning VB.Net on the fly so accepted what I was given as a good start...  Hadn't considered the other angles to solve the problem until your post.  THANKS!
I.E. "Take out all the noise"