Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
Working with DropDownList Control and Repeater Control in ASP.Net 2.0

Author: Faraz
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:

 

<asp:Repeater ID="ControlID" runat="server">

 

 <AlternatingItemTemplate />

 <FooterTemplate />

 <HeaderTemplate />

 <ItemTemplate />

 <SeparatorTemplate />

 

</asp:Repeater>

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:

 

<asp:Label ID="lblMessage" Text="Select CustomerID to View Details: " runat="server" /><br /><br /> 

 

<asp:DropDownList ID="ddlCustomerID" DataValueField="CustomerID" AutoPostBack="true" DataTextField="CustomerID"  runat="server">

</asp:DropDownList><br />

<br /><br />

 

<asp:Repeater ID="rcCustomerDetail" runat="server" >

 

<HeaderTemplate>

   <table border="0">

      <tr>

       <th>ContactName</th>

       <th>ContactTitle</th>

       <th>Country</th>

       <th>Phone</th>

      </tr>

</HeaderTemplate>

 

<ItemTemplate>

<tr>

 <th><%#Container.DataItem("ContactName")%></th>

 <th><%#Container.DataItem("ContactTitle")%></th>

 <th><%#Container.DataItem("Country") %></th>

       <th><%#Container.DataItem("Phone") %></th>

      </tr>         

</ItemTemplate>

 

<FooterTemplate >

      <tr>

       <th>Simple Way to Display Data in Repeater Control</th>

      </tr>

</FooterTemplate>

 

</asp:Repeater>  

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:

Dim DBConn As New SqlConnection("Server=localhost;Password=XXX;Persist Security Info=False;User ID=XXX;Initial Catalog=Northwind;Data Source=YourDataSource")

Dim DBAdap As New SqlDataAdapter

Dim DS As New DataSet

Dim DBCmd As New SqlCommand

Dim DR As SqlDataReader

On the Page_load event open the connection and fill the DropDownList control by getting the CustomerID from the database:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

 

DBConn.Open()

If Not IsPostBack Then

      DBAdap = New SqlDataAdapter("Select CustomerID from Customers", DBConn)

      DBAdap.Fill(DS, "Customers")

      ddlCustomerID.DataSource = DS.Tables("Customers").DefaultView

      ddlCustomerID.DataBind()

      ddlCustomerID.Items.Add(New ListItem("", ""))

      ddlCustomerID.SelectedValue = ""

End If

 

End Sub

Now comes the turn of Repeater Control. On the ddlCustomerID_SelectedIndexChanged event fill the Repeater Control:

Protected Sub ddlCustomerID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCustomerID.SelectedIndexChanged

 

DBCmd = New SqlCommand("SELECT * FROM Customers WHERE CustomerID = @CustomerID", DBConn)

DBCmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5).Value = ddlCustomerID.SelectedItem.Value

DR = DBCmd.ExecuteReader()

Me.rcCustomerDetail.DataSource = DR

Me.rcCustomerDetail.DataBind()

DR.Close()

DR = Nothing

 

End Sub

Notice for displaying data in DropDownList control I have used DataAdapter and for displaying data in Repeater control I have used DataReader. As such there is no difference between them   in term of declaration but in terms of efficiency i.e. query execution, DataReader is efficient. The main difference between the DataReader and DataAdapter is DataReader is directly connected to the Database whereas, DataAdapter first fill the DataSet and then displays the data on form.

Finally on the Page_Unload event close connection object, dispose command object and set there values to nothing:

 

Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload

 

 DBConn.Close()

 DBConn = Nothing

 DBCmd.Dispose()

 DBCmd = Nothing

 

End Sub

Article Comments
this should help

Posted on 6/28/2006 12:46:59 AM by ferny

its nice article.helped me and cleared my problem

Posted on 9/20/2006 2:48:19 AM by sheetal

its nice but if it was in C# then it would be more understandable to me

Posted on 12/12/2006 4:19:32 AM by ramkiran

All Right,But if U have code in C# then please forward it.

Posted on 2/1/2007 2:18:20 AM by anshuk

This is nice one.. can you tell me if i want to display an image. i have status id in database 1/0. if it is 0 it should show X image and if it is 1 then it should show tick mark image

Posted on 2/23/2007 9:37:42 AM by Shantanu

its nice article.helped me and cleared my problem
But can u forward c# Code....

Posted on 2/16/2008 1:47:59 AM by Anuj

its nice. it helped me lot. can you help me in developing web application. i need some sample source code

Posted on 2/16/2008 2:27:34 AM by anita

Thanks sooooooooooo much, its a great great help

Posted on 5/7/2008 4:53:32 AM by Ann

its help me. But if U have code in C# then please share it.

Posted on 8/9/2008 6:49:43 AM by shweta

thank u very much it helpmed me a lot as m a biginner in asp.net...as maximum friends asked m also asking the code in C#, if u have pls share with us

Posted on 4/29/2010 3:13:28 AM by Impa D

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Display Images in DataList Control using ASP.Net 2.0

Disclaimer - Privacy
© 2002-2014 DevASP.net