Controls In DataGrid?

Discussion in 'ASP .Net Datagrid Control' started by Arpan, Aug 6, 2006.

  1. Arpan

    Arpan Guest

    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)
    Dim dSet As DataSet
    Dim sqlConn As SqlConnection
    Dim sqlDapter As SqlDataAdapter

    sqlConn = New SqlConnection("Data Source=AD\SQLEXPRESS;Initial
    Catalog=MyDB;Integrated Security=True")
    sqlDapter = New SqlDataAdapter("SELECT * FROM Users", sqlConn)

    dSet = New DataSet()
    sqlDapter.Fill(dSet, "Users")

    dgUsers.DataSource = dSet.Tables("Users").DefaultView
    dgUsers.DataBind()
    End Sub

    Sub EditUsers(ByVal obj As Object, ByVal ea As
    DataGridCommandEventArgs)
    dgUsers.EditItemIndex = ea.Item.ItemIndex
    dgUsers.DataBind()

    Response.Write(Control0: ")
    Response.Write(ea.Item.Cells(0).Controls(0))

    Response.Write("<br>Control1: ")
    Response.Write(ea.Item.Cells(0).Controls(1))

    Response.Write("<br>Control2: ")
    Response.Write(ea.Item.Cells(0).Controls(2))

    Response.Write("<br>Control Count: " &
    ea.Item.Cells(0).Controls.Count)
    Response.Write("<br>Cell Count: " & ea.Item.Cells.Count)

    'Response.Write("<br>Control3: ")
    'Response.Write(ea.Item.Cells(0).Controls(3))
    End Sub
    </script>

    <form runat="server">
    <asp:DataGrid ID="dgUsers" OnEditCommand="EditUsers"
    AutoGenerateColumns="false" runat="server">
    <Columns>
    <asp:TemplateColumn HeaderText="NAME">
    <ItemTemplate>
    <asp:Label ID="lblName" runat="server"><%#
    Container.DataItem("FirstName") %> <%#
    Container.DataItem("LastName") %></asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn HeaderText="ADDRESS" DataField="Address"/>
    <asp:BoundColumn HeaderText="CITY" DataField="City"/>
    <asp:BoundColumn HeaderText="STATE" DataField="State"/>
    <asp:BoundColumn HeaderText="ZIP" DataField="Zip"/>
    <asp:EditCommandColumn HeaderText="EDIT" CancelText="CANCEL"
    EditText="EDIT" UpdateText="UPDATE"/>
    </Columns>
    </asp:DataGrid>
    </form>

    Assume that the DataGrid displays 5 records. When I click the "Edit"
    link corresponding to the, say, 1st record, apart from all fields
    (editable & non-editable) displayed in the DataGrid, the first 4 pairs
    of Response.Write lines within the sub "EditUsers" produce the
    following output:

    Control0: System.Web.UI.LiteralControl
    Control1: System.Web.UI.WebControls.Label
    Control2: System.Web.UI.LiteralControl
    Control Count: 3
    Cell Count: 6

    Can someone please explain me how ASP.NET computes the control count to
    3? The Label control under the 1st column named "NAME" makes up 1 of
    the 3 controls but what/where are the other 2 Literal controls?

    Thanks,

    Arpan
    Arpan, Aug 6, 2006
    #1
    1. Advertising

  2. The easiest way to check for yourself is to put trace="true" in <%@ page
    %>.

    You'll see all the controls and their containers in a nice grid so you can
    count for yourself.

    "Arpan" <> wrote in message
    news:...
    > 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)
    > Dim dSet As DataSet
    > Dim sqlConn As SqlConnection
    > Dim sqlDapter As SqlDataAdapter
    >
    > sqlConn = New SqlConnection("Data Source=AD\SQLEXPRESS;Initial
    > Catalog=MyDB;Integrated Security=True")
    > sqlDapter = New SqlDataAdapter("SELECT * FROM Users", sqlConn)
    >
    > dSet = New DataSet()
    > sqlDapter.Fill(dSet, "Users")
    >
    > dgUsers.DataSource = dSet.Tables("Users").DefaultView
    > dgUsers.DataBind()
    > End Sub
    >
    > Sub EditUsers(ByVal obj As Object, ByVal ea As
    > DataGridCommandEventArgs)
    > dgUsers.EditItemIndex = ea.Item.ItemIndex
    > dgUsers.DataBind()
    >
    > Response.Write(Control0: ")
    > Response.Write(ea.Item.Cells(0).Controls(0))
    >
    > Response.Write("<br>Control1: ")
    > Response.Write(ea.Item.Cells(0).Controls(1))
    >
    > Response.Write("<br>Control2: ")
    > Response.Write(ea.Item.Cells(0).Controls(2))
    >
    > Response.Write("<br>Control Count: " &
    > ea.Item.Cells(0).Controls.Count)
    > Response.Write("<br>Cell Count: " & ea.Item.Cells.Count)
    >
    > 'Response.Write("<br>Control3: ")
    > 'Response.Write(ea.Item.Cells(0).Controls(3))
    > End Sub
    > </script>
    >
    > <form runat="server">
    > <asp:DataGrid ID="dgUsers" OnEditCommand="EditUsers"
    > AutoGenerateColumns="false" runat="server">
    > <Columns>
    > <asp:TemplateColumn HeaderText="NAME">
    > <ItemTemplate>
    > <asp:Label ID="lblName" runat="server"><%#
    > Container.DataItem("FirstName") %> <%#
    > Container.DataItem("LastName") %></asp:Label>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > <asp:BoundColumn HeaderText="ADDRESS" DataField="Address"/>
    > <asp:BoundColumn HeaderText="CITY" DataField="City"/>
    > <asp:BoundColumn HeaderText="STATE" DataField="State"/>
    > <asp:BoundColumn HeaderText="ZIP" DataField="Zip"/>
    > <asp:EditCommandColumn HeaderText="EDIT" CancelText="CANCEL"
    > EditText="EDIT" UpdateText="UPDATE"/>
    > </Columns>
    > </asp:DataGrid>
    > </form>
    >
    > Assume that the DataGrid displays 5 records. When I click the "Edit"
    > link corresponding to the, say, 1st record, apart from all fields
    > (editable & non-editable) displayed in the DataGrid, the first 4 pairs
    > of Response.Write lines within the sub "EditUsers" produce the
    > following output:
    >
    > Control0: System.Web.UI.LiteralControl
    > Control1: System.Web.UI.WebControls.Label
    > Control2: System.Web.UI.LiteralControl
    > Control Count: 3
    > Cell Count: 6
    >
    > Can someone please explain me how ASP.NET computes the control count to
    > 3? The Label control under the 1st column named "NAME" makes up 1 of
    > the 3 controls but what/where are the other 2 Literal controls?
    >
    > Thanks,
    >
    > Arpan
    >
    Ken Cox [Microsoft MVP], Aug 7, 2006
    #2
    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. trinitypete

    HTML Controls and User controls

    trinitypete, Jun 25, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    3,360
    trinitypete
    Jun 26, 2003
  2. thegame
    Replies:
    1
    Views:
    933
    =?Utf-8?B?ZGlyazQx?=
    Feb 28, 2005
  3. Nathan Sokalski
    Replies:
    4
    Views:
    1,136
    Muhammad Naveed Yaseen
    Sep 5, 2007
  4. Andreas Klemt
    Replies:
    0
    Views:
    271
    Andreas Klemt
    Oct 8, 2003
  5. Replies:
    1
    Views:
    324
Loading...

Share This Page