473,386 Members | 1,785 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,386 software developers and data experts.

Controls

A DataGrid displays 8 columns - ProductID, ProductName, Description,
Price, Quantity, Sub-Total, Edit & Delete. *Edit* is a
EditCommandColumn, *Delete* is a ButtonColumn & *Quantity* is a
BoundColumn. If the *Edit* link is clicked, the *Quantity* column
changes to the editable mode with a TextBox & the text under the
*Edit* column of the corresponding row gets replaced by 2 hyperlinks -
Update & Cancel.

This is the DataGrid code:

<asp:DataGrid ID="dgCart" OnEditCommand="EditCart"
OnCancelCommand="CancelUpdate" OnDeleteCommand="DeleteFromCart"
OnUpdateCommand="UpdateCart" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="ProductID">
<ItemTemplate>
<asp:Label ID="lblPID" Text=<%# Container.DataItem("ProductID") %>
runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="ProductName">
<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") & ".
00" %runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="Quantity" DataField="Quantity"/>
<asp:TemplateColumn HeaderText="Sub-Total">
<ItemTemplate>
<asp:Label ID="lblSubTotal" Text=<%# Container.DataItem("UnitPrice") *
Container.DataItem("Quantity") & ".00" %runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
HeaderText="Edit" UpdateText="Update"/>
<asp:ButtonColumn CommandName="delete" HeaderText="Delete"
Text="Delete" runat="server"/>
</Columns>
</asp:DataGrid>

Assume that the DataGrid displays 5 rows. Also assume that under the
*Price* column, the 4th item is 200 & under the *Quantity* column, the
4th item is 10. To retrieve these 2 values in the OnUpdateCommand
event function of the DataGrid, the following code will suffice:

============================
Sub UpdateCart(obj As Object, ea As DataGridCommandEventArgs)
Response.Write("Price: " & CType(ea.Item.Cells(3).Controls(1),
Label).Text)
Response.Write("Quantity: " & CType(ea.Item.Cells(4).Controls(0),
TextBox).Text)
End Sub
============================

The above code correctly retrieves the price as 200 & the quantity as
10 but what I don't understand is to get the price, why I have to use
Controls(1) but to get the quantity, I have to use Controls(0). Can
someone please explain me this?

Moreover if I use Controls(0) to retrieve the price like this:

============================
Response.Write("Price: " & CType(ea.Item.Cells(3).Controls(0),
Label).Text)
============================

then ASP.NET generates the following error:

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

On the other hand, if I use Controls(1) to retrieve the quantity like
this:

============================
Response.Write("Quantity: " & CType(ea.Item.Cells(4).Controls(1),
TextBox).Text)
============================

then ASP.NET generates the following error:

Specified argument was out of the range of valid values. Parameter
name: index.

How do I understand when to use Controls(0) & when to use Controls(1)?

Someone please explain me this. I am getting too confused.

Feb 23 '07 #1
2 2290
On Feb 23, 6:49 am, r...@rediffmail.com wrote:
A DataGrid displays 8 columns - ProductID, ProductName, Description,
Price, Quantity, Sub-Total, Edit & Delete. *Edit* is a
EditCommandColumn, *Delete* is a ButtonColumn & *Quantity* is a
BoundColumn. If the *Edit* link is clicked, the *Quantity* column
changes to the editable mode with a TextBox & the text under the
*Edit* column of the corresponding row gets replaced by 2 hyperlinks -
Update & Cancel.

This is the DataGrid code:

<asp:DataGrid ID="dgCart" OnEditCommand="EditCart"
OnCancelCommand="CancelUpdate" OnDeleteCommand="DeleteFromCart"
OnUpdateCommand="UpdateCart" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="ProductID">
<ItemTemplate>
<asp:Label ID="lblPID" Text=<%# Container.DataItem("ProductID") %>
runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="ProductName">
<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") & ".
00" %runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="Quantity" DataField="Quantity"/>
<asp:TemplateColumn HeaderText="Sub-Total">
<ItemTemplate>
<asp:Label ID="lblSubTotal" Text=<%# Container.DataItem("UnitPrice") *
Container.DataItem("Quantity") & ".00" %runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
HeaderText="Edit" UpdateText="Update"/>
<asp:ButtonColumn CommandName="delete" HeaderText="Delete"
Text="Delete" runat="server"/>
</Columns>
</asp:DataGrid>

Assume that the DataGrid displays 5 rows. Also assume that under the
*Price* column, the 4th item is 200 & under the *Quantity* column, the
4th item is 10. To retrieve these 2 values in the OnUpdateCommand
event function of the DataGrid, the following code will suffice:

============================
Sub UpdateCart(obj As Object, ea As DataGridCommandEventArgs)
Response.Write("Price: " & CType(ea.Item.Cells(3).Controls(1),
Label).Text)
Response.Write("Quantity: " & CType(ea.Item.Cells(4).Controls(0),
TextBox).Text)
End Sub
============================

The above code correctly retrieves the price as 200 & the quantity as
10 but what I don't understand is to get the price, why I have to use
Controls(1) but to get the quantity, I have to use Controls(0). Can
someone please explain me this?

Moreover if I use Controls(0) to retrieve the price like this:

============================
Response.Write("Price: " & CType(ea.Item.Cells(3).Controls(0),
Label).Text)
============================

then ASP.NET generates the following error:

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

On the other hand, if I use Controls(1) to retrieve the quantity like
this:

============================
Response.Write("Quantity: " & CType(ea.Item.Cells(4).Controls(1),
TextBox).Text)
============================

then ASP.NET generates the following error:

Specified argument was out of the range of valid values. Parameter
name: index.

How do I understand when to use Controls(0) & when to use Controls(1)?

Someone please explain me this. I am getting too confused.
Can someone please explain me?

Feb 24 '07 #2
On Feb 24, 5:47 pm, r...@rediffmail.com wrote:
On Feb 23, 6:49 am, r...@rediffmail.com wrote:


A DataGrid displays 8 columns - ProductID, ProductName, Description,
Price, Quantity, Sub-Total, Edit & Delete. *Edit* is a
EditCommandColumn, *Delete* is a ButtonColumn & *Quantity* is a
BoundColumn. If the *Edit* link is clicked, the *Quantity* column
changes to the editable mode with a TextBox & the text under the
*Edit* column of the corresponding row gets replaced by 2 hyperlinks -
Update & Cancel.
This is the DataGrid code:
<asp:DataGrid ID="dgCart" OnEditCommand="EditCart"
OnCancelCommand="CancelUpdate" OnDeleteCommand="DeleteFromCart"
OnUpdateCommand="UpdateCart" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="ProductID">
<ItemTemplate>
<asp:Label ID="lblPID" Text=<%# Container.DataItem("ProductID") %>
runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="ProductName">
<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") & ".
00" %runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="Quantity" DataField="Quantity"/>
<asp:TemplateColumn HeaderText="Sub-Total">
<ItemTemplate>
<asp:Label ID="lblSubTotal" Text=<%# Container.DataItem("UnitPrice") *
Container.DataItem("Quantity") & ".00" %runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
HeaderText="Edit" UpdateText="Update"/>
<asp:ButtonColumn CommandName="delete" HeaderText="Delete"
Text="Delete" runat="server"/>
</Columns>
</asp:DataGrid>
Assume that the DataGrid displays 5 rows. Also assume that under the
*Price* column, the 4th item is 200 & under the *Quantity* column, the
4th item is 10. To retrieve these 2 values in the OnUpdateCommand
event function of the DataGrid, the following code will suffice:
============================
Sub UpdateCart(obj As Object, ea As DataGridCommandEventArgs)
Response.Write("Price: " & CType(ea.Item.Cells(3).Controls(1),
Label).Text)
Response.Write("Quantity: " & CType(ea.Item.Cells(4).Controls(0),
TextBox).Text)
End Sub
============================
The above code correctly retrieves the price as 200 & the quantity as
10 but what I don't understand is to get the price, why I have to use
Controls(1) but to get the quantity, I have to use Controls(0). Can
someone please explain me this?
Moreover if I use Controls(0) to retrieve the price like this:
============================
Response.Write("Price: " & CType(ea.Item.Cells(3).Controls(0),
Label).Text)
============================
then ASP.NET generates the following error:
Unable to cast object of type 'System.Web.UI.LiteralControl' to type
'System.Web.UI.WebControls.Label'.
On the other hand, if I use Controls(1) to retrieve the quantity like
this:
============================
Response.Write("Quantity: " & CType(ea.Item.Cells(4).Controls(1),
TextBox).Text)
============================
then ASP.NET generates the following error:
Specified argument was out of the range of valid values. Parameter
name: index.
How do I understand when to use Controls(0) & when to use Controls(1)?
Someone please explain me this. I am getting too confused.

Can someone please explain me?- Hide quoted text -

- Show quoted text -
Isn't there anyone who can explain me this? I thought this must be one
of the most basic questions in ASP.NET!

Feb 28 '07 #3

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

Similar topics

16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
0
by: Mark Johnson | last post by:
Sometimes Controls that have been added to a GroupBox do not show up. What I am doing : 1) I am not using the designer, but create all the Controls per hand: groupBoxProdukt_01 = new...
3
by: Steve Drake | last post by:
All, I have a CONTROL that contains 1 control (Control ONE), the 1 control that it can contain 1 or 2 control (Control A and B). Control A, raises and event and Control ONE receives this event...
1
by: Robert Howells | last post by:
Perhaps I'm just too new at this to pull it off, or perhaps it's just bad architecture. I'd appreciate some feedback on the the wisdom (or lack thereof) in attempting the following: I'm not new...
10
by: Sacha Korell | last post by:
I'm trying to load a drop-down list with all DropDownList control names from another page. How would I be able to find those DropDownList controls? The FindControl method will only find a...
6
by: dhnriverside | last post by:
Hi peeps, I'm trying to create some controls textboxes at runtime, based on the number of items in a IETreeView that are checked. That I can do, I've got a place holder and I can create the...
66
by: Cor | last post by:
Hi, I start a new thread about a discussion from yesterday (or for some of us this morning). It was a not so nice discussion about dynamically removing controls from a panel or what ever. It...
7
by: Mike Bulava | last post by:
I have created a base form that I plan to use throughout my application let call the form form1. I have Built the project then add another form that inherits from form1, I add a few panel controls...
15
by: Arpan | last post by:
Consider the following code which retrieves data from a SQL Server 2005 DB table & displays it in a DataGrid: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)...
8
by: Ryan | last post by:
Ok.. I have a form with lots of stuff on it; a tool strip panel, menu strip, data binding elements (dataset, binding source, table adapter), tab control with 7 tab pages, each page contains a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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...

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.