By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,724 Members | 863 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,724 IT Pros & Developers. It's quick & easy.


P: n/a
This is the code of a DataGrid:

<asp:DataGrid ID="dgCart" OnEditCommand="EditCart"
OnCancelCommand="CancelUpdate" OnDeleteCommand="DeleteFromCart"
OnItemCommand="ButtonClicked" OnPreRender="ChangeWidth"
OnUpdateCommand="UpdateCart" AutoGenerateColumns="false"


<asp:TemplateColumn HeaderText="S.N.">
<asp:Label ID="lblSNo" Text='<%# Container.ItemIndex + 1 %>'

<asp:TemplateColumn HeaderText="ID">
<asp:Label ID="lblPID" Text='<%# Container.DataItem("ProductID") %>'

<asp:TemplateColumn HeaderText="NAME">
<asp:Label ID="lblPName" Text='<%# Container.DataItem("ProductName")
%>' runat="server"/>

<asp:TemplateColumn HeaderText="DESCRIPTION">
<asp:Label ID="lblDescription" Text='<%#
Container.DataItem("ProductDescription") %>' runat="server"/>

<asp:TemplateColumn HeaderText="PRICE">
<asp:Label ID="lblPrice" Text='<%# Container.DataItem("UnitPrice") %>'

<asp:BoundColumn DataField="Quantity" HeaderText="QTY."/>

<asp:TemplateColumn HeaderText="SUB-TOTAL">
<asp:Label ID="lblSubTotal" Text='<%# Container.DataItem("Total") %>'

<asp:EditCommandColumn EditText="EDIT" CancelText="CANCEL"
HeaderText="EDIT" UpdateText="UPDATE"/>

<asp:ButtonColumn CommandName="delete" HeaderText="DELETE"



Note that only the *QTY* column is a BoundColumn. Hence when a user
clicks *EDIT*, this column becomes editable i.e. it changes to a

This is the UpdateCommand event function named *UpdateCart*

Sub UpdateCart(obj As Object, ea As DataGridCommandEventArgs)

Response.Write("Text: " & CType(ea.Item.Cells(5).Controls(0),
TextBox).Text & "<br>")
Response.Write("Val: " & CType(ea.Item.Cells(0).Controls(0),
End Sub

The first Response.Write line outputs the correct text. So for e.g.
when in the editable mode, if a user enters 10 in the TextBox of the
BoundColumn, the first Response.Write correctly outputs 10.

Using the second Response.Write line, I want to retrieve the text of
the row under the *S.N.* column that was just updated but it generates
the following error:

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

If I am not mistaken, ASP.NET adds a LiteralControl on either side of
a Label control. In other words, a Label control is always sandwiched
between 2 LiteralControls. Please correct me if I am wrong. & I guess
that's the reason why the above error gets thrown but if I change the
index of the Cells in the second Response.Write line from 0 to 1 or 2
or 3 or 4 to retrieve the *ID*, *NAME*, *DESCRIPTION* & *PRICE*
respectively, then ASP.NET still generates the above error!

Now why changing the Cells index in the second Response.Write line
from 0 to 1 or 2 or 3 or 4 generating the same error?

I know I can use FindControl instead of using the Cells & Controls
index but I would like to know what's causing the above error when the
Cells index is changed from 0 to 1 or 2 or 3 or 4.

Mar 12 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.