Odd viewstate problem with Datagrid template columns

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.
 
A

Alvin Bruney - ASP.NET MVP

can you post a small application that replicates the issue?

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
 
K

Kel Good

Hi Alvin,

Sorry to be so late getting back to you one this. I really did not have an
easy way to sample this. But I figured out what was wrong. A little hard to
describe so I'll leave it at that. But thanks for your willingness to look
at this.

Kel

Alvin Bruney - ASP.NET MVP said:
can you post a small application that replicates the issue?

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------



Kel Good said:
Anyone? Anyone? Buhler?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top