Solved

Datalist itemdatabound problem c#

Posted on 2006-07-10
2
3,021 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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