Create A Threetier Web Form

A business tier enables you to encapsulate business rules or logic into components. These business tier components enable you to automate business processes that your company uses. With Web applications, you can break your code out into three tiers—Data, Business, and Presentation. This allows you to consolidate all data access code into one component, all of the code related to business logic into another component, and all of the code for the user interface in the Web page.

Like the Data Access Layer, the Business Layer will be implemented as a C# component. In this component, you can set the connection string when the class is created. You then need to add a function that will call the Data Access Layer to retrieve the data necessary to apply the business logic. After all the code necessary for mimicking your business process is put in place, you need to compile the business component to the /bin directory as a library.

Using a business component on a Web page is the same as using a data access component. Now that you have put a Business Layer between your Data Access Layer and your Web Page, you can change the Presentation Layer without having to rewrite business logic or data access code.

CREATE A BUSINESS LAYER

CREATE A BUSINESS LAYER

□ Open a new document in your text editor.

< Add an alias to the System, System.Data, System.Data.SqlClient, and the DataLayer namespaces.

Create a private DataObject class from the DataLayer namespace.

Q Add the code to initialize the BusinessObject connection string.

□ Open a new document in your text editor.

Create a new namespace.

< Add an alias to the System, System.Data, System.Data.SqlClient, and the DataLayer namespaces.

-Q Create a public class.

Create a private DataObject class from the DataLayer namespace.

Q Add the code to initialize the BusinessObject connection string.

CREATE CUSTOM COMPONENTS

You can extend the Business Layer by adding the GetTitlesForType function. This Business Layer uses the Extra Data Access Layer.

TYPE THIS:

namespace BusinessLayer_ai { using System; using Systemi.Data; using Systemi.Data.SqlClient; using DataLayer_ex; public class BusinessObject {

private DataObject dataobjectPubs; public BusinessObject() { dataobjectPubs = new

DataObject(Mserver=(local)\\NetSDK;uid=QSUser;pwd=QSPassword;database=pubsM);

public DataView GetBusinessTitles() {

return dataobjectPubs.GetBusinessTitles();

public DataView GetTitlesForType(string stringTitleType) { return dataobjectPubs.GetBusinessTitles(stringTitleType);

RESULT:

A business layer that is between the user interface layer and the data access layer.

ËD C:\WI N N T \S y s tem32\.cmd. exe

-Inl*

J

n

Microsoft Windows 2000 [Version 5.00.2195] IK (C) Copyright 1985 2000 Microsoft Corp. ■

My Documente

C:\>cd C:\InetPub\wi«iroot

^SiäL

C:\InetpubVuMRiiroot>csc /rlbinVDataObjeet. d 11 /t:library /nut: b in\Bus inessO bject.dll BusinessObject.cs

Microsoft (It) Visual C# Compiler Version 7.00.9148 [CLR version v1.0.2615]

My Computer

Copyright (C) Microsoft Corp 2000. 011 rights reserved.

ClVInetpubV^MAuroot^

My Network Places

Recycle Bin

Internet Explorer

Setup MSN Internet Accese

J

-Q Add the function that calls GetBusinessTitles from the Data Layer.

Q Go to the command prompt.

I Compile the component using the csc command.

CONTINUED

Implementing a Business Layer into a tiered application allows for clean separation between business logic and the user interface. This gives you the flexibility of having multiple user interfaces for your application without having to rewrite business and data access code.

The business layer is a critical part of your application and is where you need to truly understand the rules that enforce good business practices. Sometimes these practices need to ensure that you interact with the database without compromising data integrity. This can be done with transactional code. The business layer is traditionally the location for where transactional code is placed to ensure integrity of your data stores.

To write code that uses your business component, you need to import the namespace for the business component. This is done using the @Import directive at the top of the page. If you are pulling data back from your business component, you can place a Server form on the page with a control to display the data. One way to display data is using a DataList control. See page 130 for more information on working with the DataList control. After you have specified what to show in the DataList, you can then create the code to populate the control. You can use the Page_Load function to do this.

CREATE A THREE-TIER WEB FORM

CREATE A THREE-TIER WEB FORM

, Open the

GenericTemplate.aspx template from the Code Templates directory.

le Add a DataList to the page to display the results.

™ Add an ItemTemplate to the page to describe the output for each item in the result set.

, Open the

GenericTemplate.aspx template from the Code Templates directory.

-— Import the BusinessLayer namespace.

le Add a DataList to the page to display the results.

™ Add an ItemTemplate to the page to describe the output for each item in the result set.

CREATE CUSTOM COMPONENTS

TYPE THIS:

public void Page_Load(Object sender, EventArgs E) { if (!IsPostBack) {

BusinessObject businessobjectPubs = new

BusinessObject("server=(local)\\NetSDK;uid=QSUser; " + "pwd=QSPassword;database=pubs"); datalistBusinessTitles.DataSource = businessobjectPubs.GetBusinessTitles(); datalistBusinessTitles.DataBind(); labelTitleType.Text = "business"; } }

public void Submit_Click(Object sender, EventArgs E) { BusinessObject businessobjectPubs = new

BusinessObject("server=(local)\\NetSDK;uid=QSUser; " + "pwd=QSPassword;database=pubs"); datalistTitles.DataSource =

businessobjectPubs.GetTitlesForType (dropdownlistTitleTypes.SelectedItem.Value.ToString()); datalistTitles.DataBind();

labelTitleType.Text = dropdownlistTitleTypes.SelectedItem.Text.ToString().ToLower;

RESULT:

A data bound list that displays a list of books that are dependent on which title type is chosen by the user.

http://localhost/ThreeTieiWebForm.asp* - Microsoft Internet Explorer

File Edit View Favorites Tools Help

Address http://localhost>ThreeTierWebForm.aspn

Done

Add the Page_Load function to the page.

Create a new instance of the DataObject class and set the connection string.

http://localhost/ThreeTieiWebForm.asp* - Microsoft Internet Explorer

File Edit View Favorites Tools Help

JSJ^

S3 LÎI rrP«i°™IBar QSearch jjJ Favorites !> j ® J,

Address http://localhost>ThreeTierWebForm.aspn

Welcome to mylifetimegoals.com

Here are some of the resources to help you reach your business goals... The Busy Executive's Database Guide

An overview of available database systems with emphasis on common business applications. Illustrated.

19.99

Cooking with Computers: Surreptitious Balance Sheets

Helpful hints on how tu use your electronic resources tu the best advantage.

11.95

You Can Combat Computer Stress!

The latest medical and psychological techniques for living with the electronic office. Easy-to-uriderstand explanations.

2.99

Done

EE Set the DataSource of the DataList on the Web page to the function in the Data Access Layer that returns the business titles.

Bind the DataList.

E Save the file and request it from the Web server.

■ The title, notes, and price appear for the business titles from the pubs database using the business and data layers.

USE A CODE-BEHIND FOR OUR ASP.NET PAGE

You can store your code in a separate file, called a Code-behind, which allows for you to clearly separate the code from presentation. This enables people with Web design skills to work on pages separately from the Web programmers. This is a big advantage in the ASP.NET framework and was not available in ASP 3.0. In ASP 3.0, your server-side code had to be inline with your HTML (on the same page).

To utilize a Code-behind in your ASP.NET page, you add two attributes to the page directive. One of the attributes is the Inherits attribute, which you can use to specify the class which you want to use in your Code-behind. The next attribute, the Src attribute, specifies the location of the file that contains the Code-behind code. In the Code-behind file, you can implement event handlers as if they were on the page that uses the Code-behind. For example, a common event handler is the Page_Load event.

All the code in the Code-behind is server-side code that may require round trips to the Web server. To avoid this, you need to use client-side code in the Web form.

USE A CODE-BEHIND FOR YOUR ASP.NET PAGE

USE A CODE-BEHIND FOR YOUR ASP.NET PAGE

Create an alias to the BusinessLayer, System, System.Web.UI, and System.Web.UI.Web Controls.

□ Create a public class of type Page.

—Q Create a public variable of type DataList.

0 Open the

ThreeTierWebForm.aspx template from the Code Templates directory.

□ Cut the Page_Load event handler from the file.

Q Save the file as the class name to the Default Web site.

Create an alias to the BusinessLayer, System, System.Web.UI, and System.Web.UI.Web Controls.

□ Create a public class of type Page.

—Q Create a public variable of type DataList.

0 Open the

ThreeTierWebForm.aspx template from the Code Templates directory.

—0 Add a Page directive at the top of the page and an Inherits attribute with a value of the class name and a Src attribute with the value of the filename.

□ Cut the Page_Load event handler from the file.

Q Save the file as the class name to the Default Web site.

CREATE CUSTOM COMPONENTS

You can use an event handler fired from a control as well. This code example calls the Button_OnClick event handler on the Code-behind page. The first section of code is to associate the Code-behind page with the aspx page, this goes at the top of the aspx page. The second section of code goes into the Code-behind page.

TYPE THIS AT THE TOP OF THE WEB FORM:

<%@ Page Inherits="CodeBehind_ai" Src="CodeBehind_ai.cs" %>

TYPE THIS IN THE CODE-BEHIND PAGE:

using BusinessLayer;

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

public class CodeBehind_ai : Page {

public Label labelMessage;

public void Button_OnClick(object Source, EventArgs labelMessage.Text="You are going to Step 2...";

RESULT:

The Web page calls the Code-behind when the button is clicked.

_• Paste the Page_Load event handler into the CodeBehind page.

, Save the file and request it from the Web server.

■ The title, notes, and price appear for the business titles from the pubs database using the business and data layers in a Code-behind.

_• Paste the Page_Load event handler into the CodeBehind page.

, Save the file and request it from the Web server.

■ The title, notes, and price appear for the business titles from the pubs database using the business and data layers in a Code-behind.

AD FORM DATA WITH REQUEST.FORM

The HTTPRequest object enables you to read the HTTP values sent by a user during a Web request. This way of working with user input is how you typically access user input with ASP 3.0, giving you a backward compatibility. There is no true backward compatibility using C# as the language; but if you are using VB, it is compatible.

You can use Request.Form and Request. QueryString methods to read data that is submitted from another Web page. In order to specify which control you want to access, you need to know the control's name or ID. For example, you can use the drop-down list box in the task that has an ID of dropdownlistSuggestions. To read the value of this control, you can use Request. Form["dropdownlistSuggestions"].

The configuration of your HTML form determines if you use the Form or QueryString to retrieve form data. The HTML form has a method attribute. If this attribute is set to post, then you need to use Request.Form to obtain user input from the HTML form. If set to method="Get", then you need to use Request.QueryString. Both of the properties on the Request object (Form and QueryString) contain a NameValueCollection collection class. The NameValueCollection class represents a sorted collection of associated String keys and String values that can be accessed either with the key or index.

READ FORM DATA WITH REQUEST.FORM

READ FORM DATA WITH REQUEST.FORM

Q Open the

GenericTemplate.aspx template from the Code Templates directory.

< Add a message to the user.

Place a drop-down list box on the page with suggestions for choosing the suggested number of goals.

□ Add a button control to the page.

-Q Add a label control to the page.

Q Open the

GenericTemplate.aspx template from the Code Templates directory.

Add a heading for the page.

< Add a message to the user.

Place a drop-down list box on the page with suggestions for choosing the suggested number of goals.

□ Add a button control to the page.

-Q Add a label control to the page.

Was this article helpful?

0 0

Post a comment