So, we've seen how to show a table of data from a database. Now, let's see how to create a link in one of the columns to open one of the rows of data in its own page.
First, let's change the GridView we used on Default.aspx in to a Repeater element:
A Repeater is just an easy way of looping the set of items we data-bind to it and have it apply the ItemTemplate we specify, to each one. Should all make sense?
In the browser this will render like this:
Looks a bit better than before, no?
The link column points to URLs like "/Animal.aspx?id=2". To handle this we need to add a new ASPX Page, called Animal.aspx, like so:
Notice we're basing it on a template. On the next screen choose Site.Master and click ok.
Loading a Single Row of Data
Until now we've been loading the whole of the Animals table. However, in the Animal.aspx page, we only want information for one row.
To fetch a single row of data based on its ID we need to add a new method to our Table Adapter.
First, open the DataSet we created and then right-click the AnimalsTableAdapter part and choose "Add Query...".
In the next two screens that appear choose "Use SQL Statements" and then "Select which returns rows".
In the third screen enter this SQL:
On the next screen give the method a meaningful name, such as GetAnimalByID:
Notice how it adds the new method to the adapter and shows that's there a single parameter required (below). In this case it's the ID. Because VS is clever it knows the ID needs to be an INT, so when you call the method it insists that's what you pass! Clever, no?
Now, let's use this new method of the adapter to extend our AnimalFactory class. Here's the new method I added:
It should make sense what it's doing? Instead of returning a List of Animal objects it only returns one.
Now we can use this from our own pages. Here's the code from the Animal.aspx.cs file and shows what happens in the Page_Load event of the Animal.aspx Page.
As you can see it declares a public Animal object and then assigns data to it using the AnimalFactory class's new method we just added, passing to the method the ID part of the URL.
The Animal object is public so that we can refer to it from the actual Animal.aspx file, like so:
Notice the lowercase animal being used. This is because we're referring to the object we created rather than the class itself. Or something like that.
Here's what you should then see if you click a link on the homepage:
A lot of work to get to this point. I know. It's easier once you get the hang of it though. Most of the groundwork gets done up front.
Although it's more work to get to this point than with err, I dunno, Domino, the pay-off comes when you realise just how much control you have over absolutely everything.
You could have done this in Domino in a matter of minutes. The strengths of using SQL and ASP.NET don't become obvious until things get more complicated. Horses for courses and all that, but I'd rather spend more time up-front doing this sort of stuff knowing the end result would offer me complete flexibility.