sj1187534 Posted March 17, 2004 Posted March 17, 2004 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 Quote
kahlua001 Posted March 17, 2004 Posted March 17, 2004 Use the ItemDataBound event of the Datalist Quote
sj1187534 Posted March 17, 2004 Author Posted March 17, 2004 can u explain more on how to use the itemdatabound event? Thanks SJ Quote
tonyf Posted March 19, 2004 Posted March 19, 2004 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. Quote Everything is possible!!
sj1187534 Posted March 23, 2004 Author Posted March 23, 2004 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 Quote
tonyf Posted March 23, 2004 Posted March 23, 2004 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!! Quote Everything is possible!!
sj1187534 Posted March 23, 2004 Author Posted March 23, 2004 (edited) 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 Edited March 23, 2004 by sj1187534 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.