Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

DataList Access + Dynamic Template

I have a serious problem I have been unable to solve in the past 3 days! I have a DataList on my site but because I don't know what columns will be returned within the DataSet, I created a dynamic template class which is defined as follows:

Imports System.Web.UI
Imports System.Text
Imports System.Data

Public Class CRTItemTemplate
Implements ITemplate

Dim TemplateType As ListItemType
Dim xmlFile As String
Dim displayAL, dbAL As ArrayList
Dim lc As Literal
Dim ds As DataSet
Dim len As Integer
Shared panCount As Integer
Shared cbCount As Integer

Dim WithEvents pan As Panel

Sub New(ByVal type As ListItemType, ByVal xmlFile As String)
TemplateType = type
Me.xmlFile = xmlFile
Call Open()
panCount = 0
cbCount = 0
End Sub

Sub New(ByVal type As ListItemType, ByVal xmlFile As String, ByVal ds As DataSet)
TemplateType = type
Me.xmlFile = xmlFile
Me.ds = ds
Call Open()
panCount = 0
cbCount = 0
End Sub

'Extract the column names from DataSet columns collection
Sub Open()
If xmlFile.Equals(String.Empty) Then Exit Sub

'set the number of columns
len = ds.Tables(0).Columns.Count - 1

'Extract the column names from the DataSet and store into an ArrayList
Dim dc As DataColumn
displayAL = New ArrayList(13)

For Each dc In ds.Tables(0).Columns
displayAL.Add(dc.ColumnName)
Next
End Sub
Sub CreateHeader(ByVal container As Control)
Dim t As Table = New Table
t.ID = "DLTable"
t.HorizontalAlign = HorizontalAlign.Left
pan.Controls.Add(t)

Dim row As TableRow = New TableRow
row.BackColor = Color.SteelBlue
pan.Controls.Add(row)

Dim o As Object
For Each o In displayAL
Dim cell As TableCell = New TableCell
cell.Text = CType(o, String)
cell.ForeColor = Color.Black
cell.Font.Bold = True
row.Cells.Add(cell)
Next
End Sub
Sub AddItem(ByVal bgColor As Color, ByVal container As Control)
Dim row As TableRow = New TableRow
row.BackColor = bgColor
Dim cell As TableCell = New TableCell
Dim cb As CheckBox = New CheckBox
cb.ID = "cb" & cbCount.ToString
cb.ToolTip = "Check to add to Population"
Dim lb As LinkButton = New LinkButton

cell.Controls.Add(cb)
cell.Controls.Add(lb)
row.Cells.Add(cell)

'Add one cell with a label insde for each column (not counting the first)
Dim i As Integer
For i = 1 To len
cell = New TableCell
cell.ForeColor = Color.Black
cell.Controls.Add(New Label)
row.Cells.Add(cell)
Next

pan.Controls.Add(row)
End Sub

Sub InstantiateIn(ByVal container As Control) _
Implements ITemplate.InstantiateIn

If xmlFile.Equals(String.Empty) Then Exit Sub

pan = New Panel
pan.ID = "panel" & panCount.ToString

If TypeOf (container) Is WebControl Then
Dim wc As WebControl = DirectCast(container, WebControl)
pan.Width = wc.Width
pan.Height = wc.Height
End If

Select Case TemplateType
Case ListItemType.Header
Call CreateHeader(container)
Case ListItemType.Item
AddItem(Color.White, container)
panCount += 1
Case ListItemType.AlternatingItem
AddItem(Color.LightGray, container)
panCount += 1
Case ListItemType.SelectedItem
End Select

container.Controls.Add(pan)
End Sub

Private Sub Pan_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles pan.DataBinding
Dim data As Object

If TypeOf (pan.NamingContainer) Is DataListItem Then
data = DirectCast(pan.NamingContainer, DataListItem).DataItem
Else
Return
End If

' assumption is DataRowView since we're dealing with a DataList and not a DataReader
Dim drv As DataRowView = DirectCast(data, DataRowView)

Select Case TemplateType
Case ListItemType.Header
'if this is a header do nothing since the header has already been created within the CreateHeader() procedure.
Case ListItemType.Footer
'if this is a footer do nothing
Case ListItemType.SelectedItem
'handle selection
Case ListItemType.EditItem
Case ListItemType.Pager
Case ListItemType.Separator
Case Else
Dim o As Object
Dim i As Integer
For Each o In pan.Controls
If TypeOf o Is TableRow Then
Dim tr As TableRow = DirectCast(o, TableRow)
Dim cell As TableCell
For Each cell In tr.Cells
'get what is inside the cell
Dim cellobject As Object
For Each cellobject In cell.Controls
If TypeOf cellobject Is LinkButton Then
Dim lb As LinkButton = DirectCast(cellobject, LinkButton)
lb.Text = drv(i)
lb.CommandName = "select"
i = i + 1
ElseIf TypeOf cellobject Is Label Then
Dim l As Label = DirectCast(cellobject, Label)
l.Text = drv(i)
i = i + 1
End If
Next
Next
End If
Next
End Select
End Sub

End Class

As you can see, I place my controls (checkboxes, LinkButtons & Labels) within a Panel.

In the code-behind page that contains the DataList, I have an image button. When clicked, I want to iterate through the Items within the DataList and locate every checkbox that is checked.

How can I do this? I've tried the following:

Dim dli As DataListItem
For Each dli In DataList1.Items
Dim obj as Control
For Each obj in dli.Controls
If typeof obj is Panel then
Dim pan as Panel = Ctype(obj,Panel)
'iterate through the Panel control collection for the checkboxes

End If
Next
Next

The problem with the above code is that obj is never of type Panel. It's always of type LiteralControl. The question is why? Or better yet, how can I get access to the checkboxes.

Any help you can provide will be greatly appreciated. By the way, the code example I used to create my template class was taken from this site:

http://www.dev-archive.com/vb2themax/Article/19908/0/page/3

thanks,
Psion
[7933 byte] By [Psion] at [2007-11-11 7:26:14]
# 1 Re: DataList Access + Dynamic Template
Try enabling Trace on the Page, then look at the control tree that gets dumped at the bottom of the page. I suspect that the Panels are not contained directly by the DataListItem, but are within another container.
Phil Weber at 2007-11-11 23:13:48 >
# 2 Re: DataList Access + Dynamic Template
Here is the trace. It looks correct (if I'm not mistaken). The DataList contains the DataListItem which contains the Panel which contains the table.

Control Tree
Control Id Type Render Size Bytes (including children) Viewstate Size Bytes (excluding children)
__PAGE ASP.QueryResult_aspx 6281 40
_ctl0 System.Web.UI.ResourceBasedLiteralControl 589 0
Form1 System.Web.UI.HtmlControls.HtmlForm 5671 0
_ctl1 System.Web.UI.LiteralControl 5 0
Image2 System.Web.UI.WebControls.Image 158 0
txtHead System.Web.UI.WebControls.Label 245 224
Image1 System.Web.UI.WebControls.Image 165 0
cmdDebugXML System.Web.UI.WebControls.Button 287 36
cmdDebug System.Web.UI.WebControls.Button 281 36
Grid System.Web.UI.WebControls.DataGrid 0 20
txtNoData System.Web.UI.WebControls.Label 0 0
txtDebug System.Web.UI.WebControls.TextBox 0 0
cmdExcel System.Web.UI.WebControls.ImageButton 0 0
cmdHTML1 System.Web.UI.WebControls.ImageButton 0 0
cmdCSV1 System.Web.UI.WebControls.ImageButton 0 0
txtCount System.Web.UI.WebControls.Label 169 96
_ctl2 System.Web.UI.LiteralControl 250 0
statusLabel System.Web.UI.WebControls.Label 0 0
controlPanel System.Web.UI.WebControls.Panel 340 0
_ctl3 System.Web.UI.LiteralControl 17 0
addPopButton System.Web.UI.WebControls.ImageButton 146 36
_ctl4 System.Web.UI.LiteralControl 9 0
DataList1 System.Web.UI.WebControls.DataList 0 76
DataList1:_ctl0 System.Web.UI.WebControls.DataListItem 0 0
DataList1:_ctl0: panel0 System.Web.UI.WebControls.Panel 0 0
DataList1:_ctl0: DLTable System.Web.UI.WebControls.Table 0 0
DataList1:_ctl0:_ctl0 System.Web.UI.WebControls.TableRow 0 0
DataList1:_ctl0:_ctl1 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl0:_ctl2 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl0:_ctl3 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl0:_ctl4 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl0:_ctl5 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl0:_ctl6 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl1 System.Web.UI.WebControls.DataListItem 0 0
DataList1:_ctl1: panel0 System.Web.UI.WebControls.Panel 0 0
DataList1:_ctl1:_ctl6 System.Web.UI.WebControls.TableRow 0 0
DataList1:_ctl1:_ctl7 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl1:cb0 System.Web.UI.WebControls.CheckBox 0 0
DataList1:_ctl1:_ctl0 System.Web.UI.WebControls.LinkButton 0 80
DataList1:_ctl1:_ctl8 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl1:_ctl1 System.Web.UI.WebControls.Label 0 36
DataList1:_ctl1:_ctl9 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl1:_ctl2 System.Web.UI.WebControls.Label 0 44
DataList1:_ctl1:_ctl10 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl1:_ctl3 System.Web.UI.WebControls.Label 0 36
DataList1:_ctl1:_ctl11 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl1:_ctl4 System.Web.UI.WebControls.Label 0 48
DataList1:_ctl1:_ctl12 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl1:_ctl5 System.Web.UI.WebControls.Label 0 40
DataList1:_ctl2 System.Web.UI.WebControls.DataListItem 0 0
DataList1:_ctl2: panel1 System.Web.UI.WebControls.Panel 0 0
DataList1:_ctl2:_ctl6 System.Web.UI.WebControls.TableRow 0 0
DataList1:_ctl2:_ctl7 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl2:cb0 System.Web.UI.WebControls.CheckBox 0 0
DataList1:_ctl2:_ctl0 System.Web.UI.WebControls.LinkButton 0 72
DataList1:_ctl2:_ctl8 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl2:_ctl1 System.Web.UI.WebControls.Label 0 52
DataList1:_ctl2:_ctl9 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl2:_ctl2 System.Web.UI.WebControls.Label 0 48
DataList1:_ctl2:_ctl10 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl2:_ctl3 System.Web.UI.WebControls.Label 0 36
DataList1:_ctl2:_ctl11 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl2:_ctl4 System.Web.UI.WebControls.Label 0 44
DataList1:_ctl2:_ctl12 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl2:_ctl5 System.Web.UI.WebControls.Label 0 56
DataList1:_ctl3 System.Web.UI.WebControls.DataListItem 0 0
DataList1:_ctl3: panel2 System.Web.UI.WebControls.Panel 0 0
DataList1:_ctl3:_ctl6 System.Web.UI.WebControls.TableRow 0 0
DataList1:_ctl3:_ctl7 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl3:cb0 System.Web.UI.WebControls.CheckBox 0 0
DataList1:_ctl3:_ctl0 System.Web.UI.WebControls.LinkButton 0 68
DataList1:_ctl3:_ctl8 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl3:_ctl1 System.Web.UI.WebControls.Label 0 36
DataList1:_ctl3:_ctl9 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl3:_ctl2 System.Web.UI.WebControls.Label 0 56
DataList1:_ctl3:_ctl10 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl3:_ctl3 System.Web.UI.WebControls.Label 0 36
DataList1:_ctl3:_ctl11 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl3:_ctl4 System.Web.UI.WebControls.Label 0 44
DataList1:_ctl3:_ctl12 System.Web.UI.WebControls.TableCell 0 0
DataList1:_ctl3:_ctl5 System.Web.UI.WebControls.Label 0 56
_ctl5 System.Web.UI.LiteralControl 4 0
_ctl6 System.Web.UI.LiteralControl 21 0

Any other hints/suggestions you have, please give it to me. I'm about to jump off the roof at this point. Can't believe I'm having such problems with such a simple thing. I don't know why this isn't indenting when I post it.

Sorry, I don't know why this isn't indenting.
Psion at 2007-11-11 23:14:48 >