Solved

Custom Repeater fails casting on binding?

Posted on 2013-01-28
2
279 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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