Work With Ma Relationships

MASTER-DETAIL

Web developers commonly work with the Master-Detail relationship when creating ASP.NET Web pages. The Master-Detail relationship usually appears as a column containing a list of items. When a user clicks an item in that column, another page with details about that particular item appears. A classic example of this is a customer order system, where you have a list of customer orders and for each order you have one or many items that make up an order.

To create a Master-Detail relationship, you work with two pages. The first page contains a DataGrid bound to data from a SQL database. You must define one of the columns in the DataGrid as the column where the user can click by using the <ASP:HYPERLINKCOLUMN> tag. You then need to specify the field with the datanavigateurlfield attribute, the page to be linked to and how to format the URL with the datanavigateurlformatstring attribute, and the text for the column with the text attribute.

After you finish the master page, you have to create the detail page. On the detail page, you use the data that was passed via the URL to execute a SQL query against the database. You can then display the results with another DataGrid or any other bound control that best suits the detail data display.

WORK WITH MASTER-DETAIL RELATIONSHIPS

4 Untitled - Notepad

File Edit Format Help

<%@ Import Namespace-'System.Data" %> «%@ Import Namespace-'System.Data SqICIient" «HTML-«HEAD>

«SCRIPT LANGUAGE-'C#" RUNAT="Server"3 protected void Page_Load(Object sender, EventArgs e) {

SqIConnection sqlconnectionPubs = new SqlConnection("serv,er-(local)\\NetSDK;uid-QSUser;pwd-QSPassword; database-pub

SqlUataAdapter sqldataadapterl itles = newSqlDataAdapter("selecttitlejd title, notes, price from titles where type-business'"!, sqlconnectionPubs),

DataSet datasetTltles - new DataSetQ; sqldataadapterTitles Fill(datasetTitles, "titles"), datagridTitles DataSource-datasetTltles.Tables["titles"].DefaultVlew; datagridTitles.DataBindQ

«FONT FACE ^"Verdana":-«H3>Welcometo myllfetimegoals.com«/H3>

D Open

DataGridTemplate.aspx from the Code Templates directory.

0 Add the SQL statement to retrieve a couple of columns from the titles table.

4 Untitled - Notepad

File Edit Format Help

JsJjt]

datagridTitles.DataBindQ,

«FONT FACE ="Verdana"s «H3>Welcome to mylifetimegoals.com«/H3:-

Here are the are some books that will help you reach your career goals. <P/>_

<ASP:DATAGRID ID="dataqridTitles" RUNAT="Server" SHQWHEADER="False": COLUMNS:-]-

«ASP.HYPERLINKCOLUMN

DATANAVIGATEURLFIELD-'titlejd" DATANAVIGATEURLFORMATSTRING-"Detail.aspx?ld-{0}' TEXT-'Details" l>_

c/ASP:DATAGRID>

-0 Add the DataGrid to the page and set properties.

Configure a special column with the <COLUMNS> tag.

0 Specify the title_id column as an

<ASP:HYPERLINKCOLUMN>

and set the other properties of the column.

0 Save the file as the master file.

ACCESS DATA WITH ASP.NET

Extra

You can use the Hyperlink column to link an item on a master list to a detail table for the master item selected. To have a column represented by a button, you could also use the

ButtonColumn or the EditCommandColumn. Here are the definintions for the ButtonColumn and EditCommandColumn.

Example:

<asp:ButtonColumn

ButtonType="LinkButton|PushButton"

Command="BubbleText"

DataTextField="DataSourceField"

DataTextFormatString="FormatString"

FooterText="FooterText"

HeaderImageUrl="url"

HeaderText="HeaderText"

ReadOnly="True|False"

SortField="DataSourceFieldToSortBy"

Text="ButtonCaption"

Visible="True|False"

<asp:EditCommandColumn

ButtonType="LinkButton|PushButton"

CancelText="CancelButtonCaption"

EditText="EditButtonCaption"

FooterText="FooterText"

HeaderImageUrl="url"

HeaderText="HeaderText"

ReadOnly="True|False"

SortField="DataSourceFieldToSortBy"

UpdateText="UpdateButtonCaption"

Visible="True|False"

íj Untitled - Notepad

File Edit Format Help

Import Namespace-'System.Data" Import Namespace-'System.Data SqICIIent" %> -=HTML> <HEAD>

•¡SCRIPT LANGUAGE-'C#" RUNAT="Server"> protected void Page_Load(Object sender, EventArgs e){

SqIConnectlon sqlconnectionPubs = new SqlCunnectlon("seiYer-(local)\\NetSDK;uld-QSUser;pwd-QSPas5word;database-pub string stringSelect = Request.QueiyStrlng["ld"] +

"select* from titles where titlejd =

SqIDataAdapter sqidataadapterTltles = new SqlDataAdapter(stringSelect, sqlconnectionPubs),

DataSet datasetTltles - new DataSet(); sqidataadapterTltles FIII(datasetTltles, "titles");

datagrldTltles DataSource-datasetTltles.Tables["tltles"].DefaultVlew; datagrldTltles.DataBlnd(),

Q Open

DataGridTemplate.aspx from the Code Templates directory.

0 Add the SQL statement to retrieve the data based on what was passed through the URL.

Click the Details button.

, Request the master file from the Web server.

Click the Details button.

■ The full details about the title are displayed.

I Save the file as the detail file.

Was this article helpful?

0 0

Post a comment