C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Interview   Jobs   Projects   Offshore Development    
Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing | Talk to Us |



My Profile

Gifts

Active Members
TodayLast 7 Days more...









Nested ASP.NET DataRepeater controls in ASP.NET


Posted Date: 18 Jul 2008    Resource Type: Articles    Category: Web Applications

Posted By: Brainstorming Guy       Member Level: Diamond
Rating:     Points: 15



In this article we are going to see a small demo on how we can nest a data repeater inside another. We are going to show the customer details with the products they purchased. We are going to work with some classes whose definitions are given below.

Products:

public class Product
{
public Product()
{

}

public Product(string productName, double price, double quantity)
{
_productName = productName;
_price = price;
_quantity = quantity;
_total = _price * _quantity;
}
private string _productName;
private double _price;
private double _quantity;
private double _total;

public double Total
{
get { return _total; }
set { _total = value; }
}

public string ProductName
{
get { return _productName; }
set { _productName = value; }
}
public double Price
{
get { return _price; }
set { _price = value; }
}
public double Quantity
{
get { return _quantity; }
set { _quantity = value; }
}
}


Customer class is given below:


public class Customer
{
private string _customerName;
private List<Product> _orderedProducts;

public string CustomerName
{
get { return _customerName; }
set { _customerName = value; }
}
public List<Product> OrderedProducts
{
get { return _orderedProducts; }
set { _orderedProducts = value; }
}
}


The Customer class is going to hold the list of Products which he/she ordered for. We generate the customer objects with a sample function. The function is given below.


private List<Customer> GetAllCustomers()
{
List<Customer> customers = new List<Customer>();
Customer customer = new Customer();
customer.CustomerName = "Venkatarajan";
customer.OrderedProducts = new List<Product>();
customer.OrderedProducts.Add(new Product("Boost", 12.23, 33));
customer.OrderedProducts.Add(new Product("Dabur Honey", 220, 3));
customer.OrderedProducts.Add(new Product("Kelogg's", 10, 7));
customers.Add(customer);

customer = new Customer();
customer.CustomerName = "Sowdeswari Venkatarajan";
customer.OrderedProducts = new List<Product>();
customer.OrderedProducts.Add(new Product("Saffron", 325, 3));
customer.OrderedProducts.Add(new Product("Badam", 220, 2));
customer.OrderedProducts.Add(new Product("Pista", 110, 7));
customers.Add(customer);

return customers;
}


You can talk to the database to load the details. Now, we will move to the ASPX page design with repeaters. We are going to place 2 repeaters. A repeater inside another. Yes! That is the power of data repeaters than any other control. Programmers can easily customize the repeater in the way they want it.


<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
<h2>Customer Details</h2>
</HeaderTemplate>
<ItemTemplate>
<asp:Repeater ID="rptPricingDetails" runat="server" OnItemDataBound="rptPricingDetails_ItemDataBound">
<HeaderTemplate>
<table width="50%" border="1" style="border-color: Navy" >
<tr style=" background-color: Navy; color: White; font-weight:bold">
<td style="width: 100%" colspan="4"><asp:Literal runat="server" ID="ltlCustomerName"></asp:Literal></td>
</tr>
<tr>
<td align="center">Product Name</td>
<td align="center">Price</td>
<td align="center">Quantity</td>
<td align="center">Total</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="left" style="width: 25%">
<%#DataBinder.Eval(Container.DataItem, "ProductName")%></td>
<td align="right" style="width: 25%" >
<%#DataBinder.Eval(Container.DataItem, "Price", "{0:n}")%>
</td>
<td align="right" style="width: 25%">
<%#DataBinder.Eval(Container.DataItem, "Quantity","{0:n}")%>
</td>
<td align="right" style="width: 25%">
<%#DataBinder.Eval(Container.DataItem, "Total", "{0:n}")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>


If you see the ASPX code, you can see a repeater, Repeater1, which contains the rptPricingDetails in its ItemTemplate. The repeater, rptPricingDetails which I placed is going to repeat for a Customer Details and Repeater for each customers. I’m going to bind the data


Repeater1.DataSource = allCustomers;
Repeater1.DataBind();


The binding is handled with the ItemDataBound event. SetValueForChildRepeater() method will bind child records (Products collection) to the child repeater.

e.Item.DataItem is the object which we are binding it to the Repeater. In this case, it is a Customer object.


protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// Check whether item is of type Item or Alternating Item.
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Put the Customer object into session. We will be
// getting this session value in the Child Repeater
Session["Customer"] = e.Item.DataItem as Customer;
// Call this method, it will bind the value to
// to the child repeater.
SetValueForChildRepeater(e.Item.DataItem, e.Item.FindControl("rptPricingDetails"));
}
}


The code for, SetValueForChildRepeater is given below.


private void SetValueForChildRepeater(object customerObject, object secondRepeater)
{
// type cast the object which is passed to
// to the type Customer
Customer customer = customerObject as Customer;
if (customer != null)
{
// typecast the object to Repeater. This is the child
// repeater
Repeater repeater = secondRepeater as Repeater;
if (repeater != null)
{
// set the object which is passed to the repeater.
// rows will be repeated for each and every object
// exists in the collection.
repeater.DataSource = customer.OrderedProducts;
repeater.DataBind();
}
}
}


In this method, we are going to bind the Products collection to the Repeater, rptPricingDetails.


repeater.DataSource = customer.OrderedProducts;


This is going to bind the Products collection to the second repeater, rptPricingDetails. Finally we are going to handle a small thing which will show the Customer Name.


protected void rptPricingDetails_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
Customer customer = Session["Customer"] as Customer;
if (customer != null)
{
Literal ltlCustomerName = e.Item.FindControl("ltlCustomerName") as Literal;
ltlCustomerName.Text = customer.CustomerName;
}
}
}


In this event, we are just going to check for the Header and we will be showing the Customer Name there.

This is all about using the DataRepeater inside a DataReapter. DataRepeaters are extremely customizable and anyone can customize the DataRepeater the way they want. There are still lots to learn about DataRepeaters.


Brainstorming Guy aka Venkatarajan A


Attachments







Responses

Author: Brainstorming Guy    18 Jul 2008Member Level: Diamond   Points : 2
Removed this section. I edited in the main content.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Nested DataRepeater in ASP.NET  .  Nested DataRepeater  .  DataRepeater inside DataRepeater  .  DataRepeater inside another  .  DataRepeater  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Data Validation Controls in ASP.NET 2.0 Part 2
Previous Resource: What are different IIS isolation levels?
Return to Discussion Resource Index
Post New Resource
Category: Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

UK Conference calling Company

Contact Us    Privacy Policy    Terms Of Use