title: Changing the last datarow from a asp:repeater table if data is shown in asp:label using vb.net
body: <p>I want to change the last value of a asp:repeater > table > tr > asp:label. If a condition is true. To be more expecific, i have the values of the bbdd stored in lblJournee and lblDateFormation. I have another lblDateFormation2 to take the value of the last date. I want to change the value of the last row in my repeater if this condition is made. I hope is understood my problem, i am quite new and trying to learn.</p>
  
  <p>CODE FOR .aspx:</p>
  
  <pre><code>&lt;asp:repeater id="rep_Dates" runat="server"&gt;
                                  &lt;HeaderTemplate&gt;
                                      &lt;table id="tblRepDates"&gt;
                                          &lt;tr&gt;
                                              &lt;th rowspan="1" colspan="2"&gt;Dates&lt;/th&gt;
                                          &lt;/tr&gt;
                                  &lt;/HeaderTemplate&gt;
                                  &lt;ItemTemplate&gt;
                                          &lt;tr&gt;
                                              &lt;td class="sombre"&gt;                                             
                                                  &lt;asp:label runat="server" id="lbl_DateFormation" text='&lt;%# Eval("[DateFormation]")%&gt;'&gt;&lt;/asp:label&gt;                                                                                                                                                                                                 
                                              &lt;/td&gt;
                                              &lt;td class="sombre"&gt;
                                                  &lt;asp:label runat="server" id="lbl_DemiJournee" text='&lt;%# Eval("[DemiJournee]")%&gt;'&gt;&lt;/asp:label&gt;
                                              &lt;/td&gt;
                                          &lt;/tr&gt;
                                  &lt;/ItemTemplate&gt;
                                  &lt;FooterTemplate&gt;
                                      &lt;/table&gt;
                                  &lt;/FooterTemplate&gt;
                              &lt;/asp:repeater&gt;
  </code></pre>
  
  <p>CODE FOR .aspx.vb:</p>
  
  <pre><code>Private Sub BindRepeaterData(ByVal noformationL As String)
  
          Dim conSQL As New System.Data.SqlClient.SqlConnection
          Dim cmdSQL As New System.Data.SqlClient.SqlCommand
  
          conSQL.ConnectionString = Application("BACKstr")
          conSQL.Open()
  
          cmdSQL.Connection = conSQL
          cmdSQL.CommandText = "select convert(char,[DateFormation],103) AS DateFormation,[DemiJournee] from [EXT$FORM_FormationDates] where NoFormation='" &amp; Replace(noformationL, "'", "''") &amp; "' and TypeDates = 0 order by DateFormation,DemiJourneeInt"
  
          Dim ds As New System.Data.DataSet()
          Dim da As New Data.SqlClient.SqlDataAdapter(cmdSQL)
  
          da.Fill(ds)
  
          If ds.Tables(0).Rows.Count &gt; 0 Then
              Dim lblDateFormation As New Label
              Dim lblJournee As New Label
              Dim lblDateFormation2 As String = ""
              Dim index As Integer = 0
  
              rep_Dates.Visible = True
              rep_Dates.DataSource = ds
              rep_Dates.DataBind()
              'je mets (1) parce que c'est le deuxieme element du repeater
              lblDateFormation = rep_Dates.Controls(1).FindControl("lbl_DateFormation")
              lblJournee = rep_Dates.Controls(1).FindControl("lbl_DemiJournee")
  
              For Each item As RepeaterItem In rep_Dates.Items
  
                  lblDateFormation = item.FindControl("lbl_DateFormation")
                  lblJournee = item.FindControl("lbl_DemiJournee")
                  'si les dates sont les memes on met une seule ligne avec lblJournee = "Journee"
                  'il faut mettre que la premiere date quelque soit la lblJournee reste M
                  'item.ItemIndex = 0 
                  'Pour tous les elements on peut se retrouver avec:
                  '   - Le premier element est Matin ou AM est la date sera prise
                  '   - Si le deuxieme element est AM et la date est egale a la premiere...on efface la ligne et le premier element prend la valeur JOURNEE
                  '   - Si le deuxieme element est AM et la date est different...On ecrit AM
                  '   - Si le deuxieme element est Matin on ecrit Matin
                  If item.ItemIndex = 0 Then
  
                      If lblJournee.Text = "Matin" Then
  
                          lblJournee.Text = "Matin"
                          lblDateFormation2 = lblDateFormation.Text
                          index = item.ItemIndex
  
                      Else
  
                          lblJournee.Text = "Après-midi"
                          lblDateFormation2 = lblDateFormation.Text
                          index = item.ItemIndex
  
                      End If
  
                  Else
  
                      If lblJournee.Text = "Après-midi" And lblDateFormation2 = lblDateFormation.Text Then
  
                          item.Visible = False
  
                          'Por cada fila que contenga Matin la cambiamos por journee 
                          'Eliminamos la ultima linea y el valor journee sera el de esta o eliminamos esta linea y cambiamos matin por journée
                          lblDateFormation2 = lblDateFormation.Text
                          index = item.ItemIndex
  
                      ElseIf lblJournee.Text = "Après-midi" And lblDateFormation2 &lt;&gt; lblDateFormation.Text Then
  
                          lblJournee.Text = "Après-midi"
                          lblDateFormation2 = lblDateFormation.Text
                          index = item.ItemIndex
  
                      ElseIf lblJournee.Text = "Matin" Then
  
                          lblJournee.Text = "Matin"
                          lblDateFormation2 = lblDateFormation.Text
                          index = item.ItemIndex
  
                      End If
  
                  End If
  
              Next
  
          Else
              rep_Dates.Visible = False
          End If
  
          conSQL.Close()
  
      End Sub
  </code></pre>
  
  <p>I hope is all clear. Thanks in advance to any help</p>
  
  <p><strong>Additional code</strong> for ItemDataBound event (taken from comment below for clarity)</p>
  
  <pre><code>Sub R1_ItemDataBound(ByVal Sender As Object, ByVal e As RepeaterItemEventArgs) Handles rep_Dates.ItemDataBound()
  Dim lblJournee As New Label 
  Dim lblDateFormation As New Label 
     If (e.Item.ItemType = ListItemType.Item) Then 
        If e.Item.ItemIndex = Session("dataRowCount") And lblJournee.Text = "Après-midi" Then 
           lblJournee.Text = "Journée" 
        End If 
     End If 
  End Sub
  </code></pre>