简体   繁体   English

回发时的ASP.NET转发器数据绑定

[英]Asp.net repeater databind on postback

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: 问题是“ tecnico”变量在每次回发时都变为0,为了避免出现任何问题,我摆脱了全局变量data1和data2并将其移入Bindrepeater例程中,如下所示:

 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

ORIGINAL QUESTION 原始问题

I'm having trouble with databind of a repeater on page load. 我在页面加载时遇到中继器的数据绑定问题。 I would make a sort of weekly calendar, so the repeater should show dates and for every day a nested repeater should show appointments. 我会做一种每周日历,这样中继器应该显示日期,嵌套的中继器每天都应该显示约会。 I made a simple week selector with a Previous/Next button to move through weeks. 我使用“上一个/下一个”按钮制作了一个简单的星期选择器,以浏览星期。 It work fine on first page load, but it shows nothing when I use the buttons to change active week. 在首页加载时效果很好,但是当我使用按钮更改活动周数时却什么也没显示。 I know the problem is the postback for sure, but I can't get around this... any help? 我知道问题肯定是回发的,但是我无法解决...任何帮助吗?

html: HTML:

<div id="divCalendario" class="panel" runat="Server">

                                <div id="divCalendarNavigator" style="display: block;" runat="server">
                                    <div>
                                        <%--<asp:Calendar runat="server" ID="calendar" SelectionMode="DayWeek" TitleStyle-ForeColor="#1f7872" TitleStyle-Font-Bold="true" PrevMonthText="<i class=icon-foo>&#xf053;</i>" NextMonthText="<i class=icon-foo>&#xf054;</i>" NextPrevStyle-Font-Names="FontAwesome" SelectWeekText="<i class=icon-foo>&#xf0a6;</i>" SelectorStyle-Font-Names="FontAwesome" WeekendDayStyle-BackColor="LightGray" SelectedDayStyle-BackColor="#f3ef98" SelectedDayStyle-ForeColor="Black" SelectedDayStyle-Font-Bold="true" OnSelectionChanged="calendar_SelectionChanged"></asp:Calendar>--%>
                                        <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 lblTecnico As Label = e.Item.FindControl("lblTecnico")
            Dim data As DateTime = Date.ParseExact(lblDate.Text, "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-US")).Date
            'lblTecnico.Text = utente.NomeExt
            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! 谢谢!

I think your problem could be your global variables not preserving their new value after postback : 我认为您的问题可能是您的全局变量在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. 尝试将值存储在Session变量或Viewstate

For example: 例如:

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

I've had this issue in the past. 我过去曾遇到过这个问题。

The global variable doesn't keep its value 全局变量不保留其值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM