|
Resources » Code Snippets » Active Directory
Use Nested Repeater in ASP.NET 2.0 with C#
|
The given code is the example of nested repeater in Asp.net 2.0 with C#.
Hereby I have given the code for both HTML file and C# code behind file.
NestedRepeater.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Nestedrepeater.aspx.cs" Inherits="Nestedrepeater" %> <%@ Import Namespace="System.Data" %> <!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>Nestedrepeater</title> <style type="text/css"> <!-- body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; } a:link { color: #0000FF; } a:visited { color: #0000FF; } a:hover { color: #0000FF; text-decoration: none; } a:active { color: #0000FF; } .basix { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; } .header1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold; color: #006699; } .lgHeader1 { font-family: Arial, Helvetica, sans-serif; font-size: 18px; font-weight: bold; color: #0066CC; background-color: #CEE9FF; } --> </style> </head> <body> <form id="form1" runat="server"> <br /> <table align="center" border="0" cellpadding="0" cellspacing="0" width="752"> <tr bgcolor="#5482fc"> <td colspan="4"> <img height="1" src="/media/spacer.gif" width="1" /></td> </tr> <tr> <td bgcolor="#5482fc" width="1"> <img alt="Server Intellect" height="1" src="media/spacer.gif" width="1" /></td> <td width="250"> <a href="http://www.serverintellect.com"> <img alt="Server Intellect" border="0" height="75" src="media/logo.gif" width="250" /></a></td> <td bgcolor="#3399ff" width="500"> <a href="http://www.serverintellect.com"> <img alt="Server Intellect" border="0" height="75" src="media/headerR1.gif" width="500" /></a></td> <td bgcolor="#5482fc" width="1"> <img alt="Server Intellect" height="1" src="media/spacer.gif" width="1" /></td> </tr> <tr bgcolor="#5482fc"> <td colspan="4"> <img height="1" src="media/spacer.gif" width="1" /></td> </tr> </table> <div> <br /> <table align="center" bgcolor="#5482fc" border="0" cellpadding="5" cellspacing="1" width="600"> <tr> <td align="center" class="lgHeader1" height="50"> To display hierarchical data using nested Repeater Control (C#)</td> </tr> </table> <br /> <br /> <fieldset> <legend>Nested Repeater</legend> <table> <tr> <td><b>Show the author and his works from the databases of pubs</b></td></tr> <tr> <td align="center"> <asp:repeater id="parent" runat="server"> <itemtemplate> <b><%# DataBinder.Eval(Container.DataItem,"au_lname") %></b><br> <asp:repeater id="child" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>' runat="server"> <itemtemplate> <%# DataBinder.Eval(Container.DataItem, "[\"title\"]")%><br> </itemtemplate> </asp:repeater> </itemtemplate> </asp:repeater></td></tr> </table></fieldset> </div> <br /> <br /> <table align="center" cellpadding="0" cellspacing="0" width="500"> <tr> <td align="center" class="basix" height="50"> <strong>Power. Stability. Flexibility.</strong><br /> Hosting from <a href="http://www.serverintellect.com">Server Intellect</a><br /> <br /> For more ASP.NET Tutorials visit <a href="http://www.AspNetTutorials.com">www.AspNetTutorials.com</a></td> </tr> </table> </form> </body> </html>
NestedRepeater.aspx.cs
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;
public partial class Nestedrepeater : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //Create the connection and DataAdapter for the Authors table. SqlConnection cnn = new SqlConnection("server=(local);database=pubs;uid=sa;pwd=;"); SqlDataAdapter cmd1 = new SqlDataAdapter("select * from authors", cnn);
//Create and fill the DataSet. DataSet ds = new DataSet(); cmd1.Fill(ds, "authors");
//Create a second DataAdapter for the Titles table. SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titles,titleauthor,authors where titles.title_id=titleauthor.title_id and authors.au_id=titleauthor.au_id", cnn); cmd2.Fill(ds, "titles");
//Create the relation bewtween the Authors and Titles tables. ds.Relations.Add("myrelation",ds.Tables["authors"].Columns["au_id"],ds.Tables["titles"].Columns["au_id"]);
//Bind the Authors table to the parent Repeater control, and call DataBind. parent.DataSource = ds.Tables["authors"]; Page.DataBind();
//Close the connection. cnn.Close();
} }
|
Did you like this resource? Share it with your friends and show your love!
|
|
|
No responses found. Be the first to respond...
|
 Follow us on Twitter: https://twitter.com/dotnetspider
|
|