Hi guys,
I am stuck on my code. I am creating Repeater control dynamically. For some reason i cant really get it to work, Maybe someone can tell where i am going wrong. I have one class name "MultipleSelectCategoryRepeaterTemplate" this class will create the repeater Header, Item and Footer dynamic. In its item template it has another dynamic repeater. This inner repeater i cant bind its values.
Public Class MultipleSelectCategoryRepeaterTemplate
Implements ITemplate
Private _type As ListItemType
Private _level As Integer
Private _headerName As String
Private Shared _lstMultipleSelectCategories As List(Of MultipleSelectCategory)
Public Sub New(ByVal type As ListItemType, ByVal intLevel As Integer, ByVal strHeaderName As String, ByVal lstCategory As List(Of MultipleSelectCategory))
_type = type
_level = intLevel
_headerName = strHeaderName
_lstMultipleSelectCategories = lstCategory
End Sub
Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn
Dim ph As New PlaceHolder
Select Case _type
Case ListItemType.Header
Dim strHtml As StringBuilder = New StringBuilder()
strHtml.AppendFormat("<ul>")
strHtml.AppendFormat("<li class='list-item-all'>")
strHtml.AppendFormat(" <div class='checkbox'>")
strHtml.AppendFormat(" <label>")
strHtml.AppendFormat(" <asp:CheckBox runat='server' ID='cbxOption' data-toggle='CheckBoxAll'/>{0}", _headerName)
strHtml.AppendFormat(" </label>")
strHtml.AppendFormat(" </div>")
strHtml.AppendFormat("</li>")
ph.Controls.Add(New LiteralControl(strHtml.ToString()))
Case ListItemType.Item
Dim li As New HtmlGenericControl With {.ID = "liItem", .TagName = "li"}
Dim dvCheckBoxWrapper As New HtmlGenericControl With {.TagName = "div"}
Dim lbl As New HtmlGenericControl With {.TagName = "Label"}
dvCheckBoxWrapper.Attributes.Add("class", "checkbox")
lbl.Controls.Add(New CheckBox() With {.ID = "cbxOption"})
lbl.Controls.Add(New Label() With {.ID = "lblOptionText"})
dvCheckBoxWrapper.Controls.Add(lbl)
li.Controls.Add(dvCheckBoxWrapper)
ph.Controls.Add(li)
Dim rptList As New Repeater() With {.ID = "rptList"}
rptList.HeaderTemplate = New MultipleSelectCategoryListRepeaterTemplate(ListItemType.Header, _level + 1)
rptList.ItemTemplate = New MultipleSelectCategoryListRepeaterTemplate(ListItemType.Item, _level + 1)
rptList.FooterTemplate = New MultipleSelectCategoryListRepeaterTemplate(ListItemType.Footer, _level + 1)
ph.Controls.Add(rptList)
AddHandler ph.DataBinding, New EventHandler(AddressOf MultipleSelectCategory_ItemDataBound)
Case ListItemType.Footer
Dim strHtml As StringBuilder = New StringBuilder()
strHtml.AppendFormat("</ul>")
ph.Controls.Add(New LiteralControl(strHtml.ToString()))
End Select
container.Controls.Add(ph)
End Sub
Public Sub MultipleSelectCategory_ItemDataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim ph As PlaceHolder = CType(sender, PlaceHolder)
Dim ri As RepeaterItem = CType(ph.NamingContainer, RepeaterItem)
Dim rptList As Repeater = Nothing
Dim mscItem As MultipleSelectCategory = ri.DataItem
Dim strHtml As StringBuilder = New StringBuilder()
If mscItem.ChildCategory Is Nothing Then
Dim lbl As Label = TryCast(ri.FindControl("lblOptionText"), Label)
rptList = TryCast(ri.FindControl("rptList"), Repeater)
If Not lbl Is Nothing Then
lbl.Text = mscItem.CategoryName
End If
If Not rptList Is Nothing Then
rptList.DataSource = mscItem.List
rptList.DataBind()
End If
Else
End If
End Sub
End Class
Dynamic Inner Repeater
Public Class MultipleSelectCategoryListRepeaterTemplate
Implements ITemplate
Private _type As ListItemType
Private _level As Integer
Public Sub New(ByVal type As ListItemType, ByVal intLevel As Integer)
_type = type
_level = intLevel
End Sub
Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn
Dim ph As New PlaceHolder
Select Case _type
Case ListItemType.Header
Dim strHtml As StringBuilder = New StringBuilder()
strHtml.AppendFormat("<ul>")
ph.Controls.Add(New LiteralControl(strHtml.ToString()))
Case ListItemType.Item
ph.Controls.Add(New LiteralControl("<li>"))
ph.Controls.Add(New LiteralControl("<div class='checkbox'>"))
ph.Controls.Add(New LiteralControl("<label>"))
ph.Controls.Add(New CheckBox() With {.ID = "cbxOption"})
ph.Controls.Add(New Label() With {.ID = "lblOptionText"})
ph.Controls.Add(New LiteralControl("</label>"))
ph.Controls.Add(New LiteralControl("</div>"))
ph.Controls.Add(New LiteralControl("</li>"))
AddHandler ph.DataBinding, New EventHandler(AddressOf MultipleSelectItem_ItemDataBound)
Case ListItemType.Footer
Dim strHtml As StringBuilder = New StringBuilder()
strHtml.AppendFormat("</ul>")
ph.Controls.Add(New LiteralControl(strHtml.ToString()))
End Select
End Sub
Public Sub MultipleSelectItem_ItemDataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim ph As PlaceHolder = CType(sender, PlaceHolder)
Dim ri As RepeaterItem = CType(ph.NamingContainer, RepeaterItem)
Dim lbl As Label = TryCast(ri.FindControl("lblOptionText"), Label)
Dim item As ListItem = ri.DataItem
If Not lbl Is Nothing Then
lbl.Text = item.Text
End If
End Sub
End Class
Populating Outer Repeater
rptMultipleSelectCategory.HeaderTemplate = New MultipleSelectCategoryRepeaterTemplate(ListItemType.Header, 1, "All", lstMultipleSelectCategories)
rptMultipleSelectCategory.ItemTemplate = New MultipleSelectCategoryRepeaterTemplate(ListItemType.Item, 1, "All", lstMultipleSelectCategories)
rptMultipleSelectCategory.FooterTemplate = New MultipleSelectCategoryRepeaterTemplate(ListItemType.Footer, 1, "All", lstMultipleSelectCategories)
rptMultipleSelectCategory.DataSource = lstMultipleSelectCategories
rptMultipleSelectCategory.DataBind()
upnlMultipleSelectCategory.Update()