Cells.Controls

R

rn5a

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"
runat="server">

<Columns>

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

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

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

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

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

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

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

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

<asp:ButtonColumn CommandName="delete" HeaderText="DELETE"
Text="DELETE"/>

</Columns>

</asp:DataGrid>

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

This is the UpdateCommand event function named *UpdateCart*

Sub UpdateCart(obj As Object, ea As DataGridCommandEventArgs)
..........
..........
dgCart.DataBind()

Response.Write("Text: " & CType(ea.Item.Cells(5).Controls(0),
TextBox).Text & "<br>")
Response.Write("Val: " & CType(ea.Item.Cells(0).Controls(0),
Label).Text)
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
'System.Web.UI.WebControls.Label'.

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.
 

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

Similar Threads

Controls 2
BoundColumn 1
Append 0 0
Problem with Data Grid 0
ASP.NET Datagrid 1
DropDownList DataGrid 1
Error referencing Datagrid table cell 3
Controls in ItemTemplate in DataGrid 0

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top