Convert RadListViewItem to custom object

2 posts, 0 answers
  1. Dale Palmer
    Dale Palmer avatar
    27 posts
    Member since:
    May 2008

    Posted 03 Jul 2013 Link to this post


    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.


  2. Marin
    Marin avatar
    1038 posts

    Posted 08 Jul 2013 Link to this post


     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;

    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.
  3. DevCraft R3 2016 release webinar banner
Back to Top