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);
}
}

Thursday 16 April 2009

App_Code Class not working

I have a class in my App_Code folder and I am trying to reference it in my code behind. The name spaces are the same but I keep getting an error that is can't find the class.

The reason being I am using a web application project and not a web site project. In order for the class to be recognised I need to change the "Build Action" of the class file to "Complie" from "Content".

Build and it works now.