Home > Archive > ASP .NET > February 2007 > Controls
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| rn5a@rediffmail.com 2007-02-22, 10:08 pm |
| 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 .
| |
| rn5a@rediffmail.com 2007-02-24, 8:11 am |
| 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 .
Can someone please explain me?
| |
| rn5a@rediffmail.com 2007-02-28, 7:10 pm |
| On Feb 24, 5:47 pm, r...@rediffmail.com wrote:
> On Feb 23, 6:49 am, r...@rediffmail.com wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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!
|
|
|
|
|