回发的Asp.net转发器数据绑定应该怎么完成?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (81)

我在加载网页时遇到了转发器的数据绑定问题。我会制作一种每周日历,所以中继器应该显示日期,并且每天嵌套的中继器应该显示约会。用一个Previous / Next按钮做了一个简单的星期选择器,可以移动数周。它在第一次加载页面时工作正常,但是当我使用按钮更改活动星期时,什么都不显示。我知道这个问题确实是回传,但我无法绕过这个

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>

码:

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
提问于
用户回答回答于

我认为你的问题可能是你的全局变量不能保留它们的值postback

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

尝试将值存储在Session变量中,或者Viewstate改为存储。

例如:

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

扫码关注云+社区

领取腾讯云代金券