【问题标题】:ASP.NET Textbox loses value after postbackASP.NET 文本框在回发后失去价值
【发布时间】:2016-04-22 16:13:35
【问题描述】:

我正在 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 中编码)

【问题讨论】:

  • 我不小心删除了一条评论:/

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


【解决方案1】:

你是对的,我不得不使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多