R
rn5a
Suppose I have the following DataGrid:
<asp
ataGrid 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
ataGrid>
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?
<asp
<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
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?