asp.netvb.netpostbackrepeater

Repeater databinding issue: Dynamic weekly calendar not updating on postback when changing active week


I'm encountering issues with databinding a repeater during page load. I'm attempting to create a weekly calendar where the repeater displays dates, and for each day, a nested repeater displays appointments. I've implemented a basic week selector using Previous/Next buttons to navigate through weeks. Everything works fine upon the initial page load, but it fails to display anything when I use the buttons to change the active week. I'm certain the problem lies with the postback, but I'm struggling to find a solution. Any assistance would be appreciated.

html:

<div id="divCalendario" class="panel" runat="Server">
   <div id="divCalendarNavigator" style="display: block;" runat="server">
      <div>
         <asp:LinkButton runat="server" ID="lnkPrevWeek" OnClick="lnkPrevWeek_Click"><i class="fa fa-arrow-circle-o-left" aria-hidden="true"></i></asp:LinkButton>
         <asp:Literal ID="litData" runat="server"></asp:Literal>
         <asp:LinkButton runat="server" ID="lnkNextWeek" OnClick="lnkNextWeek_Click"><i class="fa fa-arrow-circle-o-right" aria-hidden="true"></i></asp:LinkButton>
         <asp:HiddenField runat="server" ID="hdnDa" />
         <asp:HiddenField runat="server" ID="hdnA" />
         <asp:HiddenField runat="server" ID="hdnSpostamento"/>
         <asp:DropDownList ID="ddlTecnicoCal" runat="server" AppendDataBoundItems="true" CssClass="tendina" placeholder="Tecnico" AutoPostBack="true">
            <%--<asp:ListItem Value="-1">Tutti</asp:ListItem>--%>
         </asp:DropDownList>
      </div>
   </div>
   <asp:Repeater ID="rptParent" runat="server">
      <ItemTemplate>
         <p style="margin-left: 10px">
            <b>
               <asp:Label ID="lblDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "data", "{0:dd/MM/yyyy}")%>'></asp:Label>
            </b>
         </p>
         <asp:Repeater ID="rptAppuntamenti" runat="server">
            <ItemTemplate>
               <%# Eval("id")%></td>
            </ItemTemplate>
         </asp:Repeater>
      </ItemTemplate>
   </asp:Repeater>
</div>

CODE:

Imports System.Data.SqlClient
Imports System.Globalization

Public Class Calendario
    Inherits System.Web.UI.Page

    Dim data1, data2 As DateTime
    Dim tecnico As Integer
    Dim utente As Utenti

    Public Function GetDayFromWeek(week As DateTime, day As DayOfWeek) As DateTime
        Dim startDay As DayOfWeek = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek

        ' get the beginning of the week 
        Dim diff As Integer = CInt(week.DayOfWeek) - CInt(startDay)
        Dim beginingOfWeek As DateTime = week.AddDays(diff * -1)

        ' get the day we are looking for
        diff = CInt(day) - CInt(startDay)
        If diff < 0 Then
            diff = 7 - CInt(startDay)
        End If
        Return beginingOfWeek.AddDays(diff)
    End Function

    Public Function datasql(ByVal dataini As Date) As String
        Return dataini.Year & "-" & dataini.Month & "-" & dataini.Day
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            caricaTecnici()
            Dim monday = GetDayFromWeek(Date.Today, DayOfWeek.Monday)
            Dim sunday = GetDayFromWeek(Date.Today, DayOfWeek.Sunday)

            hdnDa.Value = monday
            hdnA.Value = sunday

            litData.Text = monday & " " & sunday
            Dim dbVulcano As New dbVulcanoEntities
            data1 = Date.ParseExact(monday, "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-US")).Date
            data2 = Date.ParseExact(sunday, "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-US")).Date
            tecnico = Session("idUtente")
            utente = dbVulcano.Utenti.Where(Function(u) u.IDUtente = tecnico).Single
        End If

        BindRepeater()
    End Sub

    Private Sub BindRepeater()
        Dim constr As String = ConfigurationManager.ConnectionStrings("dbVulcanoConnectionString").ConnectionString
        Dim query As String = 
        "Select distinct(statoRic.DataAss) as data From StatoRic 
        INNER Join (Richieste INNER JOIN Clienti On clienti.IDCliente=Richieste.RFCliente) 
        On StatoRic.RFRic=Richieste.IDRic join Utenti On utenti.IDUtente=StatoRic.RFTecnico 
        WHERE (RFStato = 11 or RFStato = 12 Or rfstato = 13 Or rfstato = 41) 
        And Attuale=1 
        and StatoRic.DataAss is not null 
        and statoric.DataAss between @data1 
        and @data2 
        And RFTecnico=@tecnico 
        group by statoRic.DataAss 
        order by statoRic.DataAss asc"

        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand(query, con)
                cmd.Parameters.AddWithValue("@data1", data1)
                cmd.Parameters.AddWithValue("@data2", data2)
                cmd.Parameters.AddWithValue("@tecnico", tecnico)
                Using sda As New SqlDataAdapter(cmd)
                    Dim dt As New DataTable()
                    sda.Fill(dt)
                    rptParent.DataSource = dt
                    rptParent.DataBind()
                End Using
            End Using
        End Using
    End Sub

    Private Sub rptParent_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles rptParent.ItemDataBound
        If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
            Dim childRepeater As Repeater = e.Item.FindControl("rptAppuntamenti")
            Dim lblDate As Label = e.Item.FindControl("lblDate")

            Dim data As DateTime = Date.ParseExact(lblDate.Text, "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-US")).Date

            Dim constr As String = ConfigurationManager.ConnectionStrings("dbVulcanoConnectionString").ConnectionString
            Dim query As String = 
            "Select statoric.id, (case when clienti.IDCliente = 796 then richieste.descr else clienti.RagSociale end) as RagSociale, 
            CAST(statoRic.DataAss AS DATE) + ISNULL(CONVERT(datetime, 
            CONVERT(varchar(10), statoRic.OraDalle / 100)+ ':' + 
            CONVERT(varchar(10), statoRic.OraDalle % 100)),'00:00') as eventstart, 
            CAST(statoRic.DataAss AS DATE) + ISNULL(CONVERT(datetime, 
            CONVERT(varchar(10), statoRic.OraAlle / 100)+ ':' + 
            CONVERT(varchar(10), statoRic.OraAlle % 100)),'00:30') as eventend, 
            Utenti.Nome as tecnico, statoRic.DataAss as data From StatoRic 
            INNER Join 
            (Richieste INNER JOIN Clienti On clienti.IDCliente=Richieste.RFCliente) On StatoRic.RFRic=Richieste.IDRic 
            join Utenti On utenti.IDUtente=StatoRic.RFTecnico 
            WHERE (RFStato = 11 or RFStato = 12 Or rfstato = 13 Or rfstato = 41) 
            And Attuale=1 
            and StatoRic.DataAss is not null 
            And RFTecnico=@tecnico 
            and statoric.DataAss = @data 
            order by statoRic.DataAss asc"

            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand(query, con)
                    cmd.Parameters.AddWithValue("@data", data)
                    cmd.Parameters.AddWithValue("@tecnico", tecnico)
                    Using sda As New SqlDataAdapter(cmd)
                        Dim dt As New DataTable()
                        sda.Fill(dt)
                        childRepeater.DataSource = dt
                        childRepeater.DataBind()
                    End Using
                End Using
            End Using
        End If
    End Sub

    Sub caricaTecnici()
        Dim Query As String = "SELECT nome,idutente FROM Utenti WHERE attivo=1"
        Dim cmd As New SqlCommand(query)

        ddlTecnicoCal.DataSource = GetData(cmd)
        ddlTecnicoCal.DataTextField = "nome"
        ddlTecnicoCal.DataValueField = "idutente"
        ddlTecnicoCal.DataBind()
        ddlTecnicoCal.SelectedIndex = ddlTecnicoCal.Items.IndexOf(ddlTecnicoCal.Items.FindByValue(Session("idUtente")))
    End Sub

    Private Function GetData(cmd As SqlCommand) As DataTable
        Dim strConnString As String = ConfigurationManager.ConnectionStrings("dbVulcanoConnectionString").ConnectionString

        Using con As New SqlConnection(strConnString)
            Using sda As New SqlDataAdapter()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using dt As New DataTable()
                    sda.Fill(dt)
                    Return dt
                End Using
            End Using
        End Using
    End Function

    Protected Sub lnkPrevWeek_Click(sender As Object, e As EventArgs)
        data1 = Date.ParseExact(hdnDa.Value, "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-US")).Date
        data2 = Date.ParseExact(hdnA.Value, "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-US")).Date
        Dim monday = data1.AddDays(-7)
        Dim sunday = data2.AddDays(-7)
        hdnDa.Value = monday
        hdnA.Value = sunday

        litData.Text = monday & " " & sunday
        BindRepeater()
    End Sub

    Protected Sub lnkNextWeek_Click(sender As Object, e As EventArgs)
        data1 = Date.ParseExact(hdnDa.Value, "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-US")).Date
        data2 = Date.ParseExact(hdnA.Value, "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-US")).Date
        Dim monday = data1.AddDays(7)
        Dim sunday = data2.AddDays(7)
        hdnDa.Value = monday
        hdnA.Value = sunday
        litData.Text = monday & " " & sunday
        BindRepeater()
    End Sub
End Class

Thanks!

SOLUTION FOR FUTURE REFERENCE

Problem was the "tecnico" variable that became 0 at every postback, also to avoid any problem I get rid of global variables data1 and data2 and moved them inside the Bindrepeater routine, like this:

Dim data1, data2 As DateTime
If Len(ViewState("dataDa")) = 0 Then
    data1 = GetDayFromWeek(Date.Today, DayOfWeek.Monday)
    data2 = GetDayFromWeek(Date.Today, DayOfWeek.Sunday)
    ViewState("dataDa") = data1
    ViewState("dataA") = data2
Else
    data1 = Date.ParseExact(ViewState("dataDa"), "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-US")).Date
    data2 = Date.ParseExact(ViewState("dataA"), "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-US")).Date
End If

Solution

  • I think your problem could be your global variables not preserving their new value after postback:

    Dim data1, data2 As DateTime
    Dim tecnico As Integer
    Dim utente As Utenti
    

    Try to store the values inside Session variables or Viewstate instead.

    For example:

    Session("tecnico") = x
    ViewState("utente") = y
    

    I've had this issue in the past.

    The global variable doesn't keep its value