ASP.Net Nested DataList Control using C#

Updated on 23 Apr 2009, Published on 23 Apr 2009

DataList control in ASP.Net 2.0 can by nested inside the other DataList control to display the related items of any item retrieved from relational SQL database using C# code. To work with nested DataList control in ASP.Net 2.0 using C# you must be familiar with Type casting of control objects, DataKeyField and DataKeys array collection of DataKey values for each list item of DataList and FindControl function.

DataList DataKeyField Property:
DataKeyField property of DataList control enables you to store the unique value of each record retrieved and bound to DataList control. For example in the HTML code provided with this tutorial you can see that CategoryID is set as DataKeyField value to store the unique value for each list item of DataLsit control. DataKeyField populates the DataKeys array collection that can be access in C# or VB code.

DataList DataKeys:
To get the value of DataKeyField for any particular list item of DataList control using C# code you can use DataKeys array by passing the Item Index of DataList control to get the unique identity of that record.

DataList FindControl:
FindControl function is used to find any control by passing its server ID reference as string value. Using FindControl function you can find the nested DataList control from the ItemTemplate of any list item of ASP.Net DataList control.

To work with nested DataList Control you can use the Northwind database. Consider the SQL database table categories and products to display the records. For example, you can display the category names as Title and related products in the nested DataList control.

HTML Source Code for Nested DataList Controls


<asp:DataList
    ID="parentDataList"
    runat="server"
    DataKeyField="CategoryID">
    <ItemTemplate>
   
    <h2><%# DataBinder.Eval(Container.DataItem, "CategoryName") %></h2>
    
    <asp:DataList
        ID="nestedDataList"
        runat="server">
        <ItemTemplate>
            <%# DataBinder.Eval(Container.DataItem, "ProductName") %>
            $<%# DataBinder.Eval(Container.DataItem, "UnitPrice") %>
        </ItemTemplate>
        <HeaderStyle Font-Bold="true" Font-Names="Arial" />
        <ItemStyle Font-Names="Arial" Font-Size="Small" />
    </asp:DataList>
   
    <hr />
    </ItemTemplate>
    <HeaderStyle Font-Bold="true" Font-Names="Arial" />
    <ItemStyle Font-Names="Arial" Font-Size="Small" />
 </asp:DataList>

Next Tutorial: ASP.Net Nested DataList Controls C# DataBinding code.

In the next part of this tutorial we will discuss about the C# code for binding the SQL data to both the DataList controls that is parent as well as nested DataList control.

1 Responses to "ASP.Net Nested DataList Control using C#"
jfleier
this has been most helpful for what I am attempting to do. Thanks
Leave a Reply

Name   (Required)


Mail   (will not be published) (Required)


Website   (http://www.example.com)




OR Subscribe via Email: