Connecting Tech Pros Worldwide Forums | Help | Site Map

Odd viewstate problem with Datagrid template columns

Kel Good
Guest
 
Posts: n/a
#1: Nov 19 '05
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.

---------------------------------------------------------------
Kel Good
MCT, MCAD, MCSD for Microsoft.NET



Kel Good
Guest
 
Posts: n/a
#2: Nov 19 '05

re: Odd viewstate problem with Datagrid template columns


Anyone? Anyone? Buhler?

"Kel Good" <kgood@customsoftware.ca> wrote in message
news:OgronCUzFHA.3256@TK2MSFTNGP09.phx.gbl...[color=blue]
>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.
>
> ---------------------------------------------------------------
> Kel Good
> MCT, MCAD, MCSD for Microsoft.NET
>
>[/color]


Alvin Bruney - ASP.NET MVP
Guest
 
Posts: n/a
#3: Nov 19 '05

re: Odd viewstate problem with Datagrid template columns


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" <kgood@customsoftware.ca> wrote in message
news:uA$EWLH0FHA.2348@TK2MSFTNGP15.phx.gbl...[color=blue]
> Anyone? Anyone? Buhler?
>
> "Kel Good" <kgood@customsoftware.ca> wrote in message
> news:OgronCUzFHA.3256@TK2MSFTNGP09.phx.gbl...[color=green]
> >I have a shopping cart datagrid that is filled only the first time the[/color][/color]
page[color=blue][color=green]
> >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[/color][/color]
the[color=blue][color=green]
> > 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[/color][/color]
case[color=blue][color=green]
> > 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[/color][/color]
using[color=blue][color=green]
> > 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[/color][/color]
state[color=blue][color=green]
> > of the controls like this?
> >
> > Thanks.
> >
> > ---------------------------------------------------------------
> > Kel Good
> > MCT, MCAD, MCSD for Microsoft.NET
> >
> >[/color]
>
>[/color]


Closed Thread