asp.net 1.2.2 Content and code separation
The current website construction usually requires the developer to do the backstage program design, the front has the professional art to do the interface designs. Although sometimes developers do some interface design, but often do not meet the professional requirements. As mentioned above, in the previous ASP, because the Code and HTML page language mixed together, this makes the website construction becomes quite difficult. In asp.net, Microsoft solved the problem by using code.
We now build an HTML page, as follows:
<%@ Page language= "C #" codebehind= "WebForm1.aspx.cs"
Autoeventwireup= "false" inherits= "Aspcool.webform1"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name= "generator" content= "Microsoft Visual Studio 7.0" >
<meta name= "Code_language" content= "C #" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<font face= "Song Body" >
<asp:textbox id= "TextBox1" style= "Z-INDEX:101; left:47px; Position:absolute; TOP:23PX "runat=" Server ></asp:TextBox>
<asp:button id= "Button1" style= "z-index:102; left:53px; Position:absolute; TOP:76PX "runat=" Server "text=" button "></asp:Button></FONT>
</form>
</body>
From the first line we can see that the background code for the page is all in the WebForm1.aspx.cs file. We can write a program in this file, as follows:
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Namespace Aspcool
{
<summary>
Summary description for WebForm1.
</summary>
public class WebForm1:System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load (object sender, System.EventArgs e)
{
Put user code to initialize the page here
}
#region Web Form Designer generated code
Override protected void OnInit (EventArgs e)
{
//
Codegen:this the call are required by the ASP.net Web Form Designer.
//
InitializeComponent ();
Base. OnInit (e);
}
<summary>
Required to Designer support-do not modify
The contents is with the Code Editor.
</summary>
private void InitializeComponent ()
{
This. Button1.Click + = new System.EventHandler (this. Button1_Click);
This. Load + = new System.EventHandler (this. Page_Load);
}
}
}
With the code behind, developers can modify the. cs file directly (in Visual Basic.NET. vb files). The page designer can modify the HTML page, which greatly simplifies the construction process of the website.
1.2.3 ASP. NET-rich Web controls
Asp. NET's other advantage is that it provides us with a lot of rich Web controls. You can find a wide variety of Web controls in the System.Web.UI.WebControls namespace, including the from controls running on the server side, such as Button, TextBox, and some special-purpose controls, such as: Ad rotation controls , calendar controls, and user validation controls, and so on. Here are a few examples to see how to use these controls.
1. Ad rotation Controls <asp:adrotator>
The ad rotation control can display rotated advertisements on the Web page. The ad link information is stored in an XML file, such as Ads.xml.
We now add a <asp:adrotator> to the ASP.net page, as follows:
<HTML>
<HEAD>
<title> Advertising Rotation Program </title>
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<asp:adrotator id= "AdRotator1" runat= "Server" width= "468px" height= "60px" advertisementfile= "Ads.xml" ></ Asp:adrotator>
</form>
</body>
</HTML>
Run this program and you'll get a page that spins the display ads.
2. Calendar Controls <asp:calendar>
With the calendar controls, we can quickly generate a Web page for a calendar. The code is as follows:
<HTML>
<HEAD>
<title> Advertising Rotation Program </title>
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<asp:calendar id= "Calendar1" runat= "Server" ></asp:Calendar>
</form>
</body>
</HTML>
In Visual Studio.NET, you can use it by dragging it directly from the toolbar.
3. validation controls
We used to write our own JavaScript scripts to validate the information that the user entered. In ASP.net, the code is automatically generated as long as we use a validation control. It is able to prompt the user for the error message entered. Next I'll list the validation controls for you:
Control Name Description
RequiredFieldValidator ensure that the user does not skip input.
CompareValidator uses the comparison operator (less than, equal to, greater than, and so on) to compare the user's input with the constant value or property value of another control.
RangeValidator checks whether the user's input is between the specified upper and lower bounds. You can check numbers, letters, or dates within the range. You can represent a boundary as a constant.
RegularExpressionValidator checks whether the input matches the pattern defined by the regular expression. This validation type allows you to check for predictable sequences of characters, such as Social Security numbers, e-mail addresses, phone numbers, postal codes, and so on.
CustomValidator checks the user's input using the validation logic that you write yourself. The validation type allows you to check the values that are exported at run time.
ValidationSummary Displays validation errors for all validators on the page as a summary.
Well, since ASP.net has so many advantages, let's start installing and using asp.net now!
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