Link to home
Start Free TrialLog in
Avatar of Mayank_Agarwal
Mayank_Agarwal

asked on

ASP.NET Menu Master Page

Hi guys

 I have weird situation that i have a home page with two hyperlinks that pass querystring to call a masterpage/contentpage site.

the masterpage has a menu and contentplaceholder. the content site fills the contents using the database. The querystring from the homepage is uses to determine which menu to display (dynamically generated)

the problem i having that when i enter the first site (siteID = 1) the corrent menu is displayed, but then go back on the browser to even manually update the url to siteID = 2, the old menu remains persistant, its doesnt refresh to the new site ID. on masterpage load event i have session(siteID) = request.querystring(siteID). using debug i have made sute teh session site is is the correct one.

can you please tell what must be happening to not make the menu refresh looking at the new site ID. the query out put is correct as well.
Avatar of Raju Srivatsavaye
Raju Srivatsavaye
Flag of United States of America image

Where did you place your code to generate the menu on the Master page. Is it in the If not ispostback block..Can you post that..
Avatar of Mayank_Agarwal
Mayank_Agarwal

ASKER

I have tried in both the cases if no ispostback and without it as well but not change.
its in the master page load event it call the sub LoadMenu.
than when i click a menu item it reads to navigate me to the right page.
I have used this method to generate sub menu dynamically.
    Protected Sub Page_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session("SiteID") = Request.QueryString("siteID")
        'If Not IsPostBack Then  'False if its a new request, True its postback
        loadMenu(Session("siteID"))
        'End If
    End Sub
 
Public Sub loadMenu(ByVal siteID As Integer)
 
        Using myconn As New SqlConnection(connStr)
            Dim ds As New DataSet
            Dim sql As String = "select MenuId, Text, ParentID from Menu where siteID = " & siteID    'keep menuId for xml
            Dim da As New SqlDataAdapter(sql, myconn)
            'myconn.Open()
            da.Fill(ds)
            da.Dispose()
            ds.DataSetName = "Menus"
            ds.Tables(0).TableName = "Menu"
            Dim d As Integer = ds.Tables(0).Rows.Count
            Dim relation As New DataRelation("ParentChild", ds.Tables("Menu").Columns("MenuID"), ds.Tables("Menu").Columns("ParentID"), True)
            relation.Nested = True
            ds.Relations.Add(relation)
            xmlDataSource1.Data = ds.GetXml
            myconn.Close()
        End Using
    End Sub
 
    Public Sub decidePage(ByVal mID As Integer)
        Using myconn1 As New SqlConnection(connStr)
            Dim sql As String = "select Text from menu where menuID = " & mID
            Dim cmd As New SqlCommand(sql, myconn1)
            myconn1.Open()
            Dim mtext As String = cmd.ExecuteScalar()
            myconn1.Close()
 
            If Strings.InStr(mtext, "Contact") > 0 Then
                'Server.Transfer("contactus.aspx")
                Response.Redirect("contactus.aspx?siteID=" & Session("siteID"))
            ElseIf Strings.InStr(mtext, "Parent") > 0 Then
                Response.Redirect("parents/page1.aspx?siteID=" & Session("siteID"))
            Else
                'Server.Transfer("contentECC.aspx")
                Response.Redirect("contentECC.aspx?siteID=" & Session("siteID"))
            End If
        End Using
    End Sub
 
    Protected Sub menu_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles menu.MenuItemClick
 
        Session("menuID") = menu.SelectedValue
        decidePage(Session("menuID"))
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Raju Srivatsavaye
Raju Srivatsavaye
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
hi
Did that no change
i have attached some screen shots for make the situation bit more clear.
.......
            xmlDataSource1.Data = ds.GetXml
            menu.DataBind()
            myconn.Close()
.......
you will notice in the first instance when i select kallngur i get the correct menu, that I GO BACK in the browser it still shows up the same menu, then if i click Parent login section, thats the time it refreshes to the correct menu, not otherwise. i have even tried to manually type the url again but that doesnt work either.
1.jpg
2.jpg
3.jpg
4.jpg
i have redone the page and this is how i achieved my dynamic menu, i forgo the XML method as it was proving to be a very unrealible and hard to debug.
using tis method is working for me so far with the only limitation that i have have one level of submenu for now, i am sure if need be it can be tweeked to get another level.
thanks to srivastavaya for your help.
....query database to get the rows from menu table to build the dataset
            
For Each item As DataRow In myDataSet.Tables(0).Rows
                Dim x As Integer = ManageSiteMenu.Items.IndexOf(ManageSiteMenu.FindItem(item(1).ToString))
                If item(1).ToString = "" Then
                    ManageSiteMenu.Items.Add(New MenuItem(item(2).ToString, item(0).ToString))
                Else
 
                    ManageSiteMenu.Items(x).ChildItems.Add(New MenuItem(item(2).ToString, item(0).ToString))
                End If
            Next

Open in new window

i forgo with the xml method and used a direct method to generate menu items dynamically.