Solved

Custom Repeater fails casting on binding?

Posted on 2013-01-28
2
256 Views
Last Modified: 2013-01-28
I have built a custom repeater server control and it works fine except that it fails when trying to bind the data with a:

Unable to cast object of type 'System.Web.UI.WebControls.RepeaterItem' to type 'System.Web.UI.WebControls.DataListItem'

I built another version of the control exactly the same but used a DataList as the control to inherit and it works perfectly but I do not like the extra formatting and overhead of the DataList control.  (NOTE: I have an open question about stripping out the extra formatting from the DataList.  I may be forced to use that solution since I have it working).

My custom control switches itemtemplate dynamically in createitem based on datasource items evaluated.  (Removed for simplicity in the code below).

Here's the server control code:

Public Class CustomRepeater
    Inherits Repeater
    Implements INamingContainer

    Protected Overrides Function CreateItem(itemIndex As Integer, itemType As ListItemType) As RepeaterItem
        If Me.DataSource IsNot Nothing Then
            Dim rightTemplate As ITemplate = Nothing
            ' Below code to use Me.DataSource.Item to select correct template is removed for simplicity.  This version always selects FirstTemplate.  Causes same casting error either way.

            rightTemplate = FirstTemplate

            If itemType = ListItemType.Item Then
                ItemTemplate = rightTemplate
            End If
            If itemType = ListItemType.AlternatingItem Then
                AlternatingItemTemplate = rightTemplate
            End If
        End If
        Return MyBase.CreateItem(itemIndex, itemType)
    End Function

    <TemplateContainer(GetType(DataListItem))> _
    Public Property FirstTemplate() As ITemplate
        Get
            Return _FirstTemplate
        End Get
        Set(value As ITemplate)
            _FirstTemplate = value
        End Set
    End Property

    <TemplateContainer(GetType(DataListItem))> _
    Public Property SecondTemplate() As ITemplate
        Get
            Return _SecondTemplate
        End Get
        Set(value As ITemplate)
            _SecondTemplate = value
        End Set
    End Property

    Private _FirstTemplate As ITemplate
    Private _SecondTemplate As ITemplate


End Class



Here's the control placed in the aspx:
                <hc:CustomRepeater id="questionRepeater" runat="server">
                    <FirstTemplate>
                        <li>
                            <div class="reportquestion"><%# Container.DataItem("theContent")%></div>
                        </li>
                    </FirstTemplate>
                    <SecondTemplate>
                        <li>
                            <div class="reportquestion">TEST: <%# Container.DataItem("theContent")%></div>
                        </li>
                    </SecondTemplate>
                </hc:QuestionRepeater>



The casting error occurs at:
<div class="reportquestion"><%# Container.DataItem("theContent")%></div>



REMEMBER I was able to take the exact code and get it to work by Interiting from DataList and returning DataListItem.  But I'd really like to use Repeater instead as the base so I don't have to override all of its assumed formating.
0
Comment
Question by:crandelj
2 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
Comment Utility
Replace DataListItem with RepeaterItem here:

<TemplateContainer(GetType(DataListItem))>
0
 

Author Comment

by:crandelj
Comment Utility
Thanks I thought I was going nuts.  Simple solution to a painful problem.  I had already created a DataList alternative that was working but the extra spans were causing formatting problems so this will be the better solution.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now