This is a migrated thread and some comments may be shown as answers.

How to access controls inside Radmenu Dynamic Template

2 Answers 99 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Zafar
Top achievements
Rank 1
Zafar asked on 09 Mar 2012, 08:32 AM
Dear Sir,

I am unable to access controls inside a radmenu dynamic template. I am want to handle radlistbox selectedindexchanged event or access selectedvalue property when the menu item is clicked. Please help 

Private Sub mnuRoom_ItemClick(sender As Object, e As Telerik.Web.UI.RadMenuEventArgs) Handles mnuRoom.ItemClick
        If e.Item.Level = 0 Then
            Response.Redirect("~/Pages/Products.aspx?category=" & e.Item.Value)
        Else
            Dim lstCat As RadListBox = DirectCast(e.Item.FindControl("lstCategories"), RadListBox)
            Response.Redirect("~/Pages/Products.aspx?category=" & lstCat.SelectedValue)
        End If
    End Sub


   Private Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
Using dbContext As New familia.OpenAccess.EntitiesModel
            mnuRoom.Items.Clear()
            Dim room As IQueryable(Of ProductCategory)
            room = From r In dbContext.ProductCategories Where r.IsActive = True And r.ParentCategory = 0 Select r
            For Each r In room
                Dim roomitem As New Telerik.Web.UI.RadMenuItem
                roomitem.DataItem = r
                roomitem.Value = r.CategoryID
                mnuRoom.Items.Add(roomitem)
                Dim mainmenuItemTemplate As New MainMenuItemTemplate()
                mainmenuItemTemplate.InstantiateIn(roomitem)
                Dim childItem As New RadMenuItem()
                childItem.CssClass = "mainmenudropdown"
                childItem.ItemTemplate = New MenuItemTemplate()
 roomitem.Items.Add(childItem)
Next
        End Using
End Sub



Class MenuItemTemplate
    Implements ITemplate
#Region "ITemplate Members"

    Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn
        Dim parentItem As RadMenuItem = DirectCast(container.Parent, RadMenuItem)
        Dim room As ProductCategory = DirectCast(parentItem.DataItem, ProductCategory)
        'Dim div As New HtmlGenericControl("div")

        Dim contents As New Panel

       contents.CssClass = "menucontents"
        container.Controls.Add(contents)

        Dim categories As New Panel
        categories.CssClass = "menucategorypanel"

        Dim l As New Label
        l.ForeColor = System.Drawing.ColorTranslator.FromHtml("#1B75BC")
        l.Font.Size = 14
        l.Font.Bold = True
        l.Text = room.CategoryName

        categories.Controls.Add(l)

        Dim cat As New RadListBox

        cat.CssClass = "menucategorylist"
        Using dbContext As New familia.OpenAccess.EntitiesModel
room.RoomID))
            cat.DataSource = From c In dbContext.ProductCategories Where c.IsActive = True And c.ParentCategory = room.CategoryID Select c
            cat.DataTextField = "CategoryName"
            cat.DataValueField = "CategoryID"
            cat.ID = "lstCategories"
            cat.Width = 231
            cat.DataBind()

      End Using
       AddHandler cat.SelectedIndexChanged, New EventHandler(AddressOf lstCategories_SelectedIndexChanged)
        cat.Font.Size = 12
        categories.Controls.Add(cat)

        contents.Controls.Add(categories)

        Dim brands As New Panel
        brands.CssClass = "menubrandpanel"

        Dim b As New Label
        b.ForeColor = System.Drawing.ColorTranslator.FromHtml("#6B6B6B")
        b.Font.Size = 14
        b.Font.Bold = True
        b.Text = "TOP BRANDS"

        brands.Controls.Add(b)

        Dim bnd As New RadListBox
        bnd.CssClass = "menucategorylist"
        Using dbContext As New familia.OpenAccess.EntitiesModel
            bnd.DataSource = dbContext.ProductBrands.Where(Function(c) c.TopBrand = True And c.ProductCategories.Any(Function(r) r.CategoryID = room.CategoryID))

            bnd.DataTextField = "BrandName"
            bnd.DataValueField = "BrandID"
            bnd.Width = 231
            bnd.DataBind()
        End Using

        bnd.Font.Size = 12
        brands.Controls.Add(bnd)

        contents.Controls.Add(brands)

        Dim bottom As New Panel

        bottom.CssClass = "menucategorybottom"

        Dim bimg As New RadBinaryImage
        bimg.ImageUrl = "/images/layout/brand.png"
        bimg.ResizeMode = BinaryImageResizeMode.Fit
        bimg.Width = 20
        bimg.Height = 20
        bimg.CssClass = "buttonimage"
        bottom.Controls.Add(bimg)
        Dim brand As New RadButton
        brand.ButtonType = RadButtonType.LinkButton
        brand.CssClass = "brandbutton"
        brand.Font.Bold = True
        brand.Font.Size = 10
        brand.Text = "All Brands"
        bottom.Controls.Add(brand)

        Dim bdimg As New RadBinaryImage
        bdimg.ImageUrl = "/images/layout/BestDeals.png"
        bdimg.ResizeMode = BinaryImageResizeMode.Fit
        bdimg.Width = 20
        bdimg.Height = 20
        bdimg.CssClass = "buttonimage"

        bottom.Controls.Add(bdimg)

        Dim deals As New RadButton
        deals.ButtonType = RadButtonType.LinkButton

        deals.CssClass = "brandbutton"
        deals.Font.Bold = True
        deals.Font.Size = 10
        deals.Text = "Best Deals"
        bottom.Controls.Add(deals)

        Dim psimg As New RadBinaryImage
        psimg.ImageUrl = "/images/layout/Premimum.png"
        psimg.ResizeMode = BinaryImageResizeMode.Fit
        psimg.Width = 20
        psimg.Height = 20
        psimg.CssClass = "buttonimage"

        bottom.Controls.Add(psimg)

        Dim pre As New RadButton
        pre.ButtonType = RadButtonType.LinkButton
        pre.CssClass = "brandbutton"
        pre.Font.Bold = True
        pre.Font.Size = 10
        pre.Text = "Premium Selections"
        bottom.Controls.Add(pre)

        Dim nimg As New RadBinaryImage
        nimg.ImageUrl = "/images/layout/new.png"
        nimg.ResizeMode = BinaryImageResizeMode.Fit
        nimg.Width = 20
        nimg.Height = 20
        nimg.CssClass = "buttonimage"

        bottom.Controls.Add(nimg)

        Dim ne As New RadButton
        ne.ButtonType = RadButtonType.LinkButton
        ne.CssClass = "brandbutton"
        ne.Font.Bold = True
        ne.Font.Size = 10
        ne.Text = "Recently Added"
        bottom.Controls.Add(ne)
        contents.Controls.Add(bottom)

  End Sub

    Private Sub lstCategories_SelectedIndexChanged(sender As Object, e As System.EventArgs)

    End Sub
#End Region
End Class

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 09 Mar 2012, 10:16 AM
Hello,

Try the following code.
VB:
Protected Sub Page_Load(sender As Object, e As EventArgs)
    Dim template As New TextBoxTemplate()
    For Each item As RadMenuItem In RadMenu1.GetAllItems()
        If item.Level > 0 Then
            template.InstantiateIn(item)
        End If
    Next
    RadMenu1.DataBind()
End Sub
 
Protected Sub RadMenu1_ItemClick(sender As Object, e As RadMenuEventArgs)
    If RadMenu1.SelectedItem.Level > 0 Then
        Dim listbox As RadListBox = DirectCast(RadMenu1.SelectedItem.FindControl("RadListBox1"), RadListBox)
        Dim s As String = listbox.SelectedValue
    End If
End Sub
 
 Class TextBoxTemplate
    Implements ITemplate
    Public Sub InstantiateIn(container As Control)
        Dim RadListBox1 As New RadListBox()
        RadListBox1.ID = "RadListBox1"
        RadListBox1.AutoPostBack = True
        RadListBox1.SelectedIndexChanged += New EventHandler(RadListBox1_SelectedIndexChanged)
        RadListBox1.DataTextField = "EmployeeID"
        RadListBox1.DataValueField = "EmployeeID"
        RadListBox1.DataSourceID = "SqlDataSource1"
        container.Controls.Add(RadListBox1)
    End Sub
 
    Private Sub RadListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim lst As RadListBox = DirectCast(sender, RadListBox)
        Dim ee As String = lst.SelectedValue
    End Sub
End Class

Thanks,
Princy.
0
Zafar
Top achievements
Rank 1
answered on 09 Mar 2012, 12:49 PM
Thanks for the reply. The code provided by you is not working. Please review the code posted by me and suggest a solution. thanks

Instead of using template I have also tried adding controls to controls collection of radmenuitem in Page_PreRender event but when I try access  controls collection in radmenu itemclick event, its show zero controls. Suggest a solution please.
Tags
Menu
Asked by
Zafar
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Zafar
Top achievements
Rank 1
Share this question
or