Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Templatefield Gridview

Status
Not open for further replies.

wallm

Programmer
Apr 4, 2005
69
GB
I converted a boundfield into a templatefield

The templatefield now looks like this

<asp:TemplateField HeaderText="Quantity">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>

</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

The sub below used to work for the boundfield

Protected Sub CartGrid_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)
Dim QuantityTextBox As TextBox = CType(CartGrid.Rows(e.RowIndex).Cells(2).Controls(0), TextBox)
Dim Quantity As Integer = Convert.ToInt32(QuantityTextBox.Text)

If Quantity = 0 Then
Profile.Cart.Items.RemoveAt(e.RowIndex)
Else
Profile.Cart.Items(e.RowIndex).Quantity = Quantity
End If

CartGrid.EditIndex = -1
BindGrid()
End Sub

But now it spits out the error

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.

thanks.
 
I imagine that "CartGrid.Rows(e.RowIndex).Cells(2).Controls(0)" isn't returning the control you are actually expecting it to (i.e. it is returning a Literal control rather than a TextBox). I suggest you make sure you are getting a reference to the correct control.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I've been trying to do that without any success.
 
You'll just have to debug thr project and see what controls are at what position and then use the relevant index.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top