473,748 Members | 8,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cells.Controls

This is the code of a DataGrid:

<asp:DataGrid ID="dgCart" OnEditCommand=" EditCart"
OnCancelCommand ="CancelUpda te" OnDeleteCommand ="DeleteFromCar t"
OnItemCommand=" ButtonClicked" OnPreRender="Ch angeWidth"
OnUpdateCommand ="UpdateCart " AutoGenerateCol umns="false"
runat="server">

<Columns>

<asp:TemplateCo lumn HeaderText="S.N .">
<ItemTemplate >
<asp:Label ID="lblSNo" Text='<%# Container.ItemI ndex + 1 %>'
runat="server"/>.
</ItemTemplate>
</asp:TemplateCol umn>

<asp:TemplateCo lumn HeaderText="ID" >
<ItemTemplate >
<asp:Label ID="lblPID" Text='<%# Container.DataI tem("ProductID" ) %>'
runat="server"/>
</ItemTemplate>
</asp:TemplateCol umn>

<asp:TemplateCo lumn HeaderText="NAM E">
<ItemTemplate >
<asp:Label ID="lblPName" Text='<%# Container.DataI tem("ProductNam e")
%>' runat="server"/>
</ItemTemplate>
</asp:TemplateCol umn>

<asp:TemplateCo lumn HeaderText="DES CRIPTION">
<ItemTemplate >
<asp:Label ID="lblDescript ion" Text='<%#
Container.DataI tem("ProductDes cription") %>' runat="server"/>
</ItemTemplate>
</asp:TemplateCol umn>

<asp:TemplateCo lumn HeaderText="PRI CE">
<ItemTemplate >
<asp:Label ID="lblPrice" Text='<%# Container.DataI tem("UnitPrice" ) %>'
runat="server"/>
</ItemTemplate>
</asp:TemplateCol umn>

<asp:BoundColum n DataField="Quan tity" HeaderText="QTY ."/>

<asp:TemplateCo lumn HeaderText="SUB-TOTAL">
<ItemTemplate >
<asp:Label ID="lblSubTotal " Text='<%# Container.DataI tem("Total") %>'
runat="server"/>
</ItemTemplate>
</asp:TemplateCol umn>

<asp:EditComman dColumn EditText="EDIT" CancelText="CAN CEL"
HeaderText="EDI T" UpdateText="UPD ATE"/>

<asp:ButtonColu mn CommandName="de lete" HeaderText="DEL ETE"
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 DataGridCommand EventArgs)
..........
..........
dgCart.DataBind ()

Response.Write( "Text: " & CType(ea.Item.C ells(5).Control s(0),
TextBox).Text & "<br>")
Response.Write( "Val: " & CType(ea.Item.C ells(0).Control s(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.Lab el'.

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 1494

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

Similar topics

0
1229
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
2375
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 some cases i can access the values with e.Item.Cells.Text, but in other cases i have to do weird thinks like the following: TextBox pp; //I know...disgusting!!! pp= (TextBox) e.Item.Cells.Controls ; int id = Convert.ToInt32(pp.Text );
2
3680
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 seem to work right, the loop appears below: for each cntl in page.controls(1).controls ddlId = cntl.Id
1
1794
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; However, I can't seem to apply the same technique to the datalist as ³Cells² is not implemented. How does one retrieve the text from a datalist cell then? Thanks, Stephan.
2
2704
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
1727
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 like it to do, but is it possible and how can I place an image in a web-datagrid cell? rg,
0
1638
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 have a FK relationship on CAR_ID so the oracledataadapter1 select statement(CommandText) is: select car_master.car_id, car_master.year,car_master.vehicle,car_detail.car_id AS EXPR1,
1
5922
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. Thanks in advance for any help or information!!
1
2058
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 retaining the newly added row with the filled in textboxes on the next button click. I think I am handling my viewstate incorrectly. This code adds the new row: Private Sub addRowsInTable() Dim TextID As Integer Dim TextIDCount...
0
8984
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.