簡體   English   中英

ASP.NET頁面不斷刷新(VB.NET)

[英]ASP.NET Page keeps refreshing (VB.NET)

每當我嘗試此代碼時,頁面仍保持刷新。

Imports System.Data.SqlClient
Imports System.Data

Partial Class ProjectReport
    Inherits System.Web.UI.Page

    Private myTotal As Decimal = 0


    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load, Chart1.Load

        Dim ProjectID As Integer = Session("project_id")
        Session("ProjectID") = ProjectID

        lblProjNameHeading.Text = "[ " + ProjectID.ToString + " ]"

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim projComm As String = "SELECT project_id, project_start, project_finish, project_budget, project_cost FROM projects WHERE project_id=@parameter"

        Dim projSQL As New SqlCommand

        conn.Open()

        projSQL = New SqlCommand(projComm, conn)
        projSQL.Parameters.AddWithValue("@parameter", ProjectID.ToString)

        Dim datareader As SqlDataReader = projSQL.ExecuteReader()


        While datareader.Read

            lblProjectCode.Text = datareader("project_id").ToString
            lblProjectStart.Text = datareader("project_start").ToString
            lblProjectStart2.Text = datareader("project_start").ToString
            lblProjectEnd.Text = datareader("project_finish").ToString
            lblProjectEnd2.Text = datareader("project_finish").ToString
            lblProjectBudget.Text = datareader("project_budget").ToString
            lblProjectBudget2.Text = datareader("project_budget").ToString
            lblProjectCost.Text = datareader("project_cost").ToString
            lblProjectCost2.Text = datareader("project_cost").ToString
            ' lblProjectLeader.Text = datareader("project_cost").ToString
            'lblProjectExpenditures.Text = agdgssag

            Dim StartDate As DateTime = datareader("project_start")

            Dim FinishDate As DateTime = datareader("project_finish")

            Dim today As DateTime = DateTime.Now

            Dim sumDays = (FinishDate - StartDate).TotalDays
            Dim daysToNow = (today - StartDate).TotalDays

            Dim percentage = daysToNow / sumDays * 100

            Dim percentageLeft = 100 - percentage

            Session("PercentageCompleted") = percentage
            Session("PercentageLeft") = percentageLeft

            GetTable()

            Expenditures()
            lblProjectPercentage.Text = percentage.ToString("N2") + "%"

        End While

        datareader.Close()
        conn.Close()

        If Not Page.IsPostBack Then
            BindData()
        End If

    End Sub

    Private Sub BindData()

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim query As New SqlCommand("SELECT Items.item_name, Items.item_cost, project_items.item_quantity FROM Items INNER JOIN project_items ON items.item_id = project_items.item_id WHERE project_items.project_id = @parameter", conn)

        conn.Open()

        query.Parameters.AddWithValue("@parameter", Convert.ToInt32(Session("ProjectID")))
        Dim da As New SqlDataAdapter(query)

        da.SelectCommand = query

        Dim table As New DataTable()

        da.Fill(table)

        grdItems.DataSource = table
        conn.Close()
        grdItems.DataBind()

    End Sub

    Protected Sub grdItems_RowDataBound(sender As Object, e As GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)
            myTotal += (CDec(rowView("item_cost")) * CDec(rowView("item_quantity")))

        End If

        If e.Row.RowType = DataControlRowType.Footer Then
            Dim lblTotalPrice As Label = DirectCast(e.Row.FindControl("lblTotalPrice"), Label)
            lblTotalPrice.Text = myTotal.ToString()

        End If
    End Sub

    Function GetTable() As DataTable

        Dim table As New DataTable

        table.Columns.Add("Percentage Completed", GetType(Double))
        table.Columns.Add("Percentage Not-Completed", GetType(Double))

        table.Rows.Add(Session("PercentageCompleted"))
        table.Rows.Add(Session("PercentageLeft"))

        Chart1.DataSource = table
        Chart1.Series("Series1").XValueMember = "Percentage Not-Completed"
        Chart1.Series("Series1").YValueMembers = "Percentage Completed"
        Chart1.DataBind()

        Return table
    End Function

    Private Sub Expenditures()

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim projExpComm As String = "SELECT Items.item_cost, project_items.item_quantity FROM Items INNER JOIN project_items ON items.item_id = project_items.item_id WHERE project_items.project_id = @parameter"

        Dim projExpSQL As New SqlCommand

        conn.Open()

        projExpSQL = New SqlCommand(projExpComm, conn)
        projExpSQL.Parameters.AddWithValue("@parameter", Session("project_id"))

        Dim datareader As SqlDataReader = projExpSQL.ExecuteReader()

        datareader.Read()

        While datareader.Read

            While datareader.HasRows

                Dim ItemsTotal As Double = 0

                For Each row In datareader

                    Dim ItemCost = datareader("item_cost")
                    Dim ItemQuantity = datareader("item_quantity")
                    Dim ItemsSubTotal As Double = ItemCost * ItemQuantity

                    ItemsTotal = ItemsSubTotal
                Next

                ItemsTotal = ItemsTotal + ItemsTotal
                lblProjectExpenditures.Text = ItemsTotal.ToString

            End While


        End While

        datareader.Close()
        conn.Close()

    End Sub

End Class

為什么會這樣呢?

我檢查了未關閉的連接/數據讀取器,但一切正常。

有什么我想念的嗎?

每次回發時,頁面加載方法都會完全執行。 您應該檢查是否看到!isPostBack來阻止該代碼的完整執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM