?
Solved

Embed Datalist in another one, ASP.NET

Posted on 2009-07-04
2
Medium Priority
?
413 Views
Last Modified: 2012-05-07
Hello,
I want to have in a ASP.NET web form, 2 Datalists, DL1, and DL2.
I want to put them like in parent child relationship, so DL2 will be embedded in DL1,
and for each parent record displayed in DL1 there is child records underlying in DL2.

I have a function in a vb class that will return data regarding the parent ID entered as a parameter for the function.  FC (parentID).

The parent Datalist DL1 is bound to an object data source. ODS1.

Can someone tell me please if i can do this?
If yes, can you please write me the asp.net code, and the method to make this work.
How can i bound the DL2 to data?
thanks
0
Comment
Question by:Nyana22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Assisted Solution

by:jinal
jinal earned 600 total points
ID: 24778698

/* ASPX File */
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default9.aspx.cs" Inherits="Default9" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server">            
            <ItemTemplate>
                    <table width="100%">
                        <tr>
                            <td>
                            <%# DataBinder.Eval(Container.DataItem,"Name").ToString() %> 
                            </td>
                        </tr>
                        <tr>
                            <td>
                            <asp:DataList ID="ChildDataList" runat="server" DataSource='<%# GetChildTable(int.Parse(DataBinder.Eval(Container.DataItem,"ID").ToString())) %>' >
                                <HeaderTemplate>
                                    <table width="90%" style="padding-left:100px">
                                </HeaderTemplate>
                                <ItemTemplate>                                    
                                            <tr>
                                                <td>
                                                 <%# DataBinder.Eval(Container.DataItem,"ID").ToString() %> 
                                                </td>
                                                <td>
                                                <%# DataBinder.Eval(Container.DataItem,"OrderID").ToString() %> 
                                                </td>
                                            </tr>                                    
                                </ItemTemplate>
                                <FooterTemplate>
                                    </table>
                                </FooterTemplate>
                            </asp:DataList>
                            </td>
                        </tr>
                    </table>
            </ItemTemplate>
        </asp:DataList>
    </div>
    </form>
</body>
</html>
 
/* Code Behind */
 
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
 
public partial class Default9 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataList1.DataSource = CreateParentTable();
            DataList1.DataBind();
        }
    }
 
    private DataTable  CreateParentTable()
    {   
 
        DataTable dtParent = new DataTable("Parent");
        dtParent.Columns.Add("ID");
        dtParent.Columns.Add("Name");
 
        DataRow dr = dtParent.NewRow();
        dr["ID"] = "1";
        dr["Name"] = "test1";
        dtParent.Rows.Add(dr);
 
        dr = dtParent.NewRow();
        dr["ID"] = "2";
        dr["Name"] = "test2";
        dtParent.Rows.Add(dr);
 
        return dtParent;
    }
 
    protected DataTable GetChildTable(int ParentID)
    {
         // Right down your own code to return datatable.
        // I jsut create temporary table for test.
        DataTable dtChild = new DataTable("Child");
        dtChild.Columns.Add("ID");
        dtChild.Columns.Add("OrderID");
 
        DataRow dr = dtChild.NewRow();
        dr["ID"] = ParentID.ToString();
        dr["OrderID"] = "1";
        dtChild.Rows.Add(dr);
 
        dr = dtChild.NewRow();
        dr["ID"] = ParentID.ToString();
        dr["OrderID"] = "2";
        dtChild.Rows.Add(dr);
 
        return dtChild;
    }
}

Open in new window

0
 
LVL 12

Accepted Solution

by:
wht1986 earned 1400 total points
ID: 24778709
Handle the item data bound event to bind the nested data list.

In the following example, the class "TestClass" has a method which returns some child objects.  The aspx page has a data list with a nested data list. On the bind, i find the control of the nested data list and call the data items method to use as a data source on that nested list.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:DataList ID="DataList1" runat="server" >
        <ItemTemplate> ID: <asp:Label ID="IDLABEL" runat="server"  Text='<%# Eval("ID") %>' />
            <br />
            NAME: <asp:Label ID="NAMELABLE" runat="server"  Text='<%# Eval("Name") %>' />
            <br />
            <asp:DataList ID="DataList2" runat="server" >
                <ItemTemplate>
                    ID:<asp:Label ID="CHILDIDLABEL" runat="server" Text='<%# Eval("ID") %>' />
                    <br />
                    Name: <asp:Label ID="CHILDNAMELABEL" runat="server"  Text='<%# Eval("Name") %>' />
                    <br />
                </ItemTemplate>
            </asp:DataList>
            <br />
        </ItemTemplate>
    </asp:DataList>
    </form>
</body>
</html>
 
==========================
 
 
Partial Class _Default
    Inherits System.Web.UI.Page
 
    Public Class TestClass
 
        Private _ID As String = ""
        Private _Name As String = ""
 
        Public Property ID() As String
            Get
                Return _ID
            End Get
            Set(ByVal value As String)
                _ID = value
            End Set
        End Property
 
        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property
 
        Public Sub New(ByVal a As String, ByVal b As String)
            Me.ID = a
            Me.Name = b
        End Sub
 
 
        Public Function GetChildren() As List(Of TestClass)
            Dim list As List(Of TestClass) = New List(Of TestClass)
            For i As Integer = 1 To 3
                list.Add(New TestClass(Me.ID + "-" + i.ToString(), "TEST " + i.ToString()))
            Next
            Return list
        End Function
 
    End Class
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        If Not IsPostBack Then
            Dim list As List(Of TestClass) = New List(Of TestClass)
            list.Add(New TestClass("1", "LARRY"))
            list.Add(New TestClass("2", "CURLY"))
            list.Add(New TestClass("3", "MOE"))
            DataList1.DataSource = list
            DataList1.DataBind()
        End If
    End Sub
 
    Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
        Dim tc As TestClass = e.Item.DataItem
        Dim dl2 As DataList = e.Item.FindControl("DataList2")
        dl2.DataSource = tc.GetChildren()
        dl2.DataBind()
    End Sub
 
End Class

Open in new window

0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question