Thursday 23 April 2009

How to find controls in ListView ItemTemplate

This example below explains how to find controls and set them in the ItemTemplate of a ListView control when using XmlDataSource:

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (ListView1.EditItem == null)
{
// Format the vertical % to 2 decimal places.
var data = (ListViewDataItem)e.Item;
var vert = XPathBinder.Eval(data.DataItem, "./Vert", "0");
var formatVert = float.Parse(vert.Replace("%", ""));

((Literal)e.Item.FindControl("Literal8")).Text = formatVert.ToString(VERT_FORMAT);
}
}

No comments: