This is a migrated thread and some comments may be shown as answers.

Convert RadListViewItem to custom object

1 Answer 117 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Dale Palmer
Top achievements
Rank 1
Dale Palmer asked on 03 Jul 2013, 10:51 AM
Hi,

can anyone please help, I am in the process of converting one of our working basic repeaters into a RadListView.

On the OnItemDataBound event I want to fire some code to format items in the itemtemplate using one of our business objects "tblVehicle" with the following code:
protected void uiRptCars_ItemDataBound(object sender, Telerik.Web.UI.RadListViewItemEventArgs e)
    {
        if (e.Item is Telerik.Web.UI.RadListViewDataItem)
        {
            tblVehicle item = ((tblVehicle)e.Item).DataItem;
            string name = string.Format("{0} {1} {2}", item.RegDate.Year, item.Make, item.Model);
            string url = string.Format("{0} {1} {2}", item.Make, item.Model, item.RegDate.Year).ToLower().Replace(" ", "-").Replace("/", "~");
            Image uiImgCar = (Image)e.Item.FindControl("uiImgCar");
            Literal uiLitTitle = (Literal)e.Item.FindControl("uiLitTitle");
            Literal uiLitColour = (Literal)e.Item.FindControl("uiLitColour");
            Literal uiLitFuel = (Literal)e.Item.FindControl("uiLitFuel");
            Literal uiLitTrans = (Literal)e.Item.FindControl("uiLitTrans");
            Literal uiLitEng = (Literal)e.Item.FindControl("uiLitEng");
            uiLitTitle.Text = string.Format("<h3><a href='/cars-for-breaking/{0}/{1}'>{2}</a></h3>", item.VehicleId, url, name);
            uiLitColour.Text = item.Colour;
            uiLitFuel.Text = item.FuelType;
            uiLitTrans.Text = item.Transmission;
            uiLitEng.Text = item.EngineCode;
            uiImgCar.AlternateText = name;
}
}

However, at the line

tblVehicle item = ((tblVehicle)e.Item).DataItem;
I'm getting the following error: Cannot convert type 'Telerik.Web.UI.RadListViewItem' to 'tblVehicle'
I'm sure there is a simple answer to this as everything worked fine under a straightforward repeater control.
Any help would be greatly appreciated.

Regards
Dale


1 Answer, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 08 Jul 2013, 06:36 AM
Hi,

 If you are binding the RadListView properly to a collection of objects of type tblVehicle then the DataItem property of the control will hold a reference to the correct object. Note that you will need to cast the DataItem property to tblVehicle and not e.Item which is always of type RadListViewDataItem:
Here is a sample code showing this approach:

var listViewDataItem = e.Item as RadListViewDataItem;
if (listViewDataItem != null)
{
      tblVehicle item = (tblVehicle)listViewDataItem.DataItem;
}


Regards,
Marin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
ListView
Asked by
Dale Palmer
Top achievements
Rank 1
Answers by
Marin
Telerik team
Share this question
or