473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Cells.Controls

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.

Mar 12 '07 #1
0 1469

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kumar | last post by:
try this Dim mycheck As CheckBox = CType(dgWelcomePacks.Items (i).Cells(7).Controls(1), CheckBox) regards, Kumar. >-----Original Message-----
5
by: Ariel Gimenez | last post by:
Morning! After several hours breaking my mind, finally my code works, but i think is trash-code, can someone tellme how is the correct way to access the value of my cells within a datagrid?... in...
2
by: JDavies | last post by:
Hi All I am dynamically Creating DropDownList boxes for a Survey form. These DropdownLists are added to Table Cells programatically. I tried to loop thru the controls on the form but it doesnt...
1
by: Stephan Bour | last post by:
I need to build a string from the content of a datalist containing a nested datagrid. This properly returns the data from the first cell in the datagrid: String first = GridItem.Cells.Text; ...
2
by: Craig Buchanan | last post by:
Is there a way to add a row to a datagrid during the ItemDataBound event? I would like to add a 'header' row before the start of a group of rows. Thanks, Craig Buchanan
4
by: EMW | last post by:
For my ASP.NET program I want to use the datagrid as a sort of planning tool. Based on the information in a XML file, a cell of the datagrid must show an image... Well, this is what I would...
0
by: cwbp17 | last post by:
I'm having trouble updating individual datagrid cells. Have two tables car_master (columns include Car_ID, YEAR,VEHICLE) and car_detail (columns include Car_ID,PRICE,MILEAGE,and BODY);both tables...
1
by: jobs | last post by:
Can somebody tell me why Jobno and Value BoundFields are not available during the command SaveParm ? i enter text into the box for Value and hit the Save button, but all I see is the Parmvalue. ...
1
by: bgernon | last post by:
I have a table that consists of two rows with three cells each row. The cells contain textboxes. I am able to successfully add a new row with textboxes when a button is clicked. The problem is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.