Saturday, September 8, 2012

Repeater Control

Playing around with repeater controls is quite fun when you code.....Ofcourse you cant play with them at your workplace because your boss doesn't pay you for that....

Repeater as the name suggests it iterates itself i.e. it repeats itself.....

Let us take an example....You have a table in the database that contains the codes of airlines and you have to show it on your website....

What would be the first control that comes to Your mind? Gridview? Why GridView? Because it arranges the data in a tabular format.....That's right....

The  specialty that a repeater provides is that you can make the data from the database as a control from toolbox....I am sure you are confused about what is this that I am writing....I mean you can make the data a linkbutton or a hyperlink, etc.

Ok too much of talking...now let me show you how can a repeater help you.....



Above you can see I have added a repeater control to the html file.
That is it....now lets go to coding i.e. writing business logic for this repeater so that the data is bound to the repeater control.

I am assuming that you know how to create connection with the server....If not please refer to my earlier post http://codersandprogrammers.blogspot.in/2012/09/database-connection-in-webconfig.html....
I have a table in my database which gives information about the airlines..

In the repeater control I have added a label to which the airline code will be bound

I will list down the airline codes in this repeater control....The airline codes are not authentic....I have just created them for my practice....

Let's see the business logic for this...

Here you can see that I have created a connection and using that connection I have retrieved data from the database using a simple query "select air_code from OAAirline"....

Then using SqlDataReader  I have loaded the datatable which will become the data source for repeater control....

When rptr_practice.DataBind() is executed it refers to the label in the repeater control the text of which is bound from database with the column air_code....

You will see the output as follows....


Here you will see that all the codes are not properly aligned.....

This problem can be solved with a datalist which gives you formatting option as well as repeat Direction i.e. whether you want to repeat the data horizontally or vertically....

We will see the DataList in my next post.....



No comments:

Post a Comment