Controls in ItemTemplate in DataGrid

Discussion in 'ASP .Net' started by rn5a@rediffmail.com, Nov 30, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bennett Haselton
    Replies:
    1
    Views:
    5,328
    Joe Fallon
    Sep 24, 2004
  2. Ned Balzer
    Replies:
    4
    Views:
    2,443
    Ned Balzer
    Aug 3, 2006
  3. AC [MVP MOSS]
    Replies:
    2
    Views:
    532
    AC [MVP MOSS]
    Feb 13, 2007
  4. Nathan Sokalski
    Replies:
    2
    Views:
    1,110
    Coskun SUNALI [MVP]
    Dec 26, 2007
  5. Andy B
    Replies:
    1
    Views:
    986
    bruce barker
    Nov 15, 2008
Loading...

Share This Page