Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Datalist itemdatabound problem c#

Posted on 2006-07-10
2
Medium Priority
?
3,040 Views
Last Modified: 2011-09-20
Hello experts,

I have binding data to me datalist from codebehind.
I need to grab the contents of a label in the datalist, make some calculations and rebind the result to the label.

But my datalist count method returns 0 even if there there are records returned, so this does not work;-

 for (int i = 0; i < DataList1.Items.Count; i++)
{
//calcualte stuff
}

and so my calcualtion are not taking place

Help!!!
0
Comment
Question by:claracruz
2 Comments
 
LVL 5

Expert Comment

by:GENTP
ID: 17076345
I think you're trying to do this with the control's name, as opposed to the argument past in's name. Also, on the item bound, you have one complete row, not a column, so you'll need to use the cells property too. Try this:

private void dgHistory_ItemBound(object source, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
     for(int i=0;i<e.Item.Cells[i].Count; i++)
     {
          //do stuff to each cell going across the line
          //use the text property and System.Convert to do the math you need to do.
     }
}
0
 
LVL 9

Accepted Solution

by:
bele04 earned 2000 total points
ID: 17078360
The datalist's ItemDataBound event is fired after an item in a datalist is binded to a certain data but before it is displayed/rendered to the page so you can still make changes to the datalist prior to rendering it.  So let's say you have 3 items in your datalist, the ItemDataBound event therefore is fired 3 times, once for each item.

So if you want to change a label in your datalist then you do not need to iterate through the entire list of items in the datalist since not all of them has been databounded yet.  You can do something like this instead:

private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
    Label label1 = (Label) e.Item.FindControl("<name of your label");
    //calculating code here...
}

If you want to get other items in your datalist then you can just use the FindControl method to get an instance of the control object and just type cast it to the proper webcontrol type.

hope that helps...
-bele04-
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question