Findcontrol issue

M

Me LK

I have a grid with an editable amount. Click update and the text box
appears and the number changes. It works fine here.

Private Sub grid_updatecommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
grid.UpdateCommand

Dim intProductId As String = grid.DataKeys(e.Item.ItemIndex)
Dim quantity As String =
CType(e.Item.FindControl("quantityTextBox"), TextBox).Text

Try
ShoppingCart.UpdateProductQuantity(intProductId, quantity)
Catch ex As Exception
'If the update generates an error this is the place we
should warn the users
Finally
grid.EditItemIndex = -1
bindShoppingCart()

End Try
End Sub

But I need to add the size and color to the update since there may be
items with the same ID but different sizes or colors. So I changed my
code to this.

Private Sub grid_updatecommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
grid.UpdateCommand
Dim intProductId As String = grid.DataKeys(e.Item.ItemIndex)
Dim quantity As String =
CType(e.Item.FindControl("quantityTextBox"), TextBox).Text


Dim Psize As String = CType(e.Item.FindControl("Psize"),
Label).Text
Dim color As String = CType(e.Item.FindControl("color"),
Label).Text

Try
ShoppingCart.UpdateProductQuantity(intProductId, quantity,
Psize, color)
Catch ex As Exception
'If the update generates an error this is the place we
should warn the users
Finally
grid.EditItemIndex = -1
bindShoppingCart()

End Try
End Sub

For some reason I get the dreaded
"Object reference not set to an instance of an object. "

at this line
Dim Psize As String = CType(e.Item.FindControl("Psize"),
Label).Text

I have my code as this
<asp:TemplateColumn HeaderText="Quantity">
<HeaderStyle
HorizontalAlign="Center"></HeaderStyle>
<ItemTemplate>
<asp:Label ID="Psize" Visible="False" runat="server" text='<%#
DataBinder.Eval(Container, "DataItem.Psize") %>'></asp:Label>
<asp:Label ID="color" Visible="False" runat="server" text='<%#
DataBinder.Eval(Container, "DataItem.color") %>'></asp:Label> </
ItemTemplate>
Is there something I am doing wrong with the syntax of findcontrol for
the Psize. The code is almost identicle to the quantity (which works)
except that it is a label.
 

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

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top