ASP.NET文本框在回发后丢失了值

时间:2016-01-17 13:09:09

标签: vb.net gridview cookies postback shopping-cart

我在ASP.NET中编写了shopping cart,我希望用户在文本框中填写他们想要的计数,并将这个计数放在一个cookie中,以便我们可以重新计算他​​们的计数回到他们的购物车。我使用更新按钮使用存储的值更新cookie,但是当页面回发时,它会使用之前的值填充文本框。有谁知道如何解决这个问题? 我的更新按钮代码:

If Request.Cookies("Aantal") Is Nothing Then
        Dim objCookieAantal As New HttpCookie("Aantal")

        For x = 0 To dgvWinkelmand.Rows.Count - 1
            Dim txtAantal As TextBox = CType(dgvWinkelmand.Rows(x).Cells(3).FindControl("txtAantal"), TextBox)
            objCookieAantal.Values.Add(dgvWinkelmand.Rows(x).Cells(0).Text, txtAantal.Text)

        Next

        objCookieAantal.Expires = Now.AddDays(30)

        Response.Cookies.Add(objCookieAantal)

        'fill
        If IsPostBack Then

            For x = 0 To dgvWinkelmand.Rows.Count - 1
                Dim txtAantal As TextBox = CType(dgvWinkelmand.Rows(x).Cells(3).FindControl("txtAantal"), TextBox)
                txtAantal.Text = objCookieAantal.Values.Item(dgvWinkelmand.Rows(x).Cells(0).Text)

            Next
        End If
    Else

        Dim objCookieAantal2 As HttpCookie = Request.Cookies("Aantal")

        For x = 0 To dgvWinkelmand.Rows.Count - 1
            Dim txtAantal As TextBox = CType(dgvWinkelmand.Rows(x).Cells(3).FindControl("txtAantal"), TextBox)
            objCookieAantal2.Values.Remove(dgvWinkelmand.Rows(x).Cells(0).Text)
            objCookieAantal2.Values.Add(dgvWinkelmand.Rows(x).Cells(0).Text, txtAantal.Text)

        Next

        Response.Cookies.Add(objCookieAantal2)

        'fill
        If IsPostBack Then

            For x = 0 To dgvWinkelmand.Rows.Count - 1
                Dim txtAantal As TextBox = CType(dgvWinkelmand.Rows(x).Cells(3).FindControl("txtAantal"), TextBox)
                txtAantal.Text = objCookieAantal2.Values.Item(dgvWinkelmand.Rows(x).Cells(0).Text)

            Next
        End If
    End If

我的页面加载代码以填充gridview:

      If IsPostBack Then

            For x = 0 To dgvWinkelmand.Rows.Count - 1
                Dim txtAantal As TextBox = CType(dgvWinkelmand.Rows(x).Cells(3).FindControl("txtAantal"), TextBox)
                txtAantal.Text = objCookieAantal.Values.Item(dgvWinkelmand.Rows(x).Cells(0).Text)

            Next
        End If

提前致谢! (我在VB.NET中编码)

1 个答案:

答案 0 :(得分:0)

你是对的,我必须使用If not ispostback,现在唯一的问题是它在我删除产品时没有填写计数,我的删除代码:

 If (e.CommandName = "Verwijder") Then
        If dgvWinkelmand.Rows.Count = 1 Then
            Dim objCookie As HttpCookie = Request.Cookies("Winkelmand")

            objCookie.Values.Remove(dgvWinkelmand.Rows(0).Cells(0).Text)

            Response.Cookies.Add(objCookie)

            dgvWinkelmand.Visible = False
            btnKassa.Visible = False
            imgShoppingCart.Visible = True
            lblLeeg.Visible = True


        Else

            lblLeeg.Visible = False
            imgShoppingCart.Visible = True
            'bepaal row index opgeslagen in CommandArgument eigenschap
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim intTeVerwijderenItem As Integer

            ' bepaal rij dat button bevat van rows collectie
            Dim row As GridViewRow = dgvWinkelmand.Rows(index)

            'Verwijder item van cookie
            Dim objCookie As HttpCookie = Request.Cookies("Winkelmand")

            intTeVerwijderenItem = Integer.Parse(dgvWinkelmand.Rows(index).Cells(0).Text)
            objCookie.Values.Remove(intTeVerwijderenItem.ToString)

            Response.Cookies.Add(objCookie)

            dgvWinkelmand.DataBind()

            'fill
            Dim objCookieAantal As HttpCookie = Request.Cookies("Aantal")
            If IsPostBack Then

                For x = 0 To dgvWinkelmand.Rows.Count - 1
                    Dim txtAantal As TextBox = CType(dgvWinkelmand.Rows(x).Cells(3).FindControl("txtAantal"), TextBox)
                    txtAantal.Text = objCookieAantal.Values.Item(dgvWinkelmand.Rows(x).Cells(0).Text)

                Next
            End If
        End If

    End If