Controls in ItemTemplate in DataGrid

R

rn5a

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?
 

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

Similar Threads

Controls 2
Controls 3
Cells.Controls 0
ItemTemplate Problem 0
Controls In DataGrid? 1
Controls? 15
link textboxes in gridview EditItem template using javascript 1
format datetime in GridView ItemTemplate? 2

Members online

No members online now.

Forum statistics

Threads
473,828
Messages
2,569,736
Members
45,518
Latest member
vlonesshirt

Latest Threads

Top