?
Solved

Custom Repeater fails casting on binding?

Posted on 2013-01-28
2
Medium Priority
?
282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
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.…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

771 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