ASP.NET学习笔记(一)

一、ASP.NET以文件作为开发单位

二、新建站点位置:
      1、Local IIS
      2、FTP Site
      3、File System  自带Web服务器
      4、Remote Site

三、页面结构
      两种: Code inline model 和 Code behind model

      1、Code inline model

            全面支持 IntelliSense

            新建页面时,去掉选择 “Place code in seprate file” 选项
            
<% @ Page Language = ”C#”  %>

<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

< script runat = ”server” >
protected   void  Button1_Click( object  sender, System.EventArgs e)
{
Label1.Text 
=  “Hello “  +  Textbox1.Text;
}
</ script >

< html xmlns = ”http: // www.w3.org/1999/xhtml” >
< head runat = ”server” >
< title > Simple Page </ title >
</ head >
< body >
< form runat = ”server” >
What 
is  your name ?< br  />
< asp:Textbox ID = ”Textbox1” Runat = ”server” ></ asp:Textbox >< br  />
< asp:Button ID = ”Button1” Runat = ”server” Text = ”Submit”
OnClick
= ”Button1_Click”  />
< p >< asp:Label ID = ”Label1” Runat = ”server” ></ asp:Label ></ p >
</ form >
</ body >
</ html >


      
      2、Code behind model

可以使用 partial classes 编写后台代码

<% @ Page Language = ”C#” CodeFile = ”Default.aspx.cs” Inherits = ”Default_aspx”  %>

<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< html xmlns = ”http: // www.w3.org/1999/xhtml” >
< head runat = ”server” >
< title > Simple Page </ title >
</ head >
< body >
< form runat = ”server” >
What 
is  your name ?< br  />
< asp:Textbox ID = ”Textbox1” Runat = ”server” ></ asp:Textbox >< br  />
< asp:Button ID = ”Button1” Runat = ”server” Text = ”Submit”
OnClick
= ”Button1_Click”  />
< p >< asp:Label ID = ”Label1” Runat = ”server” ></ asp:Label ></ p >
</ form >
</ body >
</ html >


using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

public  partial  class  _Default : System.Web.UI.Page
{
         protected   void  Button1_Click( object  sender, EventArgs e)
         {
                  Label1.Text 
=  “Hello “  +  Textbox1.Text;
         }
}


     


四、页面指令(page directives)    

格式  <%@ [Directive] [Attribute=Value] [Attribute=Value] %>

 

Directive

Description

Assembly

Links an assembly to the Page or user control for which it is associated.

Control

Page directive meant for use with user controls (.ascx).

Implements

Implements a specified .NET Framework interface.

Import

Imports specified namespaces into the Page or user control.

Master

Enables you to specify master page–specific attributes and values to use when the page parses or compiles. This directive can be used only with master pages (.master).

MasterType

Associates a class name to a Page in order to get at strongly typed refer-ences or members contained within the specified master page.

OutputCache

Controls the output caching policies of a Page or user control.

Page

Enables you to specify page specific attributes and values to use when the page parses or compiles. This directive can be used only with ASP.NET pages (.aspx).

PreviousPageType

Enables an ASP.NET page to work with a postback from another page in the application.

Reference

Links a Page or user control to the current Page or user control.

Register

Associates aliases with namespaces and class names for notation in cus-tom server control syntax.

 

五、页面事件

  顺序为:

1.

PreInit

2.

Init

3.

InitComplete

4.

PreLoad

5.

Load

6.

LoadComplete

7.

PreRender

8.

PreRenderComplete

9.

Unload



1.1 中的事件

 

AbortTransaction

CommitTransaction

❑  

DataBinding

Disposed

Error

Init

Load

PreRender

Unload


2.0新增

 

InitComplete: Indicates the initialization of the page is completed.

LoadComplete: Indicates the page has been completely loaded into memory.

PreInit: Indicates the moment immediately before a page is initialized.

PreLoad: Indicates the moment before a page has been loaded into memory.

PreRenderComplete: Indicates the moment directly before a page has been rendered in the browser.


六、处理回传

1、默认回传给自己

if (Page.IsPostBack == true) {
// Do processing
}

或者

if (!Page.IsPostBack) {
// Do processing



2、Cross-Page Posting 

使用 PostBackUrl

page1.aspx

<% @ Page Language = ”C#”  %>
< script runat = ”server” >
protected   void  Button1_Click ( object  sender, System.EventArgs e)
{
Label1.Text 
=  “Hello “  +  TextBox1.Text  +  “ < br  /> ”  +
“Date Selected: “ 
+  Calendar1.SelectedDate.ToShortDateString();
}
</ script >

< title > First Page </ title >
</ head >
< body >
< form id = ”form1” runat = ”server” >
Enter your name:
< br  />
< asp:Textbox ID = ”TextBox1” Runat = ”server” >
</ asp:Textbox >
< p >
When 
do  you want to fly ?< br  />
< asp:Calendar ID = ”Calendar1” Runat = ”server” ></ asp:Calendar ></ p >
< br  />
< asp:Button ID = ”Button1” Runat = ”server” Text = ”Submit page to itself”
OnClick
= ”Button1_Click”  />
< asp:Button ID = ”Button2” Runat = ”server” Text = ”Submit page to Page2.aspx”
PostBackUrl
= ”Page2.aspx”  />
< p >
< asp:Label ID = ”Label1” Runat = ”server” ></ asp:Label ></ p >
</ form >
</ body >
</ html >

page2.aspx


<% @ Page Language = ”C#”  %>
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
protected   void  Page_Load( object  sender, System.EventArgs e)
{
TextBox pp_Textbox1;
Calendar pp_Calendar1;
pp_Textbox1 
=  (TextBox)PreviousPage.FindControl(“Textbox1”);
pp_Calendar1 
=  (Calendar)PreviousPage.FindControl(“Calendar1”);
Label1.Text 
=  “Hello “  +  pp_Textbox1.Text  +  “ < br  /> ”  +  “Date Selected: “  +
pp_Calendar1.SelectedDate.ToShortDateString();
}
</ script >

< html xmlns = ”http: // www.w3.org/1999/xhtml” >
< head runat = ”server” >
< title > Second Page </ title >
</ head >
< body >
< form id = ”form1” runat = ”server” >
< asp:Label ID = ”Label1” Runat = ”server” ></ asp:Label >
</ form >
</ body >
</ html >

或者

作为属性公开访问


<% @ Page Language = ”C#”  %>
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
public  TextBox pp_TextBox1
{
get
{
return  TextBox1;
}
}
public  Calendar pp_Calendar1
{
get
{
return  Calendar1;
}
}
protected   void  Button1_Click ( object  sender, System.EventArgs e)
{
Label1.Text 
=  “Hello “  +  TextBox1.Text  +  “ < br  /> ”  +
“Date Selected: “ 
+  Calendar1.SelectedDate.ToShortDateString();
}
</ script >


然后就可以直接访问

<% @ Page Language = ”C#”  %>
<% @ PreviousPageType VirtualPath = ”Page1.aspx”  %>
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
protected   void  Page_Load( object  sender, System.EventArgs e)
{
Label1.Text 
=  “Hello “  +  PreviousPage.pp_TextBox1.Text  +  “ < br  /> ”  +
“Date Selected: “ 
+
PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();
}
</ script >


判断使用Page.IsCrossPagePostBack

<% @ Page Language = ”C#”  %>
<% @ PreviousPageType VirtualPath = ”Page1.aspx”  %>
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
protected   void  Page_Load( object  sender, System.EventArgs e)
{
if  (Page.IsCrossPagePostBack) {
Label1.Text 
=  “Hello “  +  PreviousPage.pp_Textbox1.Text  +  “ < br  /> ”  +
“Date Selected: “ 
+
PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();
}
else
{
Response.Redirect(“Page1.aspx”);
}
}
</ script >

六、Application Folders

1、/App_Code

to store your classes, .wsdl files, and typed datasets

添加好之后,立即对项目中的所有页面可用
不需要事先编译
不需要引用
支持IntelliSence
该文件夹中所有的类都被编译到一个单独的程序集中
对语言没有要求

使用多种开发语言

/App_Code

      /VB

               Add.vb

      /CS

               Subtract.cs


然后修改 Web.Config 文件

<compilation>
      <codeSubDirectories>
            <add directoryName=”VB”></add>
            <add directoryName=”CS”></add>
      </codeSubDirectories>
</compilation>

可以使用任何名称代替 “VB” “CS”

2、/App_Data

It is a good spot to centrally store all the data stores your application might use.

可以保存Access数据文件、XML等,

from the membership and role management systems to the GUI tools such as the ASP.NET MMC snap-in and ASP.NET Web Site Administration Tool—is built to work with the /App_Data folder.

3、/App_Themes

4、/App_GlobalResources

是一张string tables,加图片、文件等等 ,对所有页面可用

多语言

Resource.resx

Resource.fi-FI.resx

使用时

<% @ Page Language = ”C#” Culture = ”Auto” UICulture = ”Auto”  %>
<! DOCTYPE html PUBLIC “ - // W3C // DTD XHTML 1.1 // EN”
“http: // www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
< script runat = ”server” >
protected   void  Page_Load( object  sender, System.EventArgs e)
{
Page.Title 
=  Resources.Resource.PageTitle;
}
protected   void  Button1_Click( object  sender, System.EventArgs e)
{
Label1.Text 
=  Resources.Resource.Answer  +  “ “  +  Textbox1.Text;
}
</ script >


< html xmlns = ”http: // www.w3.org/1999/xhtml” >
< head id = ”Head1” runat = ”server” >
< title ></ title >
</ head >
< body >
< form id = ”Form1” runat = ”server” >
< p ><%=  Resources.Resource.Question  %></ p >< br  />
< asp:TextBox ID = ”Textbox1” Runat = ”server” ></ asp:TextBox >< br  />
< asp:Button ID = ”Button1” Runat = ”server” Text = ”Submit”
OnClick
= ”Button1_Click”  />
< p >< asp:Label ID = ”Label1” Runat = ”server” ></ asp:Label ></ p >
</ form >
</ body >
</ html >


5、/App_LocalResources

对指定页面可用
 

Default.aspx.resx

Default.aspx.fi.resx

Default.aspx.ja.resx

Default.aspx.en-gb.resx


6、/App_WebReferences
替代以前的WebReferences

7、/App_Browsers
保存XML文件  .browser,可以从C:/Windows/Microsoft.NET/Framework/v2.0xxxxx/
CONFIG/Browsers 复制

七、编译

1、消除预编译延时

in-place precompilation. 可以编译整个程序,还可以检查运行时错误。

内置Web服务器
http://[host]:[port]/[Application Name]/precompile.axd

IIS
http://[host]/[Application Name]/precompile.axd

2、precompilation for deployment.

把整个站点编译到Dll中,进行部署。

使用命令行:

aspnet_compiler -v [Application Name] –p [Physical Location] [Target]

例如

aspnet_compiler –v /INETA –p C:/Websites/INETA C:/Wrox

 

Command

Description

-m

Specifies the full IIS metabase path of the application. If you use the -m

command, you cannot use the -vor -pcommand.

-v

Specifies the virtual path of the application to be compiled. If you also

use the -pcommand, the physical path is used to find the location of the application.

-p

Specifies the physical path of the application to be compiled. If this is

not specified, the IIS metabase is used to find the application.

targetDir

Specifies the target directory where the compiled files should be

placed. If this is not specified, the output files are placed in the appli-cation directory.



编译后,整个项目目录结构和文件都保持一致,文件内容变成


This is a marker file generated by the precompilation tool
and should not be deleted!

生成 文件 Code.dll

不能编译的文件类型

 

HTML files

XML files

XSD files

web.configfiles

Text files



八、Global.asax

 

Application_Start: Called when the application receives its very first request. It is an ideal

 

spot in your application to assign any application-level variables or state that must be main-

 

tained across all users.

Session_Start: Similar to the Application_Startevent except that this event is fired when an

 

individual user accesses the application for the first time. For instance, the Application_Start

 

event fires once when the first request comes in, which gets the application going, but the

 

Session_Startis invoked for each end user who requests something from the application for

 

the first time.

Application_BeginRequest: Although it not listed in the preceding template provided by

 

Visual Studio 2005, the Application_BeginRequestevent is triggered before each and every

 

request that comes its way. This means that when a request comes into the server, before this

 

request is processed, the Application_BeginRequestis triggered and dealt with before any

 

processing of the request occurs.

Application_AuthenticateRequest: Triggered for each request and enables you to set up

 

custom authentications for a request.

Application_Error: Triggered when an error is thrown anywhere in the application by any

 

user of the application. This is an ideal spot to provide application-wide error handling or an

 

event recording the errors to the server’s event logs.

Session_End: When running in InProcmode, this event is triggered when an end user leaves

 

the application.

Application_End: Triggered when the application comes to an end. This is an event that most

 

ASP.NET developers won’t use that often because ASP.NET does such a good job of closing and

 

cleaning up any objects that are left around.


可以使用的指令

 

@Application

@Assembly

@Import

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值