Solved

Custom Repeater fails casting on binding?

Posted on 2013-01-28
2
270 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
ID: 38828663
Replace DataListItem with RepeaterItem here:

<TemplateContainer(GetType(DataListItem))>
0
 

Author Comment

by:crandelj
ID: 38829127
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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