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.
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") ) .
<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">
<%#DataBinder.Eval(Container.DataItem, "productName") %><br />
</ItemTemplate>
</asp:Repeater>
// 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);
// 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();
// Databinding with Nested Repeater Control ((Repeater)(repeaterItem.FindControl( "Repeater2" ))).DataSource = myDataSet;
((Repeater)(repeaterItem.FindControl( "Repeater2" ))).DataBind();
}
You can download the complete ASP.Net C# Source code for Nested Repeater control here:
nested-repeater.zip (2.90 kb)
Be the first to rate this post
Tags: asp.net 2.0, c#, asp.net repeater control, asp.net repeater nested, asp.net repeater databinding, asp.net sample, asp.net sql connectionstring, asp.net repeater dynamic, asp.net nested repeater, asp.net repeater table, asp.net repeater div, web.config connectionstrings, c# code, c# repeater control nested
8/23/2008 9:07:42 PM