Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Custom Repeater fails casting on binding?

Posted on 2013-01-28
2
Medium Priority
?
289 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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.…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

971 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