Home > SOA Tips > .NET Developer > Working with the repeater
SOA Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

.NET DEVELOPER

Working with the repeater


Jason Lefebvre
11.05.2002
Rating: --- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



.NET Developer Tip
(Receive this column in your inbox,
click Edit your Profile to subscribe.)

Working with the repeater
Jason Lefebvre

ADO.NET, a revolutionary step forward in accessing and manipulating data, and its set of supporting display controls, called list controls, are both incredible. But if you're going to be developing for the .NET world, you'll have to know how to use them. This tip, excerpted from InformIT, discusses one of these controls, the repeater, and offers a listing illustrating its use.


The Repeater is the most basic of the built-in list control objects, yet offers a great deal of flexibility. To format the data it displays, the Repeater uses templates. Templates are blocks of HTML mixed with server logic that define how each row in the data collection will appear when sent to the client. Templates are defined within the body of the control instantiated within the Web form.

There are a number of different types of templates. The ItemTemplate, as seen in Listing 11.1, is responsible for formatting each record in the collection. The AlternatingItemTemplate lets you define a special appearance for every other item in the collection. A typical use of this would be to define a slightly different color for alternating items, to make the records displayed easier to read. The Repeater also supports templates that enable you to define a header and footer as well as a separator that is inserted between each item displayed.

The listing shows how to use the Repeater list control to display a list of categories from the database. The server-side code should look familiar. ADO.NET calls a stored procedure named Categories_Get, which returns a resultset containing the CategoryID and CategoryName for each item in the Categories table of the Northwind database. During data binding, the Repeate


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
Microsoft .NET Web services
How do I balance throughput requirements and interoperability?
APM software traces transactions across tiers, technologies
How you can learn M Grammar for Oslo modeling
Legacy modernization opens Windows for publisher
Former .NET Web developers ride Ruby and Rails application framework
Microsoft Oslo at PDC: Dial 'M' for modeling language
Yahoo proxy fight looms
New Microsoft site for architects
LAMP coders go hybrid route
Silverlight shines on bank RIAs
Microsoft .NET Web services Research

Platforms and Servers
Tracking down managed memory leaks
Handling exceptions in .NET
.NET Compact Framework graphics
The Data Access Application Block
Decision time: .NET or J2EE?
A great .NET resource: .Net2TheMax
Delegates vs. interfaces in .NET
Project structure best practices
Working with PDFs in a .NET environment
Displaying errors with the error provider

.NET Developer
Programming Indigo
DataSets and Web services don't mix
Security in .NET 2.0
Tracking down managed memory leaks
Handling exceptions in .NET
.NET Compact Framework graphics
The Data Access Application Block
A great .NET resource: .Net2TheMax
Delegates vs. interfaces in .NET
Project structure best practices

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Common Language Infrastructure  (SearchSOA.com)
Visual J#  (SearchSOA.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


r uses the ItemTemplate to format the data being returned from the database. In this case, the data returned is used to create a set of hyperlinks for the categories.

Inside the ItemTemplate, notice that to insert fields from the resultset, you use the DataBinder.Eval() method. The template will insert the value of the field from the data source where this tag is placed.

NOTE
The category links generated in Listing 11.1 reference a page named Products.aspx. [If] this page does not exist in [y]our application, the link will generate an error. However, it's rather easy to create a page that accepts the selected category ID and returns a list of products for that category.

Listing: Generating a List of Categories Using the Repeater List Control

As mentioned, the lsting generates a list of categories using the Repeater Web control. Lines 11–29 query the database, retreive a resultset, and then bind the data to the Repeater. In this example, the actual loading and binding of data has been moved into a separate method named LoadGridData(). This convention will be used more often for the remainder of [this article], because many examples retrieve data from multiple sources, and this helps to make the code more readable. As you'll notice in line 24, we're using a stored procedure to generate the resultset. This resultset is no different than one you'd get by manually building the SELECT SQL query manually.

A Repeater is placed on the Web form in lines 40–47. Until now, you've only seen data displayed using a DataGrid or a label Web control. The Repeater is similar to the DataGrid, in that it is used to display and format data returned from the data source. Lines 4–46 define the ItemTemplate for the Repeater. The ItemTemplate contains the HTML that is generated for each row in the resultset returned from the data source. In this case, it builds a hyperlink for each category. The DataBinder.Eval() method is used to insert the value of the field from the resultset into the HTML output.


To read the article from which this tip comes, click over to InformIT. You have to register there, but the registration is free.

For More Information:


Rate this Tip
To rate tips, you must be a member of SearchSOA.com.
Register now to start rating these tips. Log in if you are already a member.




DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SOA Trends and Strategy - SOA Education, SOA Development, SOA Implementations
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2001 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts