473,401 Members | 2,068 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,401 software developers and data experts.

Controls in ItemTemplate in DataGrid

Suppose I have the following DataGrid:

<asp:DataGrid ID="dgUsers" OnUpdateCommand="UpdateDG" runat="server">
<Columns>
<TemplateColumn HeaderText="NAME">
<ItemTemplate>
<asp:Label ID="lblFName" runat="server"><%# Container.DataItem("FName")
%></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFName" Text='<%# Container.DataItem("FName") %>'
runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn....UpdateText="Update"/>
</Columns>
</asp:DataGrid>

When the DataGrid is in the editable mode, the Label control changes to
a TextBox control. When the Update link is clicked, the Text in the
TextBox in the UpdateCommand event handler named "UpdateDG" can be
accessed using the following code:

Sub UpdateDG(obj As Object, ea As DataGridCommandEventArgs)
Response.Write(CType(ea.Item.Cells(0).Controls(1), TextBox).Text)
End Sub

If I am not mistaken, Controls(1) has been used above because the
TextBox happens to be the second control in the Cells(0) Controls
collection (the Label being the first).

But if I add another Label control immediately after the Label named
"lblFName" within the same ItemTemplate like this (keeping the rest of
the DataGrid as it is)

<ItemTemplate>
<asp:Label ID="lblFName" runat="server"><%# Container.DataItem("FName")
%></asp:Label>
<asp:Label ID="lblLName" runat="server"><%# Container.DataItem("LName")
%></asp:Label>
</ItemTemplate>

then when the DataGrid is in the editable mode, shouldn't the Text in
the TextBox be accessed in the sub "UpdateDG" (when the Update link is
clicked) using

Sub UpdateDG(obj As Object, ea As DataGridCommandEventArgs)
Response.Write(CType(ea.Item.Cells(0).Controls(2), TextBox).Text)
End Sub

But it generates the error saying

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

Why is this error getting generated? Changing Controls(2) to
Controls(1) gets the correct TextBox value when the Update link is
clicked but doesn't Controls(1) refer to the Label named "lblLName" in
the Cells(0) Controls collection?

In other words, doesn't adding the Label named "lblLName" immediately
after the Label named "lblFName" in Cells(0) mean that "lblFName" is
Controls(0) in the Cells(0) Controls collection, "lblLName" is
Controls(1) in the Cells(0) Controls collection & "txtFName" is
Controls(2) in the Cells(0) Controls collection?

Can someone please explain me why am I wrong?

Nov 30 '06 #1
0 2652

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

Similar topics

8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
0
by: bill yeager | last post by:
I have a datalist displaying parent information and a datagrid presenting child information. The data is being rendered just fine. The datagrid has template columns on it that I cannot gain access...
1
by: bill yeager | last post by:
I did some more debugging and found the following: 1) I placed the following code in the button event just to see if I could cycle thru the datagrid control collection: <code> Dim strhello As...
12
by: Daniel Walzenbach | last post by:
Hi, I want to display a Label in a DataGrid according to some condition. I therefore check whether the condition is true in the ItemDateBound EventHandler of the DataGrid. Unfortunately the...
0
by: Sue | last post by:
I'm missing something very simple here, and I need an answer fairly quick if possible. I'm behind on this project now because of this... Code examples below question... I have a datagrid (DG)...
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)...
0
by: Rocky | last post by:
Dear ALL, I am facing a bit problem in DATAGRID. so may be you guys can help me out. Actually on a DataGrid I am having following controls: 1) 2 DropDown Menu 2) 1 Text Box 3) 1 ADD Button 4)...
2
by: rn5a | last post by:
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...
0
by: rn5a | last post by:
This is the code of a DataGrid: <asp:DataGrid ID="dgCart" OnEditCommand="EditCart" OnCancelCommand="CancelUpdate" OnDeleteCommand="DeleteFromCart" OnItemCommand="ButtonClicked"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
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...
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.