Iterating a DataList Issues

P

Prince

I created a class that implements. The class is defines as:

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) in the DataSet
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)
'panCount += 1
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


In my code-behind page, I assigned the class to the DataList like so:

DataList1.DataSource = ds
DataList1.HeaderTemplate = New CRTItemTemplate(ListItemType.Header,
QueryPath, ds)
DataList1.ItemTemplate = New CRTItemTemplate(ListItemType.Item, QueryPath, ds)
DataList1.AlternatingItemTemplate = New
CRTItemTemplate(ListItemType.AlternatingItem, QueryPath, ds)


where ds = the DataSet

I use a template class because I don't know what columns are being returned.

Problem: In the code-behind page, I have a button (not in the datalist)
that when pressed, I want it to iterate through the DataListItems for the
checkboxes. There should be one for each row.

As you can see from the code above, I create a Panel and add all the
controls to the Panel. However, I can't get a reference to the Panels within
the DataList. If I could, I could then iterate through the controls in the
Panel and get the CheckBoxes. Everytime I look at the controls within the
DataListItem, it returns a LiteralControl object instead of a Panel. why?

How can I get to that Panel or checkbox that's within the Panel?

-- thanks,
Prince
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top