ASP.Net Repeater Nested Using C#
Repeater Databound Control in ASP.Net 2.0 can be nested within other Repeater control to display the related database items from the relational SQL Database using C# code. To work with nested Repeater Control in ASP.Net C# code you must be familiar with FindControl function that provides the functionality to get the server control within the ItemTemplate of the Repeater Control. Here we will use FindControl Function to bind the related products table to a nested Repeater by passing the categoryId of outer Repeater control with Data Items of category table. To explain the example in this tutorial of Nested Repeater Control we have used the Northwind database of SQL Server 2000.
Repeater Examples:
You can see the live samples and examples of Repeater Control from the following links:
Repeater FindControl
FindControl function is used to find any control by its server ID reference. Using FindControl function you can find the nested Repeater control from the ItemTemplate cell of any row of ASP.Net Repeater control. Not even nested Repeater Control you can find any server control placed inside the ItemTemplate by passing the ID of the server control.
To access the properties of the server control returned by the FindControl function you have to type cast that control into its WebControls class. For example to type cast the searched Repeater control using FindControl function you can use ((Repeater) object.FindControl("RepeaterID") ) .
HTML Source Code for ASP.Net nested Repeater Control
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<h4><%#DataBinder.Eval(Container.DataItem, "CategoryName") %></h4>
<asp:HiddenField ID="hiddenCategoryID"
runat="server"
Value='<%#DataBinder.Eval(Container.DataItem, "CategoryID") %>' />
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "productName") %><br />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:Repeater>
ASP.Net C# code for Nested Repeater Control DataBinding
// SQL Select Command
SqlCommand mySqlSelect = new SqlCommand("select * from categories", mySQLconnection);
mySqlSelect.CommandType = CommandType.Text;
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlSelect);
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
// Repeater Control Databinding using Datasource
Repeater1.DataSource = myDataSet;
Repeater1.DataBind();
foreach (RepeaterItem repeaterItem in Repeater1.Items)
{
// SQL Select Command
mySqlSelect = new SqlCommand("select * from products where categoryId = @categoryId", mySQLconnection);
mySqlSelect.CommandType = CommandType.Text;
// Parameter passed as categoryID
// to get products that belong to the passed categoryId
// FindControl HiddenField used to retrieve the value of categoryId
mySqlSelect.Parameters.Add("@categoryId", SqlDbType.Int).Value = ((HiddenField)repeaterItem.FindControl( "hiddenCategoryID" )).Value;
mySqlAdapter = new SqlDataAdapter(mySqlSelect);
myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
// Databinding with Nested Repeater Control
((Repeater)(repeaterItem.FindControl( "Repeater2" ))).DataSource = myDataSet;
((Repeater)(repeaterItem.FindControl( "Repeater2" ))).DataBind();
}
Continue to next tutorial: ASP.Net Repeater Table to learn how to customize the appearance of Repeater control using HTML table layout.
