DataRow

R

rn5a

A DataList has a Label control within the ItemTemplate &
AlternatingItemTemplate. The DataList is populated using a SQL query
wherein the 5th column is a column named 'Description'. The following
code resides in the OnItemDataBound event of the DataList:

Sub BindDataItem(ByVal obj As Object, ByVal ea As
DataListItemEventArgs)
Dim i As Integer
Dim dSet As DataSet
Dim dRow As DataRow
Dim sqlDapter As SqlDataAdapter

dSet = New DataSet
sqlDapter = New SqlDataAdapter

sqlDapter = ViewOrder(iUID, iOID)
sqlDapter.Fill(dSet)

If (ea.Item.ItemType = ListItemType.Item Or ea.Item.ItemType =
ListItemType.AlternatingItem) Then
For i = 0 To dSet.Tables(0).Rows.Count - 1
CType(ea.Item.FindControl("lbl1"), Label).Text =
dSet.Tables(0).Rows(i).Item(5).ToString
Next
End If
End Sub

Assume that the SQL query retrieves 3 records & the 3 records under the
5th column are "I am big", "I am bigger" & "I am biggest".

But the above code displays the record "I am biggest" in all the 3 rows
in the DataList. How do I make the DataList display "I am big" in the
first row, "I am bigger" in the second row & "I am biggest" in the
third row?

Please note that I could have easily accomplished this by setting the
Text property of the Label control to <%#
Container.DataItem("Description") %> in the DataList & binding the
DataList to the DataSet in the Page_Load sub but I did like to know how
to do the same in the OnItemDataBound event handler.
 
K

Karl Seguin

wouldn't you need to do something like:

ea.Item.FindControl("lbl" & i.ToString()) = XXX

and have a lbl0, lbl1 and lbl2 or something? You could dynamically add
them...

dim ph as placeHolder = ctype(ea.Item.FindControl("ph"), PlaceHolder)
for ...
dim l as new Label()
l.Text = dsSet.Tables(0)....
ph.Controls.Add(l)
next


On a side note, there's no point in calling ViewOrder and .Fill() unless the
itemType is Item or AlternatingItem (i.e., move all of your code INSIDE the
IF statement)...or write a guard clause:

if ea.Item.ItemType != Item AndAlso ea.Item.ItemType != AlternatingItem then
return
end if

Karl
 

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

Similar Threads

Show "No Records Found" Message 1
DropDownList DataGrid 1
DB Value in Label 1
DataRow Delete Method 5
DataRow? 1
Default Selected Item in DropDownList 4
Update DataRow 2
Insert New Record 3

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top