Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Findcontrol with a datalist

Status
Not open for further replies.

Dimitrie1

Programmer
Jan 5, 2007
138
CA
I know this has been written about but I still can't make it work - please help.

I have a datalist with fees, the last row being the total. 2 labels per row. All I want is the 2nd label of the last line.

In the itemdatabound event i've coded
Code:
Dim xx As String = CType(e.Item.FindControl("Label25"), Label).Text

it doesn't like the label. I haven't attempted to grab only the last row yet as I'm trying 1 piece at a time.

I've also tried
Code:
fees.Text = CType(dlFees.Items(0).FindControl("Label25"), Label).Text
from a button click event
 
if you want the value of the column for computing it's better to get this from the data value, rather than the formatted value.

since you want the total, just use the source the data is bound to, iterate the collection and the calculate the total.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
do I not have to do a findcontrol to get the value
 
not if you using the actual data. find control should only be used for presentation. here is an example of what I mean:
Code:
//lets assume a datatable as the datasource
DataTable table = GetData();
int total = 0;
foreach(DataRow row in table.Rows)
{
   total+=(int)row["column name"];
}
//do something with the total

MyDataList.DataSource = table;
MyDataList.DataBind();
in terms of the datalist control. the item data bound event fires for each item (header, seperator, item, footer) so it may be that the label your looking for doesn't exist in the current row. it which case, simply check for the item type.
Code:
if(e.Item.ItemType != ItemType.DataItem) return;

Label label = e.Item.FindControl("label25") as Label;
if(label == null) return;

label.Text = "i found the control!";

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top