repeater databound headertemplate

Hi,

I am using asp.net 2.0 vb.net.

I have a repeater control and I want to make the header control (label) databound.

When i bind the repeater with my datasource, the header label does not contain the value (i have tested the datasource and there is a value).

Is this the way the repeater works? I.e. it does not allow you to databind the header controls? OR is it way too early in the morning for me and I am not thinking straight?

thanks,

saleek
LVL 11
Ramesh SrinivasTechnical ConsultantAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
VelioConnect With a Mentor Commented:
k, one way i can think of is instead of storing the image in the DataTable, put them in a dictionary where they can be associated with the repeater they should go in, something like

Dim titleImages As Dictionary(Of Repeater, String)

then have a common event handler for all the repeaters' ItemDataBound events and in there look up from the sender and bind the image's url from the dictionary...

might be round about, but it doesn't seem like there's a more direct way of binding headers
0
 
VelioCommented:
you can do this 2 ways:
this code goes in the ItemDataBound event handler
 
        If (e.Item.ItemType = ListItemType.Header) Then
            Dim headerLabel As Label = e.Item.FindControl("headerLabel")
            'bind the data here
        End If
 
--OR--
 
you can directly specify in value to bind in the web-page's markup:
<HeaderTemplate>
        <asp:Label ID="headerLabel" runat="server" Text='<%# Bind("HeaderLabelValue") %>'></asp:Label>
        </HeaderTemplate>

Open in new window

0
 
VelioCommented:
for more information on using a data binding expression, go here:
http://msdn2.microsoft.com/en-us/library/bda9bbfx.aspx
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Ramesh SrinivasTechnical ConsultantAuthor Commented:
Hi,

Thx for the responses.

I am aware of the usage regarding binding expressions, currently my usage is as follows:

<HeaderTemplate>
      <table width="100%" class="RssBorder">
         <tr>
              <td bgcolor="#fffaf0" align="left" colspan="2" class="mediumTxt">
                     <asp:Label ID="Label1" runat="server" Text'<%#DataBinder.Eval(Container.DataItem, "Title")%>></asp:Label>
              </td>
         </tr>
</HeaderTemplate>


But this is not working. The data column in my datatable called "Title" is not binding to this control. Even though the column has data in it.

thx.,
0
 
Ramesh SrinivasTechnical ConsultantAuthor Commented:
Correcting a typo.....

<HeaderTemplate>
      <table width="100%" class="RssBorder">
         <tr>
              <td bgcolor="#fffaf0" align="left" colspan="2" class="mediumTxt">
                     <asp:Label ID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Title")%>' />
              </td>
         </tr>
</HeaderTemplate>
0
 
VelioCommented:
yeah i see it doesn't like to bind header items with the repeater's own datasource (the DataItem is Nothing for a header item, even if there is binding to be done)

what's the context of this? is there only 1 record in the data set you're binding?

if so you could always just manually set the text in the ItemDataBound event?
    Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
        If (e.Item.ItemType = ListItemType.Header) Then
            Dim label As Label = e.Item.FindControl("Label1")
            label.Text = "Bind me"
        End If
    End Sub

Open in new window

0
 
Ramesh SrinivasTechnical ConsultantAuthor Commented:
Hi,

The context is that I have 6 repeater controls on my page and I am binding 6 rss feeds to them.

In the header I wanted to display the source image (i.e. the logo of the website which makes the feed available), which is a link available in the rss document object after it's created.

At the moment I am creating a datatable and binding it to the repeaters and it works fine - apart from the fact that I do not have a way to display this image in the header.

I tried to use the itemdatabound event, but it kept repeating the same image as the first feed.

Anyway, I'll post my code for you to get a better idea.

thanks,

saleek
Private dt As DataTable
 
    Protected Function BuildFeed(ByVal theRss As Rss.RssDocument) As DataTable
        dt = New DataTable
        dt.Columns.Add("title", Type.GetType("System.String"))
        dt.Columns.Add("oSource", Type.GetType("System.String"))
        dt.Columns.Add("description", Type.GetType("System.String"))
        dt.Columns.Add("FullDesc", Type.GetType("System.String"))
        dt.Columns.Add("link", Type.GetType("System.String"))
        dt.Columns.Add("oDate", Type.GetType("System.String"))
        dt.Columns.Add("img", Type.GetType("System.String"))
        Dim dr As DataRow
        Dim i As Integer = 0
        For Each li As Rss.RssItem In theRss.Channel.Items
            dr = dt.NewRow
            dr("oDate") = common.fixDate(li.PubDateParsed, 1)
            dr("title") = li.Title
            Try
                dr("img") = theRss.Channel.Image.Url
                System.Diagnostics.Debug.WriteLine(theRss.Channel.Image.Url)
            Catch er As Exception
                'System.Diagnostics.Debug.WriteLine(er.ToString)
            End Try
            Dim bb As System.Text.RegularExpressions.Regex = New _
                                            System.Text.RegularExpressions.Regex("(<(a|p)([^>]|[^p])*>)")
            If Not li.Description = Nothing Then
                If li.Description.Length > 0 Then
                    Dim matchIndex As Integer = bb.Match(li.Description).Index
                    If matchIndex > 0 Then
                        dr("FullDesc") = li.Description.Substring(0, matchIndex)
                    Else
                        dr("FullDesc") = li.Description
                    End If
                End If
                If li.Description.Length > 75 Then
                    dr("description") = li.Description.Substring(0, 75) & "..."
                Else
                    dr("description") = li.Description
                End If
            End If
            dr("link") = li.Link
            dt.Rows.Add(dr)
            i += 1
            If i = 5 Then
                Exit For
            End If
        Next
        Return dt
    End Function
 
    Protected Sub CreateRss(ByVal rptr As Repeater, ByVal rssURL As String)
        Try
            Dim theRss As Rss.RssDocument = Rss.RssDocument.Load(New System.Uri(rssURL))
            rptr.DataSource = BuildFeed(theRss)
            rptr.DataBind()
        Catch ex As Exception
 
        End Try
 
    End Sub
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CreateRss(Repeater1, "http://feeds.feedburner.com/variety/headlines")
        CreateRss(Repeater2, "http://www.empireonline.com/rss/rss.asp")
        CreateRss(Repeater3, "http://http://rss.imdb.com/news/")
        CreateRss(Repeater4, "http://feeds.hollywood.com/hollywoodcom_recent_news")
        CreateRss(Repeater5, "http://feeds.reuters.com/reuters/entertainment")
        CreateRss(Repeater6, "http://feeds.feedburner.com/variety/headlines")
    End Sub
 
 
My repeaters look like this:
 
<asp:Repeater ID="Repeater1" runat="server"> 
                                <HeaderTemplate><table width="100%" class="RssBorder"><tr><td bgcolor="#fffaf0" align="left" colspan="2" class="mediumTxt">
                                    <asp:Label ID="Label1" runat="server" Text="Variety.com"></asp:Label><asp:Image ID="Image1" runat="server" /></td></tr></HeaderTemplate>
                                    <ItemTemplate><tr>
                                        <td valign="top"  class="newsLinks"><asp:HyperLink ID="feed1" runat="server" NavigateUrl='<%#DataBinder.Eval(Container.DataItem, "link")%>' Target="_blank" Text='<%#DataBinder.Eval(Container.DataItem, "title")%>' ToolTip='<%#DataBinder.Eval(Container.DataItem, "FullDesc")%>' /><BR><%#DataBinder.Eval(Container.DataItem, "description")%></td>
                                    </tr></ItemTemplate>
                                <FooterTemplate></table></FooterTemplate>
                            </asp:Repeater>

Open in new window

0
 
VelioCommented:
sorry, i will try to have a look asap, probably only sometime tomorrow though.
0
 
Ramesh SrinivasTechnical ConsultantAuthor Commented:
Hi,

Forgot about this thread!

I think your suggestion is probably the only (best?) option.

Storing the image location in a data structure and then associating it with the repeater.

Thanks for your input.

regards,

saleek
0
All Courses

From novice to tech pro — start learning today.