K
Kel Good
I have a shopping cart datagrid that is filled only the first time the page
is accessed, like so:
If Page.IsPostBack = False Then
RetrieveShoppingCart()
End If
The user can then change the quantity or check a cart item for deletion.
When they click the update button, I am attempting to iterate through the
grid, evaluating the Quantity textbox and Remove checkbox for each cart item
like so:
For Each gridRow As DataGridItem In CartGrid.Items
Dim quantityTextBox As TextBox =
DirectCast(gridRow.FindControl("Quantity"), TextBox)
Dim removeCheckBox As CheckBox =
DirectCast(gridRow.FindControl("Remove"), CheckBox)
If quantityTextBox.Text <> CartGrid.DataKeys(0).ToString() Or
removeCheckBox.Checked Then
The anomoly is that on the first postback, although the two controls are
found, they have not retained their values. The quantityTextBox.Text
property is a blank string, the the removeCheckBox.Checked property is
always the default of False, no matter what it has been set to by the user.
But on each subsequent postback after that, they do retain their changed
values.
The normal reason template controls would lose their value on a postback
would be if the grid was being accidentally rebound at each postback, or
viewstate is not properly set. But neither of these appears to be the case
here. I've literally modified the update button event handler so it does
nothing except display the value of the Quantity textbox on the page using a
label like so:
Dim testTextBox As TextBox =
DirectCast(CartGrid.Items(0).FindControl("Quantity"), TextBox)
TestLabel.Text = "Quantity is : " & testTextBox.Text
Exit Sub
On the first postback the label reads: "Quantity is: "
Each postback thereafter: "Quantity is: 2 (3,4, etc depending
on what its changed to)
Any thoughts on what would cause a first postback to not retain the state of
the controls like this?
Thanks.
is accessed, like so:
If Page.IsPostBack = False Then
RetrieveShoppingCart()
End If
The user can then change the quantity or check a cart item for deletion.
When they click the update button, I am attempting to iterate through the
grid, evaluating the Quantity textbox and Remove checkbox for each cart item
like so:
For Each gridRow As DataGridItem In CartGrid.Items
Dim quantityTextBox As TextBox =
DirectCast(gridRow.FindControl("Quantity"), TextBox)
Dim removeCheckBox As CheckBox =
DirectCast(gridRow.FindControl("Remove"), CheckBox)
If quantityTextBox.Text <> CartGrid.DataKeys(0).ToString() Or
removeCheckBox.Checked Then
The anomoly is that on the first postback, although the two controls are
found, they have not retained their values. The quantityTextBox.Text
property is a blank string, the the removeCheckBox.Checked property is
always the default of False, no matter what it has been set to by the user.
But on each subsequent postback after that, they do retain their changed
values.
The normal reason template controls would lose their value on a postback
would be if the grid was being accidentally rebound at each postback, or
viewstate is not properly set. But neither of these appears to be the case
here. I've literally modified the update button event handler so it does
nothing except display the value of the Quantity textbox on the page using a
label like so:
Dim testTextBox As TextBox =
DirectCast(CartGrid.Items(0).FindControl("Quantity"), TextBox)
TestLabel.Text = "Quantity is : " & testTextBox.Text
Exit Sub
On the first postback the label reads: "Quantity is: "
Each postback thereafter: "Quantity is: 2 (3,4, etc depending
on what its changed to)
Any thoughts on what would cause a first postback to not retain the state of
the controls like this?
Thanks.