How to create your first page with asp.net

Source: Internet
Author: User
Tags html page iis connect numeric value ole microsoft access database oracle database visual studio

If you are an experienced Asp/ado developer but have not tried. NET, it's best to try it now. To make you realize. NET, we will connect the Microsoft Access database (you can also SQL Server or Oracle database) to the Internet and retrieve and display some data. This example requires that information Internet Services (IIS) also require the. NET Framework. You can download it here. NET Framework. If you want to try out a free aps.net development environment, you can download it from the Web matrix.

An Introduction to server controls

The Dynamic server page (Active Server page,asp) is Microsoft's first web technology for connecting to databases and the Web. Asp. NET is a complete rewrite of its traditional language, the ASP. You can use both at the same time because the extensions of the files are different (. NET page uses an. aspx extension, while ASP uses an. asp extension.

Most of the code you write in asp.net will run on the Web server, but it is only HTML that returns to the customer. Fortunately. NET provides a number of new controls similar to standard HTML controls, such as Drop-down lists and text boxes. Table A lists the most common servers (server) controls.

Table A

Common server Controls
Control features
<asp:button id= "Button1" text= "Press" runat= "server"/> Create a standard button on the page
<asp:calendar runat= "server"/> Creating calendars (Calendar)
<asp:droplist id= "List1" runat= "Server" > Create a drop-down list
<asp:listbox id= "ListBox" rows= "4" runat= "Server" > Create a basic list box
<asp:textbox id= "Txtbox" runat= "Server"/> Create a standard text box

. NET server control has the advantage that it is created on a Web server (rather than creating a control on a page like HTML). Therefore, they can be processed before they are sent to the customer. For example, you can do content validation on the page as well as on the server side. This means that you can verify the content in the page and then validate it again on the server side.
In most cases, you can create one by simply adding a component to it. NET control:
runat= "Server"

The corresponding HTML element code is as follows:
<asp:control_name id= "ControlID" runat= "Server"/>

Some development tools are even simpler to use. For example, Visual Studio. NET lets you create server controls by dragging controls on a Web page.

In addition to these server-side standard HTML controls, ASP. NET also provides a set of validation controls (validation controls):

The RequiredFieldValidation control requests a numeric value.
The CompareValidator control compares the values in two controls, such as verifying that two input is the same when a user is asked to enter an e-mail address twice.
The RangeValidator control is used to determine whether the entry value is within a range.
The RegularExpressionValidator control uses a regular expression to validate control input.
The CustomValidator control is used to let you enter the verification code.
Validation summary is used to display a list of all the correct validations used by a page.
Using Ado.net

You may be familiar with ADO, but Ado.net is a completely new language. But it doesn't matter, there's plenty of similarity between the two, and learning how to use new objects is not difficult. There are three steps to connecting to a database:

1. Import one. NET namespace to establish a connection.
2. Create a ado.net DataReader object to get the data.
3. Create a Ado.net Repeater object to display the data.

For. NET, namespaces are a new concept, so there is no corresponding object in ADO. In ADO, a connection is established by providing a string and a connection or Command object. The DataReader corresponds to the ADO recordset; Reperter is a server control that is used to display data on a template basis.

Create a ado.net connection

In order to retrieve data from the database, you need to ado.net. If you are familiar with the level of IIS and Web files, you may want to create an example without any help. You can also follow our demo process by copying the Northwind (an Access database example) to your local hard drive. Our example is in a Web folder called Nettest. Copy paste (or enter) the following code into a text editor and save it as a nettest.aspx.

Now, import one. NET namespace so that you can use the OLE DB database:
<%@ Import namespace= "System.Data.OleDb"%>


The Pageload event executes the code that connects to the Northwind database, and Server.MapPath returns the physical path that contains the database, as shown in code listing a.

Connection string

If you want to connect to SQL Server (with an OLE DB connection), you can use the following statement:
"Provider=sqloledb;data source=martin;initial catalog=northwind;integrated Security=sspi;"

If you are using Oracle, you can use the following statement:
"Provider=msdaora;data source=oracledatabase; User Id=yourusername; Password=yourpassword; "

Connectionstrings.com provides a number of information about the connection, which contains the connection string for each possible case.

Creating Ado.net Objects

The next step is to create a DataReader object that holds the data you want to display. The following code uses the ExecuteReader method of the Command object, which creates a DataReader object that holds all the records in the Northwind client database.
Cnn. Open ()
Sql= "Select CompanyName, ContactName, address, city from Customers"
Cnn=new OleDbCommand (SQL,CNN)
Dbread=cnn. ExecuteReader ()

Using the Reaperter control to display data

Now, by binding the DataReader object you created in the previous step, you can use a Reperter control to display the data. The Reaperter control allows you to create a simple template (for example, an HTML table) that repeats the action for each row of data returned by the query.

Use the Herdertemplate block to create the initial table structure, and the data will appear in the ItemTemplate (table rows and columns) block. For example, the following code creates a header for our customer data that indicates the fields returned by the previous SQL statement:
<HeaderTemplate>
<table border= "1" width= "100%" >
<tr>
<th>CompanyName</th>
<th>ContactName</th>
<th>Address</th>
<th>City</th>
</tr>
</HeaderTemplate>

Unlike other ASP.net objects, repeater objects do not have ready-made layouts (layout) and styles; you have to define your own layout and style. Each row that returns data is displayed in a ItemTemplate block. Each field in the following script contains a cell (in an HTML table):
<ItemTemplate>
<tr>
<td><% #Container. DataItem ("Companame")%></td>
<td><% #Container. DataItem ("ContactName")%></td>
<td><% #Container. DataItem ("Address")%></td>
<td><% #Container. DataItem ("City")%></td>
</tr>
</ItemTemplate>

The template code is in the body tag of the HTML and outside the script definition-although you can't see this from the example above.

Browse. NET page

The code in Listing B is used to display customer data in a browser (with HTML table templates to display individual data entries).

Copy and paste the scripts into any text editor and save them as. aspx files. The file is then saved or copied to the Web root directory (the root of this example is Wwwroot\nettest). Start your browser and enter the appropriate address, which opens the file. When the browser loads the file (as shown in Figure a), the browser displays a simple HTML page that contains the data returned by the request for the SQL statement.

Figure A

In the browser. NET page

You can also improve the appearance of the page. For example, you can change the color of each row by adding additional template blocks. Alternatingitem.template blocks can change the background color of each table cell. The following script changes the background of the unit to Yellow (FFFF00):
<AlternatingItemTemplate>
<tr bgcolor= "#FFFF00" >
<td><% #Container. DataItem ("CompanyName")%></td>
<td><% #Container. DataItem ("ContactName")%></td>
<td><% #Container. DataItem ("Address")%></td>
<td><% #Container. DataItem ("City")%></td>
<td><% #Container. DataItem ("Region")%></td>
</tr>
</AlternatingItemTemplate>

To work on all other rows, place the AlternatingItemTemplate block after the ItemTemplate block.

Section
Although. NET is not particularly fresh, but if you have not yet implemented from the traditional ASP to the ASP.net jump, then it is not too late to start. If you have extensive experience in ASP and ADO, this transformation will be quite simple.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.