Controls

R

rn5a

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

rn5a

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?
 
R

rn5a

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!
 

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

Top