2

QUESTION

How to change the value of a control inside the header template of a DataList based on the value of a DataItem?

I cannot change the value in ListItemType.HeaderItem because I do not know what value to set it too until the ListItem is bound.

ASP.NET

<asp:DataList ID="dl" runat="server" ShowHeader="true">
  <HeaderTemplate>
    <asp:Label ID="lbl" runat="server" Text="MyText" />
  </HeaderTemplate>
  <ItemTemplate>

  </ItemTemplate>
</asp:DataList>

CODE BEHIND

  Protected Sub dl_ItemDataBound(sender As Object, e As DataListItemEventArgs) Handles dl.ItemDataBound
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
      Dim Type As Integer = e.Item.DataItem("Type")

      If Type = 0 Then
        'Change lbl text in HeaderTemplate
      End If

    End If
  End Sub
Andrei
  • 55,890
  • 9
  • 87
  • 108
DreamTeK
  • 32,537
  • 27
  • 112
  • 171
  • see if this helps http://stackoverflow.com/questions/14933540/accessing-controls-in-datalist-headertemplate-from-codebehind – Ashley John Oct 12 '16 at 09:37
  • @AshleyJohn Thanks but I already reviewed these. I cannot use HeaderTemplate because of my points noted in my question. – DreamTeK Oct 12 '16 at 09:40

2 Answers2

2

Use below code:

  Private headerItem As DataListItem

  Protected Sub dl_ItemDataBound(sender As Object, e As DataListItemEventArgs) Handles dl.ItemDataBound

            If e.Item.ItemType = ListItemType.Header Then
                headerItem = e.Item
            End If
            If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
               Dim Type As Integer = e.Item.DataItem("Type")

               If Type = 0 Then
                    Dim lbl As Label = CType(headerItem.FindControl("lbl"), Label)
                    lbl.Text = "New Text"
               End If

            End If
  End Sub
Dinesh Singh
  • 210
  • 1
  • 4
1

Header is one of the items, so you need to iterate over the list of items, find the header one and use FindControl to get the label. The problem here, however, is that at any point of data binding process you cannot be sure that header item was already bound and the label exists. Usually you'll find it to be data bound first, but there is no guarantee in the API, so you should not be relying on that behavior.

So what you could do is have some class field to store the text you need, and then set this text on PreRender, when data binding has already happened. Something like that (C#, but should be easy to translate to VB):

Private headerText as String

Protected Sub dl_ItemDataBound(sender As Object, e As DataListItemEventArgs) Handles dl.ItemDataBound
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
       Dim Type As Integer = e.Item.DataItem("Type")

       If Type = 0 Then
           headerText = "SomeText"
       End If

    End If
End Sub

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
    Dim item As DataListItem
    For Each item In dl.items
        If item.ItemType = ListItemType.Header Then
            Dim HeaderLabel As Label = _
                CType(item.FindControl("lbl"), Label)
            HeaderLabel.Text = headerText
    End If
    Next item
End Sub
Andrei
  • 55,890
  • 9
  • 87
  • 108