Nested Repeater
DataBase:
Table Name: authors
ColumnName
|
DataType
|
| ID |
Int(set identity property=true)
|
| Name |
varchar(50)
|
Table Name: titles
ColumnName
|
DataType
|
| ID |
Int(set identity property=true)
|
| titles |
varchar(50)
|
aid
|
Int (Foreign Key)
|
<asp:Repeater ID="rptP" runat="server">
<HeaderTemplate>
<h2>
Author List</h2>
<ul>
</HeaderTemplate>
<ItemTemplate>
<b><%#DataBinder.Eval(Container.DataItem, "author") %></b>
<br />
<asp:Repeater runat="server" ID="rptC"
DataSource='<%# ((DataRowView)Container.DataItem).Row
.GetChildRows("myrelation") %>'>
<ItemTemplate>
<ul>
<li>
<%#DataBinder.Eval(Container.DataItem, "[\"titles\"]") %>
<br />
</li>
</ul>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
Code Behind Fle:
public partial class repeater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection("Data Source=SRIDITYA-PC;Initial Catalog=MYDB;Integrated Security=True");
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", cnn);
cmd2.Fill(ds, "titles");
//Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("myrelation",
ds.Tables["authors"].Columns["id"],
ds.Tables["titles"].Columns["aid"]);
//Bind the Authors table to the parent Repeater control, and call DataBind.
rptP.DataSource = ds.Tables["authors"];
rptP.DataBind();
//Close the connection.
cnn.Close();
}
}
Please Make Sure to Include this line in the HTML file: <%@ Import Namespace="System.Data" %>
No comments:
Post a Comment