Load datalist programmatically

sj1187534

Centurion
Joined
Jun 10, 2003
Messages
108
Location
Dallas, Houston, etc.etc.
Hi...I am trying to load data from the database into a datalist. I want to do this programmatically instead of using the <%# Databinder.Eval(Container.DataItem,"") %>.
The datalist has some controls in it, like labels where the data need to be loaded. Thats because I need to modify some data that I get from the database, before I load it into the datalist. Any ideas?

SJ
 
Using the itemdatabound event:

Obviously you have created your template in the datalist. So say you have added a label named "label1" to your item template.

In the itemdataboundevent you would type the following code:

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim lb As Label
lb = CType(e.Item.FindControl("Label1"), Label)
lb.Text = "whatever you want to put here"
End If

The lb now relates to the label in your datalist template, so if you wanted to assign it the value from the database then just add the reference from the table required etc.
 
Hi..What if we want to load the label from the database? In your example, you are loading it with a constant value. If the datalist is loaded from the dataset, how do we use it to show a specific value in the label? Thanks

SJ
 
In my previous example, you just need to assign the text property of the label to the relevant fiend in the datatable/dataset etc e.g.


If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim lb As Label
lb = CType(e.Item.FindControl("Label1"), Label)
lb.Text = dataset.tables(0).rows(e.item.itemindex)("field name etc").tostring
End If


If you need to know the exact coding then post your code here!!
 
Hi....Thanks for the reply. I have one question though. I understood that we can use the itemindex to load the label from the dataset. But, that is not what I meant in my previous post. What I am trying to comprehend is how do we pass the dataset to the ItemDataBound method of the datalist. In your example, you are just using the "dataset" directly but where does it come from. Is it the same dataset that we used to bind the datalist??? Or is it something else. If it is the same, do we have to go with the same name of the dataset?

The only code I have is what you gave me in your previous post!!

I am really confused here.

SJ
 
Last edited:
Back
Top