spacer
spacer

Visual Basic & VB.NET resources | VBprofs.com

Google
 
Main Menu
Home
IT News
Articles
Source Codes
Books
Calendar of Events
Publish your Content
Links Directory
Reviews and Recs
Premium Content
 VBprofs Premium Content Become a member to get access to our premium content! 

 Membership is entirely free, and allows you to:
  •  access programming projects posted by our partners;
  •  publish your articles, news, links;
  •  publish a project request;
  •  access our VB Careers section.
Polls
How did you reach VBprofs.com?
  
Which is your favorite programming language?
  
Buy Text Link Ad
LinkWorth Stats
Check our stats on LinkWorth. If you like what you see, signup with LinkWorth and buy a link with us.
Related Items
 
Home arrow Articles arrow Visual Basic / .NET Articles arrow Nesting repeaters in .NET

Nesting repeaters in .NET PDF Print E-mail
Articles - Visual Basic / .NET Articles
Written by Hawkfield   
VB .NET articlesData repeaters in .NET are very usefull to display database records onto screen. But usually, in a real world situation, you don't have enough functionality with 1 repeater.

Take for example some kind of menu structure. You want to display a category, but every category can have 0 or more subcategories. With 1 repeater, this cannot be done in a simple way. In .NET, you can use nested repeaters for this.

Take the code below in the .aspx file:

... other HTML code ...


<'a href="category.aspx?category=<%# DataBinder.Eval(Container.DataItem, "Category") %>"><%# DataBinder.Eval(Container.DataItem, "Category") %>


... other HTML code ...

Here you will get a list of categories. If you want to add subcategories, one could try to make the code below work.

... other HTML code ...

<'a href="category.aspx?category=<%# DataBinder.Eval(Container.DataItem, "Category") %>"><%# DataBinder.Eval(Container.DataItem, "Category") %>


<'a href="category.aspx?category=<%# DataBinder.Eval(Container.DataItem, "SubCategory") %>"><%# DataBinder.Eval(Container.DataItem, "SubCategory") %>




... other HTML code ...

To make things work, you have to change several things though.
In your .aspx.cs file (or in the script area if you are not using codebehind files) you will have the following code:

... other c# code ...
DataSet sqlDS = new DataSet();

/* Get the categories */
string Query = "select CategoryId, Category from Categories order by Category";
SqlCommand sqlC = new SqlCommand(Query, objConn);
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = sqlC;

sqlDA.Fill(sqlDS, "Category"); /// Our dataset contains the categories

/* Get the subcategories */
string Query = "select SubCategoryId, CategoryId, SubCategory from SubCategories order by SubCategory";
sqlC = new SqlCommand(Query, objConn);
sqlDA.SelectCommand = sqlC;

sqlDA.Fill(sqlDS, "SubCategory"); /// Our dataset contains the categories

/* Add a relationship between the 2 tables
In our case, every SubCategory has a CategoryId to pojnt out there parent */ ds.Relations.Add("CategoryRelation", ds.Tables["Category"].Columns["CategoryId"],
ds.Tables["SubCategory"].Columns["CategoryId"]);

Category.DataBind();
... other c# code ...

In your .aspx file you will have to change the DataSource to your child repeater (SubCategory) and the syntax in which you get the data. The code below does so:

... other HTML code ...
<%@ Import Namespace="System.Data" %>



<'a href="category.aspx?category=<%# DataBinder.Eval(Container.DataItem, "Category") %>"><%# DataBinder.Eval(Container.DataItem, "Category") %>
"><%# DataBinder.Eval(Container.DataItem, "Category") %>
"><%#
((DataRow)Container.DataItem).GetParentRow("CategoryRelation")["Category"] %> - <%#
DataBinder.Eval(Container.DataItem, "[\"SubCategory\"]") %>




... other HTML code ...

This code is both easy to maintain, and performant. The same principles count for other items bound to database data, like the DataGrid for example.
Hawkfield.Tech: Article based resource site
Hawkfield: Web design and development

< Prev   Next >

 
Antivirus Shop
BitDefender Antivirus v10
Newsflash

PowerRefresh 1.0 released!
A simple yet extremely useful tool for webmasters, publishers and SEO working bees: PowerRefresh allows you to automatically refresh your IE windows every x minutes. It can handle unlimited number of windows and, unlike similar applications, is using full browsers.


Get PowerRefresh from Kaloyani.com: download
Login Form
Username

Password

Remember me
Forgotten your password?
No account yet? Create one
Popular

VBprofs.com - online resources for Visual Basic and VB.NET professionals: Visual Basic and VB.NET articles, industry news and events, career tools, VB / VB.NET books, calendar and much more. VBprofs.com is an interactive web site with free membership.
(c) Copyright 2005 - 2006 by VBprofs.com
powered by Mambo Open Source Software
spacer