Download Source Code : 265_RepeaterControl.zip
In this article I will try to explain you how you fill the Repeater control, on the selection of item from the DropDownlist Control. Here I have created application in ASP.Net 2.0 with Sql Server 2000
In ASP.Net and ASP.Net 2.0 we find very interesting and helpful Web Server Controls that provide great flexibility to user in binding the data with Database. Among these server controls we find Repeater control, this control almost behaves like DataList or DataGrid/GridView control. Repeater Control does not provide very much functionality like DataGrid/GridView e.g. paging and sorting, selection, editing, updating, etc. but in some scenarios it is quite helpful. For example we want to format our data using HTML tags; Repeater control is the only control that allows the developers to split HTML tags across templates.
Repeater control is efficient when you want to simply get the data from the database and display it on your web form. For Example, you are developing a website for any traveler agency and you want to display customer’s record (not allowing any modification/updating data) on the web form instead of using DataGrid/GridView control, repeater is efficient and simple to use.
Default syntax of the Repeater control is as follows:
Following are the members of the repeater control:
DataMember and DataSource: These properties allow the control to bind to the datasource. Note if there are more then one tables in DataSource then DataMember is used to identify the table to use.
Items: Identifies the item in the Repeater control it works as a collection of RepeatorItem objects.
DataBind(): Used to bind the Data with a DataSource.
ItemCreated Event: This event occurs when item is created and added to the Repeator Control.
ItemDataBound Event: Event occurs before rendering the item on the page. Note item is data bound before the occurrence of this event.
ItemCommand Event: Occurs on the click event of a button in Repeater control.
DataItem: Provides the data associated with this item.
ItemIndex: Indicates the index of an item in the Repeater control.
ItemType: Returns one of the ListItemType enumerated values identifying what template the item is in.
For presenting the retrieve data on the web form repeater control provides five templates. The brief description of these templates is as follows:
AlternatingItemTemplate: Used to define the format and layout of every odd item.
FooterTemplate: Used to display the content in footer. It is rendered once.
HeaderTemplate: Used to define heading of the repeater control.
ItemTemplate: Used to define the contents and layout of the items in the list.
SeparatorTemplate: Rendered between every item.
After the introduction of repeater control now we see its example. To create an application select create website option in Visual Studio 2005. Add three controls on your form Label, DropDownList and Repeater control. Source of your form will be look like as follows:
Now we look at the code behind portion i.e. .aspx.vb file. First of all Import two files that are System.Data and System.Data.SqlClient. Declare four global variables: