In this article, I’ll show you how I used it to create the left side menu for my own site, which looks like this:
I store the menu categories in a table called “
Sub_Category” in SQL Server, so that if I ever need to add one, I just add it to the table and it will appear on the menu. There are two fields in the table:
STEP 1 — CREATE THE PAGE AND INSERT THE
Repeatercontrol allows you to create templates to define the layout of its content. The templates are:
ItemTemplate— Use this template for elements that are rendered once per row of data.
AlternatingItemTemplate— Use this template for elements that are rendered every other row of data. This allows you to alternate background colors, for example.
HeaderTemplate— Use this template for elements that you want to render once before your
FooterTemplate— Use this template for elements that you want to render once after your
SeperatorTemplate— Use this template for elements to render between each row, such as line breaks.
Here is a part of the Web Form (subcategories.aspx) that contains the
Repeaterhas a name of “
catlist“. It uses the
HeaderTemplateto print out the
Areasimage. It then uses the
ItemTemplateto display a
Hyperlinkcontrol that has our data in it. We’ll come back to this in Step Two.
FooterTemplateis not necessary, but I put it in here for consistency.
STEP 2 — GET THE DATA
Now let’s look at the data retrieval. Here is the
Page_Loadevent in the Code Behind file.
The first five lines open a database connection and retrieve the contents of the
Sub_Categorytable. The last two lines bind our
Repeatercontrol to the
DataReader. Now, let’s look again at the
DataBindmethod of the
Repeatercontrol is called, ASP.NET will loop through the
DataReaderand populate the
Repeaterwith the data we specify. The
Databinder.Evalmethod uses reflection to parse and evaluate a data-binding expression against an object at run time, in this case the object is our
Repeater. So this line of code:
will render the contents of the
"Sub_Category_ID"field for each row in the
If you spend much time with ASP.NET, you will certainly be using this control often. I hope you find it handy! -james haron