ASP.NET Dynamic Repeater in VB.NET

Public Class SubCategoriesTemplate    Implements System.Web.UI.ITemplate    Dim templateType As ListItemType    Sub New(ByVal type As ListItemType)        templateType = type    End Sub    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) _      Implements System.Web.UI.ITemplate.InstantiateIn        Dim plcGeneral As New PlaceHolder()        Dim plcItemPlaceholder As New PlaceHolder()        plcItemPlaceholder.ID = "plcCategory"        Dim plcHeaderPlaceholder As New PlaceHolder()        plcHeaderPlaceholder.ID = "plcCategoryHeader"        Dim plcFooterPlaceholder As New PlaceHolder()        plcFooterPlaceholder.ID = "plcCategoryFooter"        Select Case (templateType)            Case ListItemType.Header                plcGeneral.Controls.Add(plcHeaderPlaceholder)            Case ListItemType.Item                plcGeneral.Controls.Add(plcItemPlaceholder)            Case ListItemType.AlternatingItem            Case ListItemType.Footer                plcGeneral.Controls.Add(plcFooterPlaceholder)        End Select        container.Controls.Add(plcGeneral)    End SubEnd Class

Markup

    <ul class="secondary">        <asp:PlaceHolder ID="plcSecondaryCategories" runat="server" />    </ul

Usage

Dim rptSecondaryCategories As New RepeaterrptSecondaryCategories.ID = String.Format("rpt{0}", channelCategory.Name.Replace(" ", "-"))rptSecondaryCategories.HeaderTemplate = New SubCategoriesTemplate(ListItemType.Header)rptSecondaryCategories.ItemTemplate = New SubCategoriesTemplate(ListItemType.Item)rptSecondaryCategories.FooterTemplate = New SubCategoriesTemplate(ListItemType.Footer)plcSecondaryCategories.Controls.Add(rptSecondaryCategories)rptSecondaryCategories.DataSource = From category In SubCategories Where category.ParentCategory = channelCategory.Name Select categoryAddHandler rptSecondaryCategories.ItemDataBound, AddressOf rptSecondaryCategories_OnItemDataBoundrptSecondaryCategories.DataBind()

Simple OnITemDataBound usage

Protected Sub rptSecondaryCategories_OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)    If e.Item.ItemType = ListItemType.Header Then        Dim plcCategoryHeader As PlaceHolder = CType(e.Item.FindControl("plcCategoryHeader"), PlaceHolder)        plcCategoryHeader.Controls.Add(New LiteralControl(String.Format("<ul class=""{0}{1}"">", CurrentCategoryParent.ToLower().Replace(" ", "-"), IIf(CurrentCategoryParent = SelectedCategoryParent, " active", String.Empty))))    ElseIf e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then        Dim channelCategory As ChannelCategory = CType(e.Item.DataItem, ChannelCategory)        Dim plcCategory As PlaceHolder = CType(e.Item.FindControl("plcCategory"), PlaceHolder)        Dim hlCategory As New HyperLink        Dim url As String = CurrentChannelURL.GetChannelCategoryUrl(channelCategory.Name, Request.Url.AbsoluteUri)        plcCategory.Controls.Add(New LiteralControl(String.Format("<li class=""{0}{1}"">", CurrentChannelURL.ParseCategoryName(channelCategory.Name, True).ToLower().Replace(" ", "-"), IIf(url.Replace("~", String.Empty) = CurrentChannelURL.CurrentChannelCategoryURL, " active", String.Empty))))        hlCategory.NavigateUrl = CurrentChannelURL.GetChannelCategoryUrl(channelCategory.Name, Request.Url.AbsoluteUri)        hlCategory.Text = channelCategory.Name        plcCategory.Controls.Add(hlCategory)        plcCategory.Controls.Add(New LiteralControl("</li>"))    ElseIf e.Item.ItemType = ListItemType.Footer Then        Dim plcCategoryFooter As PlaceHolder = CType(e.Item.FindControl("plcCategoryFooter"), PlaceHolder)        plcCategoryFooter.Controls.Add(New LiteralControl(" </ul>"))    End IfEnd Sub

 

Leave a Reply

Your email address will not be published. Required fields are marked *