Solved

Simplifying MasterPage SelectedIndex for Datalist

Posted on 2014-02-10
11
189 Views
Last Modified: 2014-02-12
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

0
Comment
Question by:EDW_Gideon
  • 6
  • 5
11 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39849827
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
0
 

Author Comment

by:EDW_Gideon
ID: 39850656
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

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39852158
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?
0
 

Author Comment

by:EDW_Gideon
ID: 39853644
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.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39854632
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
 
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 96

Expert Comment

by:Bob Learned
ID: 39854660
Attached is an Excel spreadsheet with the matrix from step 2.
Evaluation-Matrix.xls
0
 

Author Comment

by:EDW_Gideon
ID: 39854688
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?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39854705
Here is some example code for the simplified test matrix:

Public Sub Test()
	Dim evaluationMatrix = New List(Of Evaluation)() From { 
		New Evaluation(0, 0), 
		New Evaluation(1, 1), 
		New Evaluation(1, 2, "N"), 
		New Evaluation(1, 3, "N", "N"), 
		New Evaluation(1, 4, "N", "N", "N"), 
		New Evaluation(2, 2, "Y"), 
		New Evaluation(2, 3, "Y", "N"), 
		New Evaluation(2, 3, "N", "Y"), 
		New Evaluation(2, 4, "Y", "N", "N"), 
		New Evaluation(2, 4, "N", "Y", "N"), 
		New Evaluation(2, 4, "N", "N", "Y"), 
		New Evaluation(3, 3, "Y", "Y"), 
		New Evaluation(3, 4, "Y", "Y", "N"), 
		New Evaluation(3, 4, "Y", "N", "Y"), 
		New Evaluation(3, 4, "Y", "Y", "Y"), 
		New Evaluation(4, 4, "Y", "Y", "Y") 
	}

	For Each evaluation As var In evaluationMatrix
		Dim index = evaluation.SelectedIndex
		Dim page = evaluation.Page
		Dim create = evaluation.Create
		Dim access = evaluation.Access
		Dim password = evaluation.Password

                ' Do the evaluate to set the SelectedIndex here.
	Next
End Sub

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39854708
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.
0
 

Author Comment

by:EDW_Gideon
ID: 39854714
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!
0
 

Author Comment

by:EDW_Gideon
ID: 39854716
I.E. "Take out all the noise"
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now