Saturday, September 22, 2012

A Little More Fun with Datalist Controls

We have seen in the earlier post (atleast I have written it, don't know whether you have seen it or not), how the datalist iterates data from the database. In the earlier post I had taken the example of an airline code.

Now lets see something else. What you can do with a control within a datalist.

In this post I will make the airline code a link which will direct me to another page where the details of that airline will be available.

So let's get started....

First of all lets see the page which has the air_code as Link.

figure 1


Here you can see that inside the datalist "dal_practice" there is a linkbutton "lbl_aircode" which has an OnClick event "lnk_aircode_Click". This means that all the linkbuttons will point to the same event or method.

Now lets see the event and what does it do.

figure 2

Here you can see that there is a method "lnk_aircode_Click". Now when a link is clicked, this method will execute. I have created a new LinkButton. It will hold the reference of the link clicked. "sender", as you can see tells the compiler which link has been clicked.

Then we are using Response.Redirect() method. This will simply direct the user to the page "FlightDetails.aspx" with the reference of the link clicked. Here we have passed something like "?code=". This is simply a parameter that has been passed to the querystring.

You will understand this with the example below.

figure 3

In the image above I am clicking on the link "AF C105" which is an airline code. Now when a user clicks on it, he will be redirected to FlightDetails.aspx page which will have the code as AF C105.

Now when the user is redirected to  FlightDetails.aspx page the details of that flight will be shown as below.

figure 4


Above you can see the URL as FlightDetails.aspx?code=AF%20C105. 

Now lets see the design and the code and wrap up this post.

figure 5

I have declared five labels which will hold data from five different fields for a particular airline.

Let's see the code for it.

figure 6


I have written a query in which I am passing the airline code as parameter.

The data returned will be displayed in five different labels as shown above in figure 4.

I hope this article was quite simple and easy to understand.

Will be back with new post and some other control soon....Till then Happy Coding.....

No comments:

Post a Comment