Hi vijaykrishnan...,
Refer below sample code.
HTML
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css" />
<script type="text/javascript">
$(function () {
$("div.accordian").accordion({
collapsible: true,
active: false,
autoHeight: false
});
});
</script>
<asp:Repeater ID="rptAccordian" runat="server" OnItemDataBound="OnItemDataBound">
<ItemTemplate>
<div class="accordian">
<h3><a class="" href="#"><%#Eval("Title") %></a></h3>
<div class="accordian">
<asp:Repeater ID="rptSubAccordian" runat="server" OnItemDataBound="OnSubItemDataBound">
<ItemTemplate>
<h3><a class="" href="#"><%#Eval("Level") %></a></h3>
<div class="accordian">
<asp:Repeater ID="rptSubSubAccordian" runat="server">
<ItemTemplate>
<h3><a class="" href="#"><%#Eval("Content") %></a></h3>
<div><%#Eval("ContentElement") %></div>
</ItemTemplate>
</asp:Repeater>
</div>
<asp:HiddenField ID="hfLevel" runat="server" Value=' <%# Eval("Level") %>' />
<asp:HiddenField ID="hfSubTitle" runat="server" Value=' <%# Eval("Title") %>' />
</ItemTemplate>
</asp:Repeater>
</div>
<asp:HiddenField ID="hfTitle" runat="server" Value=' <%# Eval("Title") %>' />
</div>
</ItemTemplate>
</asp:Repeater>
Namespaces
C#
using System.Data;
using System.Linq;
VB.Net
Imports System.Data
Imports System.Linq
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
var distinctTitle = (from data in GetData().AsEnumerable()
select data["Title"]).Distinct().ToList();
DataTable dt = new DataTable();
dt.Columns.Add("Title");
foreach (var item in distinctTitle)
{
dt.Rows.Add(item);
}
rptAccordian.DataSource = dt;
rptAccordian.DataBind();
}
}
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string title = ((HiddenField)e.Item.FindControl("hfTitle")).Value;
var subSection = (from data in GetData().AsEnumerable()
where data["Title"].Equals(title)
select new { Title = data["Title"], Level = data["Level"] }).Distinct();
DataTable dt = new DataTable();
dt.Columns.Add("Title");
dt.Columns.Add("Level");
foreach (var item in subSection)
{
dt.Rows.Add(item.Title, item.Level);
}
Repeater rptSubAccordian = (Repeater)e.Item.FindControl("rptSubAccordian");
rptSubAccordian.DataSource = dt;
rptSubAccordian.DataBind();
}
}
protected void OnSubItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string title = ((HiddenField)e.Item.FindControl("hfSubTitle")).Value;
string level = ((HiddenField)e.Item.FindControl("hfLevel")).Value;
var subSubSection = (from data in GetData().AsEnumerable()
where data["Level"].Equals(level) && data["Title"].Equals(title)
select new { Content = data["Content"], ContentElement = data["ContentElement"] }).Distinct();
DataTable dt = new DataTable();
dt.Columns.Add("Content");
dt.Columns.Add("ContentElement");
foreach (var item in subSubSection)
{
dt.Rows.Add(item.Content, item.ContentElement);
}
Repeater rptSubSubAccordian = (Repeater)e.Item.FindControl("rptSubSubAccordian");
rptSubSubAccordian.DataSource = dt;
rptSubSubAccordian.DataBind();
}
}
private static DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("Title"),
new DataColumn("Level"),
new DataColumn("Content"),
new DataColumn("ContentElement")
});
dt.Rows.Add("Section 1", "CATEGORY 1", "This is content of Section 1.1", "<p>Element of Section 1.1</p>");
dt.Rows.Add("Section 1", "CATEGORY 1", "This is content of Section 1.2", "<p>Element of Section 1.2</p>");
dt.Rows.Add("Section 1", "CATEGORY 2", "This is content of Section 2.1", "<p>Element of Section 2.1</p>");
return dt;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim distinctTitle = (From data In GetData().AsEnumerable() Select data("Title")).Distinct().ToList()
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Title")
For Each item In distinctTitle
dt.Rows.Add(item)
Next
rptAccordian.DataSource = dt
rptAccordian.DataBind()
End If
End Sub
Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim title As String = (CType(e.Item.FindControl("hfTitle"), HiddenField)).Value
Dim subSection = (From data In GetData().AsEnumerable() Where data("Title").Equals(title) Select New With {Key
.Title = Data("Title"), Key
.Level = Data("Level")
}).Distinct()
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Title")
dt.Columns.Add("Level")
For Each item In subSection
dt.Rows.Add(item.Title, item.Level)
Next
Dim rptSubAccordian As Repeater = CType(e.Item.FindControl("rptSubAccordian"), Repeater)
rptSubAccordian.DataSource = dt
rptSubAccordian.DataBind()
End If
End Sub
Protected Sub OnSubItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim title As String = (CType(e.Item.FindControl("hfSubTitle"), HiddenField)).Value
Dim level As String = (CType(e.Item.FindControl("hfLevel"), HiddenField)).Value
Dim subSubSection = (From data In GetData().AsEnumerable() Where data("Level").Equals(level) AndAlso data("Title").Equals(title) Select New With {Key
.Content = Data("Content"), Key
.ContentElement = Data("ContentElement")
}).Distinct()
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Content")
dt.Columns.Add("ContentElement")
For Each item In subSubSection
dt.Rows.Add(item.Content, item.ContentElement)
Next
Dim rptSubSubAccordian As Repeater = CType(e.Item.FindControl("rptSubSubAccordian"), Repeater)
rptSubSubAccordian.DataSource = dt
rptSubSubAccordian.DataBind()
End If
End Sub
Private Shared Function GetData() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Title"), New DataColumn("Level"), New DataColumn("Content"), New DataColumn("ContentElement")})
dt.Rows.Add("Section 1", "CATEGORY 1", "This is content of Section 1.1", "<p>Element of Section 1.1</p>")
dt.Rows.Add("Section 1", "CATEGORY 1", "This is content of Section 1.2", "<p>Element of Section 1.2</p>")
dt.Rows.Add("Section 1", "CATEGORY 2", "This is content of Section 2.1", "<p>Element of Section 2.1</p>")
Return dt
End Function
Screenshot