PeterBox Homepage
Hallo, willkommen auf Singapur, 9.9.10 15:05:57

.NET Development Handbook

.NET Development Handbook .NET Development Handbook .NET Development Handbook .NET Development Handbook
.NET Development Handbook

6 ASP.NET

6.1Data Storage
6.1.1Application Data
6.1.2Global.asax
6.1.3ViewState
6.1.4Query in HTML Link
6.1.5Session Data
6.1.6Transactional Data
6.2Web Forms
6.2.1.ASPX files
6.2.1.1Meta Tags
6.2.1.2Title Tag
6.2.2Structure of code listing of Web Forms
6.2.3Sequence of Events
6.3User Interface
6.3.1Page
6.3.2Menu
6.3.3Button and Links
6.3.4Radio Control
6.3.5Images <img>
6.3.6Web User Controls
6.3.7Web Custom Controls
6.3.8Third Party Controls
6.3.9Tab Order
6.4Controls
6.4.1Naming Convention Controls
6.4.2HTML Code
6.4.3HTML Controls and Web Controls
6.4.4User Controls
6.4.5Custom Controls
6.5Database Access from ASP.NET
6.5.1Use of DataReader instead DataSet
6.5.2One Database Access per Webpage
6.6Exception Handling ASP.NET
6.7Multithreading issues ASP.NET
6.7.1Global.ASAX
6.8Testing ASP.NET
6.8.1Individual Tests ASP.NET
6.8.2Peer Review ASP.NET
6.8.3Integration Test ASP.NET
6.8.4Stress Test ASP.NET
6.9ASP.NET Tracing

6.1 Data Storage

Storing data is expensive, if a database is involved. Be careful to match lifetime of data with the appropriate data storage method:

 

lifetime of data

data storage method

Storage

Permanent (configuration data, etc.)

Application Data

Web server

Processing of one HTML request

Global.asax

Web server

Postback (same HTML page request processed again)

ViewState

Web page

Passed between 2 different pages

Query in HTML link

Web page

General User related data (preferred language, etc.)

Session Data

Database

User created orders, etc.

Transactional Data

Database

6.1.1 Application Data

Application data like country lists, etc.,?can be stored in the database, but should be read only once when the web server starts. Even better is to store this data in source code, possibly with the help of a code generator. Other application data might change from time to time, like exchange rates. In this case reading of this data needs to be protected by a lock.

6.1.2 Global.asax

Several pieces of ASP.NET (Global.asax, Web Form, Web User Control) might need to share some data (for example: Header control and Footer control need to know which language to use, but is detected in Global.asax). Use for that purpose public members of Global.asax. The lifetime of this data is only the creation of one HTML response. Don抰 use Context.Items, which is slower (string search in a collection vs. no search at all).

6.1.3 ViewState

???

6.1.4 Query in HTML Link

???

6.1.5 Session Data

Store session related data (Language, etc.) in the database. This has the following advantages:

1)      several web servers can work in parallel

2)      access to session data with database tools (important for CRM statistics)

The speed disadvantage of accessing the database server can be minimized by bundling the query for session data with another database query which would take place anyway.

Since session data (language, etc.) is needed on every page, Application_PreRequestHandlerExecute in Global.asax.cs accesses the session data and stores it in public members. They are called CookieLanguage, CookieName, etc., since the session data is stored in the database according to cookie data. From other page, the session data is accessed as follows:

 

protected Global GlobalData;

 

private void Default_Init(object sender, System.EventArgs e)

{

牋牋?GlobalData = (Global)Context.ApplicationInstance;

牋牋?if (GlobalData.CookieMissing) {

牋牋??/code>

 

6.1.6 Transactional Data

???

6.2 Web Forms

Don抰 put server code in .ASPX files, put it in code behind files. Code which is not user interface related or code which is used by more than one web page should be written in a separate class file.

Example ???

6.2.1 .ASPX files

The structure of the .ASPX files follows the requirements of HTML and ASP.NET:

 

<%@ Page language="c#" Codebehind="Default.aspx.cs" AutoEventWireup="false" Inherits="PeterBox.WebSite._Default" %>

<%@ Register TagPrefix="PBNameSpace" TagName="Header" Src="Header.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<html>

牋?<head>

牋牋牋?<meta http-equiv="content-type" content="text/html; charset=UTF-8">

牋牋牋?and other meta tags

 

牋牋牋?<title>PeterBox - Homepage</title>

牋牋牋?<link href="PeterBox.css" type="text/css" rel="stylesheet">

牋?</head>

牋?<body MS_POSITIONING="FlowLayout">

牋牋牋?<form id="Test" method="post" runat="server">

牋牋牋牋牋?<PBNameSpace:Header id="HPHeader" title="HomePageTitle" runat="server">

牋牋牋牋牋?</PBNameSpace:Header>

牋牋牋牋牋?<div style="margin-right: 10px; margin-left: 10px">

牋牋牋牋牋牋牋?Here comes actual page content, between page header and footer

牋牋牋牋牋? </div>

牋牋牋牋牋? <br>

牋牋牋牋牋?<PBNameSpace:Footer id="HPFooter" runat="server">

牋牋牋牋牋?</PBNameSpace:Footer>

牋牋牋?</form>

牋?</body>

</html>

 

6.2.1.1 Meta Tags

 

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<meta name="title" content="DotNet homepage Peter Huber">

<meta name="author" content="contact@peterbox.com">

<meta name="description" content="The PeterBox shows how the .NET technology can be used to build a website, including the use of CSS, C#, ADO.NET and SQL server. It is also the homepage of J黵gpeter Huber, Singapore.">

<meta name="keywords" content="asp.net css .net dotnet csharp peter huber j黵gpeter ">

<meta name="copyright" content="?2003 Peter Huber, Singapore">

<meta name="robots" content="all">

 

6.2.1.2 Title Tag

The HTML element <title> should start with 揚eterBox ??

 

<title>PeterBox ?Home Page</title>

 

6.2.2 Structure of code listing of Web Forms

• General structure:

  1. File Header
  2. Using statements
  3. Namespace
  4. Class Declaration
  5. Control Declarations
  6. Member Declarations
  7. OnInit procedure
  8. InitializeComponent() (system created)
  9. Other event procedures as needed, in chronological order

• Rename Page_Load to FormName_Load.

• Visual Studio creates events in a random order. Reorganize events in a chronological order: Move them after InitializeComponent().

• Visual Studio tends to lose events settings. Do it manually in InitializeComponent().

 

6.2.3 Sequence of Events

???Validation occurs only before render event

6.3 User Interface

6.3.1 Page

Test behavior of every page if user keys in Page URL directly into Internet Explorer (instead using provided links and menus).

6.3.2 Menu

???

6.3.3 Button and Links

Any element a user can click should change if the cursor moves over it. This can be achieved with the pseudo-class hover for <a> elements.

 

a.GrayButton {background-color: #eeeeee;}

 

a.GrayButton:hover {background-color: #d0d0d0;}

 

It抯 a bit more difficult to achieve this effect for buttons, Javascript has to be used.

Each link / button should have a hint (Title attribute, ToolTip), explaining what will happen if the user clicks here.

6.3.4 Radio Control

Make sure that also the label of a radio button is clickable.

 

<LABEL for="LangGermanRadio">Deutsch</LABEL>

<INPUT id="LangGermanRadio" type="radio" value="German" name="radLanguage">

 

6.3.5 Images <img>

Images should have a hint, giving additional information for the picture displayed.

6.3.6 Web User Controls

???

6.3.7 Web Custom Controls

???

6.3.8 Third Party Controls

Infragistics ???

6.3.9 Tab Order

???

6.4 Controls

6.4.1 Naming Convention Controls

Function ?Control Type

Example: ???

6.4.2 HTML Code

For speed reason, use whenever possible pure HTML code. A server control has quite some overhead because of all the functionality provided (event, ViewState, ?. Even if few attributes of an HTML element have to be changed during run time, instead using HTML or Web Control, use <%=xxx %>:

 

<th>

牋牋?<%=AnyObject.GetAString("SomeParameter") %>

</th>

 

In an HTML option, the above code wouldn抰 work due to the ? ?in GetAString(). In that case, copy everything into a protected class variable during the Page_Load

 

File XXX.ASCX.CS:

Protected string AnyObjectString = AnyObject.GetAString("SomeParameter");

 

File XXX.ASCX:

<A class=HeaderLink title="<%= AnyObjectString %>"

 

6.4.3 HTML Controls and Web Controls

Web Controls are preferred over HTML Control,

6.4.4 User Controls

???

Use PBoxNameSpace as TagPrefix:

 

<%@ Register TagPrefix="PBoxNameSpace" TagName="Header" Src="Header.ascx" %>

 

6.4.5 Custom Controls

???

Use PBoxNameSpace as TagPrefix:

 

<%@ Register TagPrefix="PBoxNameSpace" Namespace="PeterBox.WebSite" Assembly="PeterBox" %>

 

6.5 Database Access from ASP.NET

6.5.1 Use of DataReader instead DataSet

Datasets use DataReaders to access the database. They provide additional functionality like keeping state information (i.e. edited data) for certain time, which makes them about 3 times slower than DataReaders. This information is already stored in forms on HTML pages and DataSets are usually not needed for WebPages.

6.5.2 One Database Access per Webpage

A database access is quite time consuming for the web server, because processes on two different machines need to be synchronized together with the LAN activities for exchanging the data. On the other hand, there it doesn抰 take much longer to perform 2 queries on the database instead of 1. Therefore, the number of roundtrips to the database should be minimized. Ideally, all information exchanged with the database (logging information, user information, page specific information) should be processed by one stored procedure. However, if user input is involved (post back), a second database access can often not be avoided.

6.6 Exception Handling ASP.NET

???

See chapter handling ASP.NET in ms-help://MS.VSCC/MS.MSDNQTR.2002JUL.1033/dnbda/html/exceptdotnet.htm

Storing of error tracing information in a local file if database connection is not available

6.7 Multithreading issues ASP.NET

???

6.7.1 Global.ASAX

???

6.8 Testing ASP.NET

Define a special console project which will run all the tests defined in the middle tier.

6.8.1 Individual Tests ASP.NET

• Test with different browser and browser versions

• Test with cookie disabled

• Test with different regional settings and languages, is everything properly translated ?

• Test for down-level browser (no client validation)

• Test shortcut keys in all languages

• Test tab key, enter key

• Test how it looks with different sized texts

• Test how it looks when windows size is changed

• Check HTML created on client (Also during peer review)

6.8.2 Peer Review ASP.NET

• Placement of CSS information

- Is inline style used where an internal style sheet should be used (a style formatting is used more than once in one HTML page) ?

- Is an internal style sheet used where an external style sheet should be used (a style formatting is used on several HTML pages) ?

???

6.8.3 Integration Test ASP.NET

???

6.8.4 Stress Test ASP.NET

???

6.9 ASP.NET Tracing

See also .Net Framework, Building Applications, Creating ASP.NET Applications, ASP.NET Trace

 

Application settings ???

 

View traces with http://localhost/PeterBox/Trace.axd?id=0 ???

Chapters Overview

1Introduction
2Overview Development Process
3General Guidelines
4.NET Framework
5Windows Client
6ASP.NET
7HTML
8Cascading Style Sheets
9ADO.NET
10SQL Server
11Appendix
  Version 0.8b contact@peterbox.com ©2002 Peter Huber