By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

Saving The quantity of a textbox control between postbacks

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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...001.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.)

Nov 18 '05 #2

P: n/a
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

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.