| An introduction to Data-Bound ASP.NET List Controls | ||||||||||||||||||||||||||
| First steps in using the Repeater ASP.NET web server control. Learn how to data bind multi-record controls. Understanding the DataBinder class and the DataBinder.Eval method. | ||||||||||||||||||||||||||
Welcome to the 21st chapter of the FREE online programming course: Back in the last two chapters, we've looked at two different approaches to data binding in ASP.NET web applications:
Throughout this chapter, you'll learn about the following:
Despite their similarities, each of these Web controls has their individual advantages and disadvantages. In designing data-driven Web applications, ASP.NET developers often find themselves wondering which of these three controls is the "best" one.
An example: creating an ordered list output using the Repeater control
If the items you use in the example output above (simple read only output of HTML) come from some datasource, your best option is to use the Repeater control.
The ordered list HTML element is "opened" by the "<ol>" - you might think of this as the "header" When such an output is required using the Repeater control, the HeaderTemplate, ItemTemplate and FooterTemplate can be specified.
Repeater in Delphi ASP.NET applications Start Delphi, open your ASP.NET test project and add a new web form to it. Name the unit "wfRepeaterExample" (the web page class is "TwfRepeaterExample"). ![]() The "aspx/html" view is very simple:
Repeater.DataSource Let's create a custom class that will be added to the ArrayList used as the Repeater's DataSource.
The TColorEntry class has one property named "ColorValue" and one function named "ToHTML". The constructor of the TColorEntry receives a System.Drawing.Color value. An ArrayList of TColorEntry objects will be used as the DataSource property for the Repeater control. Define a variable called "ColorList" and a procedure "BuildColorList" in the private section of the TwfBinderSimple interface.
Uh, oh. Now back to web page programming. In the web form's Page_Load handler add the following:
By now, you already should be familiar with the code above, if you have followed the chapters of this course. Quick clarification: only on the first visit to the page, the ColorList ArrayList object is filled with TColorEntry instances, assigned as the Repeater1's DataSource. Note: if you run the page now, you'll get NO html output - this is because you have not specified at least the ItemTemplate for the Repeater. Let's do that now. As the "Repeater" in the design mode suggests, switch to the aspx view, let the Repeater's asp.net markup look like:
The HTML above defines the HeaderTemplate (opening tag "<ol>"), the ItemTemplate (item: "<li></li>") and the FooterTemplate (closing tag "</ol>"). If you switch to design view, the design time representation of the Repeater finally has some HTML shape. However, there's still NO mention of our TColorValue objects - this is where we introduce the DataBinder class. The DataBinder class Switch again to the "aspx" view and add the following binding expression into the Repeater's ItemTemplate:
What happens at run time? For each of the items in the assigned DataSource - the ColorList ArrayList, since an item is an object of the TColorEntry type, the DataBinder class with its Eval method gets the value from the TColorEntry's property named "ColorValue". This is what you get when you run the page, finally:
Note 1: DataBinder.Eval performs late-bound evaluation, using reflection. First the "Container.DatatItem" is casted to "TColorEntry", then the value for the item's "ColorValue" property is retrieved. Omitting DataBinder, casting to the correct class A much better approach would be to first cast the "Container.DataItem" to the correct class and then use any of the public properties/functions in the binding expression:
In the above version of the Repeater's ItemTemplate, each item has been assigned a "style" attribute with a specific CSS "background-color" value. Once the Container.DataItem has been casted to TColorEntry, the ToHTML() function is called. The final HTML result (at run time) is:
Ah, I almost forgot ... you might receive the following exception (at run time): Now what?! Well, to tell the long story in short, you need to add a special page directive on top of the aspx page:
The "Import namespace" directive, explicitly imports a namespace into a page, making all classes and of the imported namespace available to the page. The namespace where the TColorEntry class is defined is "wfRepeaterExample" - the name of the unit where all the code from this example is defined. To the next chapter: A Beginner's Guide to ASP.NET Programming for Delphi developersThat's it for today. In the next chapter we'll continue exploring the topic of data binding in ASP.NET.If you need any kind of help at this point, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts. A Beginner's Guide to ASP.NET Programming for Delphi developers: Next Chapter >>
|
||||||||||||||||||||||||||


