locked
Redirection in Programaticly created Menu in vb.net RRS feed

  • Question

  • User-1578974752 posted

    In the below code, the 2 Menu (ABC and Test ) is showing in the Pageload.But when I either ABC or Test both are redirecting to Hardware.aspx.

    How can I modify such that If ABC is clicked will redirect to Hardware.aspx and IF menu Test is clicked will go to Software.aspx. Appreciate the Help

    Dim subMenu2 = New MenuItem("ABC")

    Dim items1 As ArrayList = fetchFromDBQSC()

    For index = 0 To items1.Count - 1

    subMenu2.ChildItems.Add(New MenuItem(items1.Item(index), "Value"))

    Next

    Menu1.Items.Add(subMenu2)

     

    'End If

    cmd.Dispose()

    DRD.Close()

     

    Catch ex As Exception

    ' Label2.Text = ex.Message()

    End Try

     

    'Dim subMenu = New MenuItem("Test")

    Dim subMenu = New MenuItem("TEST")

    Dim items As ArrayList = fetchFromDB()

    For index = 0 To items.Count - 1

    subMenu.ChildItems.Add(New MenuItem(items.Item(index), "Value"))

    Next

    Menu1.Items.Add(submenu)

    Private Sub Menu1_MenuItemClick(sender As Object, e As MenuEventArgs) Handles Menu1.MenuItemClick

    Response.Redirect("Hardware.aspx")

     

    End Sub

    Friday, October 5, 2018 2:06 AM

Answers

  • User-893317190 posted

    Hi shsu,

    You should add NavigateUrl property to your  MenuItem object insteadof ChildItems.

     If Not IsPostBack Then
                Dim subMenu = New MenuItem("TEST")
    
                Dim items As ArrayList = New ArrayList()
                items.Add("item1")
                items.Add("item2")
    
                For index = 0 To items.Count - 1
                    Dim item As MenuItem = New MenuItem(items.Item(index), "Value")
                    item.NavigateUrl = "yourPath"
                    subMenu.ChildItems.Add(item)
    
                Next
    
                Menu1.Items.Add(subMenu)
    
            End If

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 5, 2018 8:50 AM

All replies

  • User-893317190 posted

    Hi shsu,

    If you want to use menu to navigate to another  page, you should use NavigateUrl property.Please set its value to the path of the page you want and you needn't to add Menu1_MenuItemClick event.

       Dim menu = New Menu()
            Dim sub1 = New MenuItem("toPage1")
            sub1.NavigateUrl = "/Page1"
            Dim sub2 = New MenuItem("toPage2")
            sub1.NavigateUrl = "/Page2"
    
            menu.Items.Add(sub1)
            menu.Items.Add(sub2)
            Form.Controls.Add(menu)

    The result.

    Best regards,

    Ackerly Xu

    Friday, October 5, 2018 5:39 AM
  • User-1578974752 posted

    when I add a below, instead of going to another page new menu is creating. If I type response.redirect in

    Menu1_MenuItemClick it is working bot I have 2 menu both going to same page.In  Menu1_MenuItemClick, any changes can be done?

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

    Dim subMenu = New MenuItem("TEST")

    Dim items As ArrayList = fetchFromDB()

    subMenu.NavigateUrl = "password.aspx"

    For index = 0 To items.Count - 1

    subMenu.ChildItems.Add(New MenuItem(items.Item(index), "Value"))

    Next

    Menu1.Items.Add(submenu)

    Friday, October 5, 2018 6:41 AM
  • User-893317190 posted

    Hi shsu,

    The navigation only applies to the item which has NavigateUrl.In you case , only when you click the subMenu ,it will go to another page. When you click subMenu's Child menu , it only posts back.

    If you want your child item to navigate , you should  add NavigateUrl to it.

    About creating new menu,you should put your code in If Not IsPostBack

     If Not IsPostBack Then
                Dim subMenu = New MenuItem("TEST")
    
                Dim items As ArrayList = New ArrayList()
                items.Add("item1")
                items.Add("item2")
    
                subMenu.NavigateUrl = "password.aspx"
    
                For index = 0 To items.Count - 1
    
                    subMenu.ChildItems.Add(New MenuItem(items.Item(index), "Value"))
    
                Next
    
                Menu1.Items.Add(subMenu)
    
            End If

    Best regards,

    Ackerly Xu

    Friday, October 5, 2018 7:13 AM
  • User-1578974752 posted

    I want the child item when click to be navigated.

    subMenu.ChildItems.NavigateUrl = "/Network"  this one is showing error.How can put the natigation url in the child item.Thanks

    If Not IsPostBack Then

     

    Dim subMenu = New MenuItem("TEST")

    Dim items As ArrayList = fetchFromDB()

     

    For index = 0 To items.Count - 1

    subMenu.ChildItems.Add(New MenuItem(items.Item(index), "Value"))

    subMenu.ChildItems.NavigateUrl = "/Network"

    Next

    Menu1.Items.Add(subMenu)

    End If

    Friday, October 5, 2018 7:46 AM
  • User-893317190 posted

    Hi shsu,

    You should add NavigateUrl property to your  MenuItem object insteadof ChildItems.

     If Not IsPostBack Then
                Dim subMenu = New MenuItem("TEST")
    
                Dim items As ArrayList = New ArrayList()
                items.Add("item1")
                items.Add("item2")
    
                For index = 0 To items.Count - 1
                    Dim item As MenuItem = New MenuItem(items.Item(index), "Value")
                    item.NavigateUrl = "yourPath"
                    subMenu.ChildItems.Add(item)
    
                Next
    
                Menu1.Items.Add(subMenu)
    
            End If

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 5, 2018 8:50 AM
  • User-1578974752 posted

    Thanks for the help

    Friday, October 5, 2018 9:11 AM
  • User-893317190 posted

    I'm  glad to see you have solved your problem.

    Friday, October 5, 2018 9:20 AM