Saving The quantity of a textbox control between postbacks

Discussion in 'ASP .Net' started by Gary Vidal, Feb 9, 2004.

  1. Gary Vidal

    Gary Vidal Guest

    I have a shopping cart webpage that shows a product and the Quantity they
    would like to order:
    When you click the link I want to take the quantity that they entered in the
    textbox and post that to the next page. But it keeps the original quantity I
    added to the viewstate.
    Where can i code this to post the correct quantity.
    Here is the code-behind I use.

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    If Not IsPostBack Then

    ' Obtain ProductID from QueryString

    Dim ProductID As Integer = CInt(Request.Params("ProductID"))

    myProductID = ProductID

    ' Obtain Product Details

    Dim products As IBuySpy.ProductsDB = New IBuySpy.ProductsDB

    Dim myProductDetails As IBuySpy.ProductDetails =
    products.GetProductDetails(ProductID)

    ' Update Controls with Product Details

    desc.Text = myProductDetails.Description

    UnitCost.Text = String.Format("{0:c}", myProductDetails.UnitCost)

    'ModelName.Text = myProductDetails.ModelNumber

    ModelNumber.Text = myProductDetails.ModelNumber.ToString()

    NumericCode.Text = myProductDetails.NumericCode.ToString()

    UnitOfMeasure.Text = myProductDetails.UnitofMeasure.ToString()

    MinQuantity.Text = myProductDetails.MinQuantity.ToString()

    MinQuantity_Hidden.Text = myProductDetails.MinQuantity.ToString()

    OrderQuantity.Text = myProductDetails.MinQuantity.ToString()

    If myProductDetails.Available = "Y" Then

    Available.Text = "Product is Available"

    OrderQuantity.Enabled = True

    Else

    Available.Text = "Product is not available"

    OrderQuantity.Enabled = False

    End If

    ProductImage.ImageUrl = "Images/webimages/" & myProductDetails.ProductImage
    & ".gif"

    'addToCart.NavigateUrl = "AddToCart.aspx?ProductID=" & ProductID &
    "&Referer=" & Me.Request.UrlReferrer.ToString

    ReviewList.ProductID = ProductID

    AlsoBoughtList.ProductID = ProductID

    ViewState("ProductID") = CInt(Request.Params("ProductID"))

    ViewState("OrderQuantity") = myProductDetails.MinQuantity.ToString()

    ViewState("ReferrerUrl") = Request.UrlReferrer.ToString()

    Else

    OrderQuantity.Text = ViewState.Item("OrderQuantity")

    End If

    End Sub

    Private Sub AddToCart_Click(ByVal sender As System.Object, ByVal e As
    System.Web.UI.ImageClickEventArgs) Handles AddToCart.Click

    Response.Redirect("AddToCart.aspx?ProductID=" & ViewState.Item("ProductID")
    & "&Quantity=" & ViewState.Item("OrderQuantity") & "&ReturnURL=" &
    ViewState("ReferrerUrl"))

    End Sub

    End Class
     
    Gary Vidal, Feb 9, 2004
    #1
    1. Advertising

  2. Hi Gary,


    Thanks for posting in the community!
    From your description, you used some entry fields to contain productID and
    product Quntity info. And there is a linkbutton on the page when the
    linkbuton is clicked you 'd like to retrieve the latest values in the
    (productID and quantity)entry fields and post them as querystrings to
    another page, yes?
    If there is anything I misunderstood, please feel free to let me know.

    I've viewed the page code you provide and I'd like to ask some further
    questions on it:
    1. Do use the ViewState to store and retrieve the values of the
    textboxes(product quantity),yes? Why don't you directly get the value via
    accessing the textbox directly? Such as
    Dim quantity as Integer = Integer.parse(txtQuantity.Text ) ?
    I think this'll be more direct and simpler, do you think so?

    2. You used the below code block in the page's Page_Load event handler:
    If Not IsPostBack Then
    ' Obtain ProductID from QueryString
    .........................
    ...........
    Else
    OrderQuantity.Text = ViewState.Item("OrderQuantity")
    End If

    What does this sentense "OrderQuantity.Text =
    ViewState.Item("OrderQuantity")" mean? This sentense will make the
    "OrderQuantity" textbox's value return to its older value. If you'd like
    retrieve the new value, you should directly use the value via
    "OrderQuantity.Text" rather than the ViewState because the value in the
    ViewState are the former one which is set in the last postback. For more
    detailed infos on the ViewState of ASP.NET webpage, you may view the
    following
    web reference in MSDN:
    #Taking a Bite Out of ASP.NET ViewState
    http://msdn.microsoft.com/library/en-us/dnaspnet/html/asp11222001.asp?frame=
    true

    In addition, do you think it possible for you to get the latest value and
    post them in the below approach:
    Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles LinkButton1.Click

    Dim quantity As Integer = Int32.Parse(txtQuantity.Text)
    Response.Redirect("newpage.aspx?quantity=" & quantity.ToString())

    End Sub

    Thus, the value you get from textbox.Text is the latest value user has
    inputed before the page is posted back.

    Please check out my suggestion. If you feel anything unclear or if my
    suggestion isn't quite suitable for your situation, please feel free to let
    me know. I'll be willing to assit you.



    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Feb 10, 2004
    #2
    1. Advertising

  3. Hi Gary,


    Have you had a chance to try out my suggestion or have you resolved your
    problem? If you need further assistance, please feel free to let me know.



    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    Steven Cheng[MSFT], Feb 12, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nav

    time quantity in vhdl

    Nav, Oct 1, 2003, in forum: VHDL
    Replies:
    3
    Views:
    16,366
  2. Dave Lech

    saving xml between postbacks

    Dave Lech, Jul 20, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    375
    Daniel Walzenbach
    Jul 20, 2005
  3. Przemek Klein
    Replies:
    0
    Views:
    375
    Przemek Klein
    Oct 6, 2005
  4. Replies:
    0
    Views:
    454
  5. Replies:
    3
    Views:
    311
    Earl Purple
    May 17, 2006
Loading...

Share This Page