Two Page Data Retrieval in ASP.NET

Here’s the scenario, using ASP.Net 2.0 (and above):
We have two web pages called GV1.aspx and NewPage.aspx. In GV1.aspx, we have a GridView, listing only a few fields from a database. From that page, we will designate one field as a HyperLinkField, so that, by clicking on it, we will be transferred to the second page (Newpage.aspx), using a QueryString. In that second page, we will retrieve all fields in the table based on the ID used in the QueryString.

This actually very easy to accomplish using ASP.Net 2.0, and most of the technology has already been included here, atASPNet101.comCode Sample – Master Detail Solution
Really, the only 2 new things are the use of the HyperLinkField and the two-page scenario.

For those of you who are familiar with ASP.Net 1.x, the HyperLinkField looks pretty much like the HyperLinkColumn, used with a DataGrid. The format is:

<asp:HyperLinkField DataTextField="au_lname"
	DataNavigateUrlFields="au_ID"
	DataNavigateUrlFormatString="NewPage.aspx?id={0}"
	HeaderText="Click For Details" />

Like the BoundField (or BoundColumn, with the DataGrid), it is not necessary to use a TemplateColumn.

In our scenario, the first page will consist of a GridView and SQLDataSource control only, using the Pubs database for the example. The last name field will be used for the Hyperlink:

<asp:GridView Runat="server" Id="gvAuthors" GridLines="Both" DataSourceID="SQLDS2" DataKeyNames="au_id" SelectedItemStyle-Backcolor="Pink" AllowPaging="True" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="au_fname" HeaderText="au_fname" /> <asp:HyperLinkField DataTextField="au_lname" DataNavigateUrlFields="au_ID" DataNavigateUrlFormatString="NewPage.aspx?id={0}" HeaderText="Click For Details" /> <asp:BoundField DataField="state" HeaderText="state" /> </Columns> </asp:GridView>

The SQLDataSource control looks like this:

<asp:SqlDataSource ID="SQLDS2" runat="server"
	SelectCommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [Authors] Order by [state]"
ConnectionString="<%$ ConnectionStrings:Pubs %>">
</asp:SqlDataSource>

The ConnectionString here, is using the ConnectionString section of the Web.Config file, with an entry titled ‘Pubs’.

What happens on this page, is that, all the records are returned from the Authors table and, with the use of the HyperLinkField, the end-user is then, taken to the second page, based on the item selected in the GridView.

Now, on the second page (NewPage.aspx), we again have only two controls – a DetailsView and a SQLDataSource control. The DetailsView is fairly straightforward, using all BoundFields for display. Naturally, if there were other needs or possibilities here, we could use TemplateColumns, or add an Edit Button, etc.

<asp:DetailsView DataKeyNames="au_id"
	DataSourceID="SQLDS3"
	GridLines="Both"
	cellpadding="0"
	cellspacing="0"
	Headerstyle-Font-Size="12"
	Font-Name="Arial"
	Font-Size="10"
	HeaderText="Author Details" AutoGenerateRows="False"
	ID="dvPubs" runat="server">
	<Fields>
		<asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True"  />
		<asp:BoundField DataField="au_lname" HeaderText="au_lname"  />
		<asp:BoundField DataField="au_fname" HeaderText="au_fname"  />
		<asp:BoundField DataField="phone" HeaderText="phone"  />
		<asp:BoundField DataField="address" HeaderText="address"  />
		<asp:BoundField DataField="city" HeaderText="city"  />
		<asp:BoundField DataField="state" HeaderText="state"  />
		<asp:BoundField DataField="zip" HeaderText="zip"  />
		<asp:CheckBoxField DataField="contract" HeaderText="contract"  />
	</Fields>
</asp:DetailsView>

The way in which this is populated depends on two things. The main thing is the DataSource control. Here, we assign the ID of the DataSource control in the DataSourceID property, in the same way most or all new 2.0 compliant controls are handled. In that DataSource Control, though, we add a QueryStringParameter for the SelectParameter. Therefore, the data displayed will depend on whatever is assigned in the QueryString, when the page is accessed.

<asp:SqlDataSource
	ConnectionString="<%$ ConnectionStrings:Pubs %>" ID="SQLDS3"
	runat="server"
	SelectCommand="SELECT * FROM [PBAuthors] WHERE ([au_id] = @au_id)">
	<SelectParameters>
		<asp:QueryStringParameter Name="au_id" QueryStringField="id" />
	</SelectParameters>
</asp:SqlDataSource>

As you can see here, there’s a ‘Where’ clause in the Select statement. It’s requesting (from the same Database and table) all fields concerning an author, based on the ‘au_id’ field. That au_id field is in the QueryString. When you copy and paste these two pages and create them on your own website, on the second page. The URL will be something like:

http://localhost/NewPage.aspx?id=118-45-7412

In the QueryStringParameter, the QueryStringField property relates to the the ‘id’ in the URL and the Name property relates to the actual field in the table, from where we retrieve the data.

At this point, you can take a big breath, and take this all in. But, that’s all you need to do, because, in its simplicity, that’s all there is to accomplish this task. Now that wasn’t bad at all, was it?

Related Posts:

  • No Related Posts
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!