ASP.NET笔记(一)


目 录

1、页面生命周期

2、页语法
3、ASP.NET Web页面代码模型
4、页面和应用程序上下文
5、Page类的继承模型
6、ASP.NET Web页面中的客户端脚本
7、给服务器控件添加客户端脚本
8、ASP.NET控件
9、数据绑定
10、事件、事件句柄、事件参数
11、服务器控件:怎样用一个事件句柄处理多个事件
12、动态绑定事件句柄
13、知识点参考

 


 

 

一、页面生命周期

页面框架通过如下过程处理aspx文件请求:
1:解析aspx文件,并创建一个控件树;
2:使用控件树动态实现一个继承自Page类的类或者控件 ;
3:动态编译类;
4:缓存编译类,以备后用;
5:动态创建一个编译类的实例。页面开始启动生命期,在这个过程中,页面将通过生命周期的不同阶段;=========================================================
页面经历了如下阶段【其中一些阶段标记为仅仅回传,是说只有在被回传到服务器时,页面才经历这些阶段】:

 

01:页面首先从QueryString或者Request对象的Form集合中获得回传数据。

02:页面检测回传数据集合(NameValueCollection,Form或者QueryString)是否包含一个键为_CallBackId的项。如 果存在,那么设置其Boolean属性IsCallBack为True,以便通过AspNet客户端回调机制,标明页面已经回传到服务器。

03:预初始化(PreInit):
在页面生命周期的预初始化阶段执行如下操作:
a:调用OnPreInit方法引发PreInit事件。
b:利用App_Themes目录中的内容初始化主题,以动态实现一个PageTheme类型的类,
编译该类,并创建一个编译类的实例,接着将实例赋值给它的PageTheme属性值
c:应用母版页

04:初始化(Init):
在页面生命周期的初始化阶段执行以下操作
a:递归初始化Controls集合中的控件。初始化包括设置这些控件的属性,
例如:Page,Id和NameContainer等
b:递归应用控件皮肤
c:调用OnInit方法以引发自身的Init事件,接着递归调用子控件的OnInit方法来引发它们的Init事件
d:调用自身的TrackViewState方法来启动自身的视图状态跟踪,接着递归调用子控件
的TrackViewState方法来启动它们的视图状态跟踪。

05:完成初始化(InitComplete):
页面调用OnInitComplete方法来引发InitComplete事件。该事件标明了初始化阶段的结束。
此时,页面Controls集合的所有控件都被初始化了。


06:加载控件状态(Load Control State)[PostBack Only]:
页面递归调用Control集合中控件的LoadControlState方法,这些控件已经调用了Page类
的RegisterRequiresControlState方法,以使用它们的控件状态。

07:加载视图状态(Load View State)[PostBack Only]:
页面首先调用自身的LoadViewState方法,接着递归调用Controls集合控件的LoadViewState方法,以允许它们加载它们的已经保存的视图状态。

08:加载回传数据(Load Post Data)[PostBack Only]:
页面调用实现IPostBackDataHandler接口的控件的LoadPostData方法,并将回传数据传递给该方法。每个控件的LoadPostDate方法都必须访问回传数据,并据此更新相应的控件属性。
例如:TextBox控件的LoadPostData方法将文本框的新值设置为TextBox控件的Text属性值。

09:预加载(PreLoad):
页面调用OnPreLoad方法以引发PreLoad事件。该事件表示页面生命周期将进入加载阶段。

10:加载(Load):
页面首先调用自身的OnLoad方法以引发自身的Load事件,接着递归调用Controls集合中控件的OnLoad方法以引发它们的Load事件。页面开发人员可以为Load事件注册回调,那么就可以通过编程将子控件添加到页面的Controls集合中。

11:加载回传数据(Load Post Data)[PostBack Only Second Try]:
页面调用控件的LoadPostBack方法。这些控件如果实现了IPostBackDataHandler接口,那么在加载阶段,它们已通过编程添加到Controls集合中。

12:引发回传数据修改事件(Raise Post Data Changed Event)[PostBack Only]:
页面调用控件的RaisePostDataChangeEvent方法,这些控件的LoadPostData方法返回true。
RaisePostDataChangeEvent方法引发了回传数据修改事件。例如:当文本框的新值与旧值
不同时,那么TextBox控件将引发该事件。

13:引发回传事件(Raise PostBack Event)[PostBack Only]:
页面调用控件的RaisePostEvent方法,这些控件相关的Html元素用于提交表单。例如,Button控件的相关Html元素将页面回传到服务器。控件的RaisePostBackEvent方法必须将回传事件映射到一个或多个服务器事件。例如,Button控件的RaisePostBackEvent方法将事件映射到了服务器端事件Command和Click上。

14:完成加载(Load Complete):
页面调用OnLoadComplete方法来引发LoadComplete事件,这表示所有加载活动,包括加载回传数据,以及引发回传数据修改事件,并以更新控件自身的活动都完成了。

15:引发回调事件(Raise CallBack Event)(PostBack And CallBack Only):
页面调用控件的RaiseCallBackEvent方法。该控件可使用AspNet客户端回调机制来允许客户端方法(例如JavaScript函数)调用服务器端方法,而无需将整个页面回传给服务器。
RaiseCallBackEvent方法必须调用服务器端方法。如果页面的回传使用了客户端回调机制,那么页面将不会执行剩余的页面生命周期阶段。

16:预呈现(PreRender):
在页面生命周期这个阶段执行一下操作。
a:调用EnsureChildControls方法来确保在页面进入呈现阶段之前,创建其子控件。
b:调用自身的OnPreRender方法来引发PreRender事件。
c:递归调用Controls集合中控件的OnPreRender方法,以引发它们的PreRender事件。

17:预呈现完成(PreRender Complete):
页面调用OnPrerenderComplete方法来引发PreRenderComplete事件,这表示所有预呈现活动完成了。

18:保存控件状态(Save Control State):
页面递归调用Controls集合中控件的SaveControlState方法。这些控件已经调用了Page类的RegisterRequiresControlState方法来保存它们的控件状态。

19:保存视图状态(Save View State):
页面首先调用自身的SaveViewState方法,然后调用Controls集合中的SaveViewState方法,以允许它们来保存其视图状态。

20:保存状态完成(Save View Complete):
页面调用OnSaveStateComplete方法以引发SaveStateComplete事件,这表示所有状态保存活动都完成了。

21:呈现:
在页面生命周期的这个阶段执行一下操作。
a:创建一个HtmlTextWriter类实例,该实例封装了输出响应流
b:调用RenderCOntrol方法,并将HtmlTextWriter实例传递给该方法。
RenderControl方法递归调用子控件的RenderControl方法,以便允许每个控件能够呈现其
Html标记文本。子控件的Html标记文本组成了最终发送给客户端浏览器的Html标记文本。

 

ASP.NET页面生命周期:http://hi.baidu.com/wwhhyx/item/3e45132761400f50c28d594d

ASP.NET2.0页面生命周期:http://www.dwww.cn/News/2007-10/200710151643485771.shtml

ASP.net事件执行顺序:http://readwall.blog.163.com/blog/static/10127132200821105622188/

 

 

二、页语法

 

1、指令语法

@ Page              定义 ASP.NET 页面的配置选项。如页面中代码的服务器编程语言,页面代码模式,调试和跟踪选项,是否与母板面关联。只能包含在 .aspx 文件中。参见:http://msdn.microsoft.com/en-us/library/ydy4x04a

http://msdn.microsoft.com/zh-cn/library/ydy4x04a
@ Control           定义 ASP.NET 页分析器和编译器使用的控件特定属性。只能包含在 .ascx 文件(用户控件)中。参见:http://msdn.microsoft.com/en-us/library/d19c0t4b
@ Import            指定想在代码中使用的命名空间。参见:http://msdn.microsoft.com/en-us/library/eb44kack
@ Implements        以声明的方式指示页或用户控件实现指定的 .NET Framework 接口。参见:http://msdn.microsoft.com/en-us/library/cbsf6k72
@ Register          将别名与命名空间及类名关联起来,从而允许用户控件和自定义服务器控件在被包括到请求的页或用户控件时呈现。声明控件的标签前缀,控件程序集的位置。参见:http://msdn.microsoft.com/en-us/library/c76dd5k1.aspx
@ Assembly          在编译过程中将程序集链接到当前页,以使程序集的所有类和接口都可用在该页上。参见:http://msdn.microsoft.com/en-us/library/d864zc1k
@ Master            标识 ASP.NET 母版页。参见:http://msdn.microsoft.com/en-us/library/ms228176
@ PreviousPageType  提供用于获得上一页的强类型的方法,可通过 PreviousPage 属性访问上一页。参见:http://msdn.microsoft.com/en-us/library/ms228169
@ MasterType        为 ASP.NET 页的 Master 属性分配类名,使得该页可以获取对母版页成员的强类型引用。参见:http://msdn.microsoft.com/en-us/library/ms228274
@ OutputCache       以声明的方式控制页或用户控件的输出缓存策略。参见:http://msdn.microsoft.com/en-us/library/hdxfb6cy
@ Reference         以声明的方式将页或用户控件链接到当前页或用户控件。参见:http://msdn.microsoft.com/en-us/library/w70c655a

 

<% Master  Language = " C# "  AutoEventWireup = " true "  CodeFile = " Site.master.cs "  Inherits = " SiteMaster "   %>

 

<% Page  Title = " 主页 "  Language = " C# "  MasterPageFile = " ~/Site.master "  AutoEventWireup = " true "
    CodeFile
= " Default.aspx.cs "  Inherits = " _Default "   %>

 


2、代码声明块
代码声明块定义嵌入 ASP.NET 应用程序文件内使用 runat="server" 属性标记的 script 块中的服务器代码节。

< script  runat ="server"  language ="codelanguage"  Src ="pathname" >
   Code goes here...
</ script >

 

< html >
   < script  language ="C#"  runat ="server" >
      
void  EnterBtn_Click(Object Src, EventArgs E) {
          Message.Text 
=   " Hi  "   +  Name.Text  +   " , welcome to ASP.NET! " ;
      }
  
</ script >

   < body >
    < form  runat ="server" >
    Enter your name:  < asp:textbox  id ="Name"  runat =server /> 
                      < asp:button  text ="Enter"  Onclick ="EnterBtn_Click"  runat ="server" />
         < p >
         < asp:label  id ="Message"  runat =server />
     </ form >
   </ body >
</ html >


3、代码呈现块
代码呈现块定义当呈现页时执行的内联代码或内联表达式。存在两种样式的代码呈现块:内联代码和内联表达式。使用内联代码可以定义独立的行或代码块。将内联表达式用作调用 Write 方法的快捷方式。

<% @ page language = " C# "   %>

  

<%  Response.Write( "  %> " );  %>

 

<%   for  ( int  i = 0 ; i < 10 ; i ++ ) {  %>
      < font  size ="<%=i %>" > Hello World!  </ font >
<%  }  %>

 


4、服务器端注释

<% --  Content of comments,  or  commented out server controls  -- %>



5、自定义服务器控件语法
自定义服务器控件语法用于将用户控件和自定义服务器控件声明为 ASP.NET 应用程序文件中的标记元素,包括网页、用户控件和母版页。此语法几乎与用于声明所有 ASP.NET 服务器控件的语法相同,其中的差别仅在于:对于自定义和用户控件,您通常会声明一个唯一的标记前缀和一个与您的控件相对应的标记名称。

< tagprefix:tagname  id ="OptionalID"
   attributename
="value"
   eventname
="eventhandlermethod"
   runat
="server"   />

OR

< tagprefix:tagname  id ="OptionalID"
   runat
="server"   />

 

// A custom Button control to reference in the page.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Security.Permissions
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Samples.AspNet.CS.Controls
{
  [AspNetHostingPermission(SecurityAction.Demand,
  Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class CustomButton : Button
  {
    public CustomButton()
    {
      this.Text = "Click Here";
    }
  }
}

<!--  A page that references the custom control.  -->
<% @Page language = " C# "   %>
<!--  This directive does not require the assembly attribute 
     because the source file is in the App_Code directory, 
     so it gets dynamically compiled with the page. 
-->
<% @ Register TagPrefix = " custom "       namespace = " Samples.AspNet.CS.Controls "   %>
    
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>


< html >   
  < script  runat ="server" >
     private 
void  custButton_Click(Object sender, EventArgs e)
     {
       TextBox.BackColor 
=  System.Drawing.Color.Green;
       TextBox.Text 
=   " You clicked the button " ;
     }       
 
</ script >
  < body >      
     < form  id ="Form1"  runat =server >
       Here is the custom button. < br >
        < custom:CustomButton  runat ="server"  id ="custButton"           onclick ="custButton_Click"   /> 
        < br >
        < br >
        < asp:TextBox  id  = "TextBox"  Text ="Click the button"
        Width 
= "200"  BackColor ="Cyan"  runat ="server"   /> 
        < br >      
     </ form >         
   </ body >          
</ html >


 

6、数据绑定表达式语法
当在页上调用 DataBind 方法时,数据绑定表达式创建服务器控件属性和数据源之间的绑定。可以将数据绑定表达式包含在服务器控件开始标记中属性/值对的值一侧,或页中的任何位置。

< tagprefix:tagname  property ="<%# data-binding expression %>"
   runat
="server"   />

- or -

literal text  <% # data - binding expression  %>

 

< html >
< head >
     < script  language ="C#"  runat ="server" >
        
void  SubmitBtn_Click(Object sender, EventArgs e) {
          
//  Rather than explictly pulling out the variable from the StateList control
           //  and then manipulating a Label control, just call Page.DataBind.
           //  This will evaluate any <%# %> expressions within the page.   
          Page.DataBind();
        }
    
</ script >
</ head >
< body >

     < h3 >< font  face ="Verdana" >Binding to a property of another server control </ font ></ h3 >
     < form  runat ="server" >
         < asp:DropDownList  id ="StateList"  runat ="server" >
           < asp:ListItem >CA </ asp:ListItem >
           < asp:ListItem >IN </ asp:ListItem >
           < asp:ListItem >KS </ asp:ListItem >
           < asp:ListItem >MD </ asp:ListItem >
           < asp:ListItem >MI </ asp:ListItem >
           < asp:ListItem >OR </ asp:ListItem >
           < asp:ListItem >TN </ asp:ListItem >
           < asp:ListItem >UT </ asp:ListItem >
         </ asp:DropDownList >       
         < asp:button  Text ="Submit"  OnClick ="SubmitBtn_Click"  runat ="server" />        
         < p >     
        Selected State:  < asp:label  text ='<%#  StateList.SelectedItem.Text % >' runat="server"/>     
     </ form >
</ body >
</ html >

 


7、服务器端对象标记语法
在 Web 窗体页中声明并创建 COM 和 .NET Framework 对象

< object  id ="id"  
        runat
="server"  
        latebinding
="true|false"  
        class
="Class Name" >
< object  id ="id"  
        runat
="server"  
        latebinding
="true|false"  
        progid
="COM ProgID" />
< object  id ="id"  
        runat
="server"  
        latebinding
="true|false"  
        classid
="COM ClassID" />

 

<% @Page language = " C# "   %>
< html >
    < object  id ="items"  class ="System.Collections.ArrayList"  runat ="server"   />
    < script  language ="C#"  runat =server >
      
void  Page_Load(Object sender, EventArgs e) {
         items.Add(
" One " );
         items.Add(
" Two " );
         items.Add(
" Three " );

         MyList.DataSource 
=  items;
         MyList.DataBind();
      }
   
</ script >

    < body >
       < form  id ="form1"  runat ="server" >
       < asp:datalist  id ="MyList"  runat =server >
          < ItemTemplate >
            Here is a value:  <% # Container.DataItem  %>
          </ ItemTemplate >
       </ asp:datalist >
       </ form1 >
    </ body >
</ html >

 


8、服务器端包括指令语法
将指定文件的内容插入 ASP.NET 文件中,包括网页(.aspx 文件)、用户控件文件(.ascx 文件)和 Global.asax 文件。

<!--  #include file|virtual="filename"  -->

 

< html >
    < body >
       <!--  #Include virtual="/include/header.inc"  -->
        Here is the main body of the .aspx file.
       <!--  #Include virtual="/include/footer.inc"  -->
    </ body >
</ html >

 

 

三、ASP.NET Web页面代码模型

(1)单文件页面模型

页面的标签和程序代码在同一个物理.aspx文件中。程序代码在包括runat="server"属性的script块中。 runat="server"属性表明这块代码是由ASP.NET执行的。程序代码是Click事件的处理代码。

<%@ Page Language= " C# " %>
<script runat= " server ">
void Button1_Click(Object sender, EventArgs e)
{
    Label1.Text =  " Clicked at  " + DateTime.Now.ToString();
}
</script>
<html>
<head>
  <title>Single-File Page Model</title>
</head>
<body>
  <form runat= " server ">
    <div>
       <asp:Label id= " Label1 " 
         runat= " server " Text= " Label ">
       </asp:Label>
       <br />
       <asp:Button id= " Button1 " 
         runat= " server " 
         οnclick= " Button1_Click " 
         Text= " Button ">
      </asp:Button>
    </div>
  </form>
</body>
</html>

(2)Code-Behand模式

这种模式允许你针标签放在.aspx文件,程序放在另一个文件。

<%@ Page Language= " C# " CodeFile= " SamplePage.aspx.cs " 
    Inherits= " SamplePage " AutoEventWireup= " true " %>
<html>
<head runat= " server " >
   <title>Code-Behind Page Model</title>
</head>
<body>
  <form id= " form1 " runat= " server ">
    <div>
       <asp:Label id= " Label1 " 
         runat= " server " Text= " Label " >
      </asp:Label>
      <br />
      <asp:Button id= " Button1 " 
         runat= " server " 
         οnclick= " Button1_Click " 
         Text= " Button " >
       </asp:Button>
    </div>
  </form>
</body>
</html>

 

 

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public  partial  class SamplePage : System.Web.UI.Page
{
     protected  void Button1_Click( object sender, EventArgs e)
    {
        Label1.Text =  " Clicked at  " + DateTime.Now.ToString();
    }
}

 

 

四、页面和应用程序上下文

当一个Web应用程序运行时,ASP.NET保持当前的应用程序,每个用户会话,当前HTTP请求,请求的页面等信息。ASP.NET包含了一系列的类来封装这方面的信息。ASP.NET可作为内在对象,你可以从你的代码访问这些类的实例。下表列出了这些内在的对象和他们的类的实例。

Object Name

Description

ASP.NET Class

Response

Provides access to the output stream for the current page. You can use this class to inject text into the page, to write cookies, and more. For details, see Page.Response property.

HttpResponse

Request

Provides access to the current page request, including the request headers, cookies, client certificate, query string, and so on. You can use this class to read what the browser has sent. For details, see Page.Request property.

HttpRequest

Context

Provides access to the entire current context (including the request object). You can use this class to share information between pages. For details, see Page.Context property.

HttpContext

Server

Exposes utility methods that you can use to transfer control between pages, get information about the most recent error, encode and decode HTML text, and more. For details, see Page.Server property.

HttpServerUtility

Application

Provides access to application-wide methods and events for all sessions. Also provides access to an application-wide cache you can use to store information. For details, see ASP.NET Application State Overview.

HttpApplicationState

Session

Provides information to the current user session. Also provides access to a session-wide cache you can use to store information, along with the means to control how the session is managed. For details, see ASP.NET Session State Overview.

HttpSessionState

Trace

Provides a way to display both system and custom trace diagnostic messages in the HTTP page output. For details, see ASP.NET Tracing Overview.

TraceContext

 

从Application Status读值:

if (Application[ " AppStartTime "] !=  null)
{
    DateTime myAppStartTime = (DateTime)Application[ " AppStartTime "];
}

 

Application[ " Message "] =  " Welcome to the Contoso site. ";

 

读写Cookie

if (Request.Cookies[ " UserSettings "] !=  null)
{
     string userSettings;
     if (Request.Cookies[ " UserSettings "][ " Font "] !=  null)
    { userSettings = Request.Cookies[ " UserSettings "][ " Font "]; }
}

 

Response.Cookies[ " UserSettings "][ " Font "] =  " Arial ";
Response.Cookies[ " UserSettings "][ " Color "] =  " Blue ";
Response.Cookies[ " UserSettings "].Expires = DateTime.Now.AddDays(1d);

 

HttpCookie myCookie =  new HttpCookie( " UserSettings ");
myCookie[ " Font "] =  " Arial ";
myCookie[ " Color "] =  " Blue ";
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);

 

处理应用程序级的错误:

void Application_Error( object sender, EventArgs e)
{
   //  Code that runs when an unhandled error occurs

  
//  Get the exception object.
  Exception exc = Server.GetLastError();

   //  Handle HTTP errors
   if (exc.GetType() ==  typeof(HttpException))
  {
     //  The Complete Error Handling Example generates
    
//  some errors using URLs with "NoCatch" in them;
    
//  ignore these here to simulate what would happen
    
//  if a global.asax handler were not implemented.
       if (exc.Message.Contains( " NoCatch ") || exc.Message.Contains( " maxUrlLength "))
       return;

     // Redirect HTTP errors to HttpError page
    Server.Transfer( " HttpErrorPage.aspx ");
  }

   //  For other kinds of errors give the user some information
  
//  but stay on the default page
  Response.Write( " <h2>Global Page Error</h2>\n ");
  Response.Write(
       " <p> " + exc.Message +  " </p>\n ");
  Response.Write( " Return to the <a href='Default.aspx'> " +
       " Default Page</a>\n ");

   //  Log the exception and notify system operators
  ExceptionUtility.LogException(exc,  " DefaultPage ");
  ExceptionUtility.NotifySystemOps(exc);

   //  Clear the error from the server
  Server.ClearError();
}

 

处理页面级的错误:

private  void Page_Error( object sender, EventArgs e)
{
   //  Get last error from the server
  Exception exc = Server.GetLastError();

   //  Handle exceptions generated by Button 1
   if (exc  is InvalidOperationException)
  {
     //  Pass the error on to the Generic Error page
    Server.Transfer( " GenericErrorPage.aspx "true);
  }

   //  Handle exceptions generated by Button 2
   else  if (exc  is ArgumentOutOfRangeException)
  {
     //  Give the user some information, but
    
//  stay on the default page
    Response.Write( " <h2>Default Page Error</h2>\n ");
    Response.Write( " <p>Provide as much information here as is  " +
       " appropriate to show to the client.</p>\n ");
    Response.Write( " Return to the <a href='Default.aspx'> " +
         " Default Page</a>\n ");

     //  Log the exception and notify system operators
    ExceptionUtility.LogException(exc,  " DefaultPage ");
    ExceptionUtility.NotifySystemOps(exc);

     //  Clear the error from the server
    Server.ClearError();
  }
   else
  {
     //  Pass the error on to the default global handler
  }
}

 

Session Status的读与写

string firstName =  " Jeff ";
string lastName =  " Smith ";
string city =  " Seattle ";
Session[ " FirstName "] = firstName;
Session[ " LastName "] = lastName;
Session[ " City "] = city;

 

string firstName = ( string)(Session[ " First "]);
string lastName = ( string)(Session[ " Last "]);
string city = ( string)(Session[ " City "]);


参考System.Web命名空间:http://msdn.microsoft.com/zh-cn/library/w7f1y429 

 

类                     说明

HttpApplication 定义 ASP.NET 应用程序中的所有应用程序对象共有的方法、属性和事件。 此类是用户在 Global.asax 文件中所定义的应用程序的基类。
HttpCookie 提供创建和操作各 HTTP Cookie 的类型安全方法。
HttpRequest 使 ASP.NET 能够读取客户端在 Web 请求期间发送的 HTTP 值。
HttpResponse 封装来自 ASP.NET 操作的 HTTP 响应信息。
HttpServerUtility 提供用于处理 Web 请求的 Helper 方法。
HttpSessionStateBase 用作一些类的基类,可提供对会话状态值、会话级别设置以及生存期管理方法的访问。
TraceContext 捕获并提供有关 Web 请求的执行详细信息。 此类不能被继承。

 

 

 

五、Page类的继承模型

 

 

 

六、ASP.NET Web页面中的客户端脚本

当脚本依赖于运行时才能获取的信息时,采用动态创建客户端脚本。动态创建客户端脚本:

void Page_Load( object sender, EventArgs e)
{
     if(checkDisplayCount.Checked)
    {
        String scriptText =  "";
        scriptText +=  " function DisplayCharCount(){ ";
        scriptText +=  "    spanCounter.innerText =  " + 
             "  document.forms[0].TextBox1.value.length ";
        scriptText +=  " } ";
        ClientScriptManager.RegisterClientScriptBlock( this.GetType(), 
            " CounterScript ", scriptText,  true);
        TextBox1.Attributes.Add( " onkeyup "" DisplayCharCount() ");
        LiteralControl spanLiteral =  new 
            LiteralControl( " <span id=\"spanCounter\"></span> ");
        PlaceHolder1.Controls.Add(spanLiteral);
    }
}

 

protected  void Page_Load(Object sender, EventArgs e)
{
    String scriptText = 
         " return confirm('Do you want to submit the page?') ";
    ClientScript.RegisterOnSubmitStatement( this.GetType(), 
         " ConfirmSubmit ", scriptText);
}

 

 

七、给服务器控件添加客户端脚本

<%@ Page Language= " C# " %>
<html>
<head runat= " server ">
  <title>Untitled Page</title>
  <script type= " text/javascript ">
       var previousColor;
      function MakeRed()
      {
          previousColor = window. event.srcElement.style.color;
          window. event.srcElement.style.color =  " #FF0000 ";
      }
      function RestoreColor()
      {
          window. event.srcElement.style.color = previousColor;
      }
  </script>
</head>
<body>
  <form id= " form1 " runat= " server ">
  <asp:button id= " Button1 " runat= " server " 
    text= " Button1 " 
      οnmοuseοver= " MakeRed(); " 
      οnmοuseοut= " RestoreColor(); " />
  </form>
</body>
</html>

 

protected  void Page_Load( object sender, EventArgs e)
{
    String displayControlName =  " spanCounter ";
    TextBox1.Attributes.Add( " onkeyup ", displayControlName + 
         " .innerText=this.value.length; ");
}

 

<%@ Page Language= " C# " %>
<script runat= " server ">
     protected  void Button1_Click(Object sender, EventArgs e)
    {
        Label1.Text =  " Server click handler called. ";
    }
</script>

<html xmlns= " http://www.w3.org/1999/xhtml " >
<body>
  <form id= " form1 " runat= " server ">
    <asp:Button ID= " Button1 " Runat= " server " 
      OnClick= " Button1_Click " 
        OnClientClick= " return confirm('Ready to submit.'); " 
        Text= " Test Client Click " />
    <br />
    <asp:Label ID= " Label1 " Runat= " server " text= "" />
  </form>
</body>
</html>

 

 

 

八、ASP.NET控件

 

ASP.NET 用户控件


 

一、 ASP.NET用户控件概述

 

有时可能需要控件中具有内置 ASP.NET Web 服务器控件未提供的功能。 在这种情况下,您可以创建自己的控件。 有两个选择。 您可以创建:

  • 用户控件。 用户控件是能够在其中放置标记和 Web 服务器控件的容器。 然后,可以将用户控件作为一个单元对待,为其定义属性和方法。

  • 自定义控件。 自定义控件是编写的一个类,此类从 Control 或 WebControl 派生。

创建用户控件要比创建自定义控件方便很多,因为可以重用现有的控件。

 

ASP.NET Web 用户控件与完整的 ASP.NET 网页(.aspx 文件)相似,同时具有用户界面页和代码。 可以采取与创建 ASP.NET 页相似的方式创建用户控件,然后向其中添加所需的标记和子控件。 用户控件可以像页面一样包含对其内容进行操作(包括执行数据绑定等任务)的代码。

用户控件与 ASP.NET 网页有以下区别:

  • 用户控件的文件扩展名为 .ascx。

  • 用户控件中没有 @ Page 指令,而是包含 @ Control 指令,该指令对配置及其他属性进行定义。

  • 用户控件不能作为独立文件运行。 而必须像处理任何控件一样,将它们添加到 ASP.NET 页中。

  • 用户控件中没有 htmlbody 或 form 元素。 这些元素必须位于宿主页中。

可以在用户控件上使用与在 ASP.NET 网页上所用相同的 HTML 元素(htmlbody 或 form 元素除外)和 Web 控件。 例如,如果您要创建一个将用作工具栏的用户控件,则可以将一系列Button Web 服务器控件放在该控件上,并创建这些按钮的事件处理程序。 

 

 

二、 如何:将 Web 窗体页转换为 ASP.NET 用户控件

 

将单文件 ASP.NET 网页转换为用户控件

  1. 重命名控件使其文件扩展名为 .ascx。

  2. 从该页面中移除 htmlbody 和 form 元素。

  3. 将 @ Page 指令更改为 @ Control 指令。

  4. @ Control 指令中除 LanguageAutoEventWireup(如果存在)、CodeFile 和 Inherits 之外的所有特性。

  5. 在 @ Control 指令中包含 className 特性。 这允许将用户控件添加到页面时对其进行强类型化。

将代码隐藏 ASP.NET 网页转换为用户控件

  1. 重命名 .aspx 文件,使其文件扩展名为 .ascx。

  2. 根据代码隐藏文件使用的编程语言,重命名代码隐藏文件使其文件扩展名为 .ascx.vb 或 .ascx.cs。

  3. 打开代码隐藏文件并将该文件继承的类从 Page 更改为 UserControl

  4. 在 .aspx 文件中,执行以下操作:

    1. 从该页面中移除 htmlbody 和 form 元素。

    2. 将 @ Page 指令更改为 @ Control 指令。

    3. @ Control 指令中除 LanguageAutoEventWireup(如果存在)、CodeFile 和 Inherits 之外的所有特性。

    4. 在 @ Control 指令中,将 CodeFile 特性更改为指向重命名的代码隐藏文件。

  5. 在 @ Control 指令中包含 className 特性。 这允许将用户控件添加到页面时对其进行强类型化。

 

 

<%@ Page Language= " C# " %>
<html>
<script runat=server>
void EnterBtn_Click(Object sender, EventArgs e)
{
    Label1.Text =  " Hi  " + Name.Text +  "  welcome to ASP.NET! ";
}
</script>
<body>
<h3> <u>Web Forms Page</u> </h3>
<form>
Enter Name: <asp:textbox id= " Name " runat=server/>
<asp:button Text= " Enter " OnClick= " EnterBtn_Click "
    runat=server/>
<br>
<br>
<asp:label id= " Label1 " runat=server/>
</form>
</body>
</html>

 

 

 

<%@ Control Language= " C# " ClassName= " SampleUserControl " %>
<h3> <u>User Control</u> </h3>
<script runat=server>
void EnterBtn_Click(Object Sender, EventArgs e)
{
    Label1.Text =  " Hi  " + Name.Text +  "  welcome to ASP.NET! ";
}
</script>
Enter Name: <asp:textbox id= " Name " runat=server/>
<asp:button Text= " Enter " OnClick= " EnterBtn_Click "
runat=server/>
<br>
<br>
<asp:label id= " Label1 " runat=server/>

 

 

、 ASP.NET网页中包含用户控件

 

  1. 在包含 ASP.NET 网页中,创建一个 @ Register 指令,在其中包括:

    • 一个 TagPrefix 特性,该特性将前缀与用户控件相关联。 此前缀将包括在用户控件元素的开始标记中。

    • 一个 TagName 特性,该特性将名称与用户控件相关联。 此名称将包括在用户控件元素的开始标记中。

    • 一个 Src 特性,该特性定义包括的用户控件文件的虚拟路径。

      注意注意

      Src 特性值既可以是相对路径,也可以是从应用程序的根目录到用户控件源文件的绝对路径。 为灵活使用,建议使用相对路径。 代字号 (~) 表示应用程序的根目录。 用户控件不能位于 App_Code 目录中。

  2. 在网页主体中,请在 form 元素内部声明用户控件元素。

  3. (可选)如果用户控件公开公共属性,请以声明方式设置这些属性。

 

<%@ Page Language= " C# " %>
<%@ Register TagPrefix= " uc " TagName= " Spinner " 
    Src= " ~/Controls/Spinner.ascx " %>
<html>
<body>
<form runat= " server ">
    <uc:Spinner id= " Spinner1 " 
        runat= " server " 
        MinValue= " 1 " 
        MaxValue= " 10 " />
</form>
</body>

 

 

以编程方式创建ASP.NET用户控件的实例

 

  1. 在用户控件中,请确保 @ Control 指令包含将类分配给用户控件的 ClassName 特性。

    下面的示例对 ClassName 特性进行设置以强类型化用户控件。

    <%@ Control className="MyUserControl" %> 

     

  2. 在要使用该用户控件的页中,通过 @ Reference 指令创建对该用户控件的引用。

    当以编程方式创建用户控件时,只有创建了对该控件的引用之后,ASP.NET 网页才能使用该用户控件的强类型。 例如,下面的代码创建对 MyUserControl.ascx 文件中所创建用户控件的引用。

    <%@ Reference Control="MyUserControl.ascx" %> 

     

    注意注意

    打算以编程方式加载控件时,请使用 @ Reference 打算以声明方式向页添加用户控件时,请使用 @ Register 指令。 有关详细信息,请参见 如何:在 ASP.NET 网页中包括用户控件

  3. 使用用户控件的类名创建该控件的实例变量。 该类将成为 ASP 命名空间的一部分。

    例如,如果希望创建声明为 Spinner 类的用户控件的实例,请使用如下所示的语法:

    Protected ASP.Spinner Spinner1; 

     

  4. 通过在代码中调用 LoadControl 方法创建用户控件的实例。

  5. 根据需要分配属性值,然后向页上容器的 ControlCollection 集合添加控件,如 PlaceHolder 控件。

    注意注意

    当使用 Add 方法向 ControlCollection 对象添加控件时,这些控件将按被处理的顺序放置在集合中。 如果希望将控件添加到集合中的特定位置,请使用 AddAt 方法并指定要在其中存储该控件的索引位置。

 

 

<%@ Page Language= " C# " %>
<%@ Reference Control= " ~/Controls/Spinner.ascx " %>    
<script runat= " server ">
private ASP.Spinner Spinner1

protected  void Page_Load( object sender, EventArgs e)
{
   Spinner1 = (ASP.Spinner)LoadControl( " ~/Controls/Spinner.ascx ");
}

protected  void Button1_Click( object sender, EventArgs e)
{
    PlaceHolder1.Controls.Add(Spinner1);
}
</script>

<html>
<head id= " Head1 " runat= " server ">
  <title>Load User Control Programmatically</title>
</head>
<body>
  <form id= " form1 " runat= " server ">
    <div>
      <asp:PlaceHolder runat= " server " ID= " PlaceHolder1 " />
      <br />
      <asp:Button ID= " Button1 " runat= " server " 
        Text= " Click to Add User Control " 
        OnClick= " Button1_Click " />
      <br />
      <br />
      <asp:Label ID= " Label1 " runat= " server " Text= ""></asp:Label>
    </div>
  </form>
</body>
</html>

 


 

http://msdn.microsoft.com/zh-cn/library/bb386451 

 

 

九、数据绑定

 

数据绑定概要

<%# %> 语法
ASP.NET 引入了一种新的声明语法 <%# %>。该语法是在 .aspx 页中使用数据绑定的基础。所有数据绑定表达式都必须包含在这些字符中。下面的列表包含从多个源进行简单数据绑定的示例:
  • 简单属性(用于客户的语法):
    <%# custID %> 					
  • 集合(用于订单的语法):
    <asp:ListBox id="List1" datasource='<%# myArray %>' runat="server"> 					
  • 表达式(用于联系人的语法):
    <%# ( customer.First Name + " " + customer.LastName ) %> 					
  • 方法结果(用于未结清余额的语法):
    <%# GetBalance(custID) %> 					
在前面的示例中,<%# %> 内联标记用于指示将把特定数据源中的信息放在 .aspx 页中的什么位置。以下数据绑定示例使用  TextBox  Web 服务器控件:
<asp:textbox id=txt text="<%# custID %>" runat=server /> 				

 

 

Page.DataBind() 与 Control.DataBind()

为 .aspx 页上的对象确定并设置了特定数据源后,必须将数据绑定到这些数据源。您可以使用 Page.DataBind  或  Control.DataBind  方法将数据绑定到数据源。

这两种方法的使用方式很相似。主要差别在于:调用  Page.DataBind  方法后,所有数据源都将绑定到它们的服务器控件。在显式调用 Web 服务器控件的  DataBind  方法或在调用页面级的 Page.DataBind  方法之前,不会有任何数据呈现给控件。通常,可以从  Page_Load  事件调用 Page.DataBind (或  DataBind )。

 

 

数据绑定列表控件

列表控件是可以绑定到集合的特殊的 Web 服务器控件。您可以使用这些控件以自定义的模板格式显示数据行。所有列表控件都公开  DataSource  和  DataMember  属性,这些属性用于绑定到集合。

这些控件可以将其  DataSource  属性绑定到支持  IEnumerable ICollection  或  IListSource  接口的任一集合。
Repeater 控件
Repeater  控件是模板化的数据绑定列表。 Repeater  控件是“无外观的”;即,它不具有任何内置布局或样式。因此,您必须在控件的模板中明确声明所有 HTML 布局标记、格式标记和样式标记。

以下代码示例向您演示了如何使用  Repeater  这一列表控件显示数据: 

 

 

<%@ Page language= " c# " %>
<%@ Import Namespace= " System.Data " %>
<%@ Import Namespace= " System.Data.SqlClient " %>

<script runat= " server ">
void Page_Load(Object sender, EventArgs e) 

   SqlConnection cnn =  new 
       SqlConnection( " server=(local);database=pubs;Integrated Security=SSPI "); 
   SqlDataAdapter da =  new SqlDataAdapter( " select * from authors ", cnn); 
   DataSet ds =  new DataSet(); 
   da.Fill(ds,  " authors "); 
   Repeater1.DataSource = ds.Tables[ " authors "];
   Repeater1.DataBind();
}
</script>
<html>
<body>
   <form id= " WebForm2 " method= " post " runat= " server ">
      <asp:Repeater id= " Repeater1 " runat= " server ">
         <ItemTemplate>
         <%# DataBinder.Eval(Container.DataItem, " au_id ") %><br>
         </ItemTemplate>
      </asp:Repeater>
   </form>
</body>
</html>

 

 

DataList 控件
DataList  类是一个多功能的、模板化的数据绑定列表。您可以通过修改模板来自定义此控件。与 Repeater  控件不同, DataList  支持定向呈现,并且可以根据需要在运行时呈现到 HTML 表中。 


DataGrid 控件
DataGrid  控件是一个多功能的、多列的数据绑定网格。要自定义  DataGrid  中各列的布局,您可以将列类型设置为“模板”,然后修改列的模板。 DataGrid  控件无需模板即可进行呈现,这使得该控件成了用于报告方案的理想控件。 DataGrid  还支持根据列和根据按钮列进行选择、编辑、删除、分页和排序。 

 

访问数据

本节介绍如何访问数据库中的数据以及如何将访问的数据绑定到列表控件。您可以使用  DataSet  或 DataReader  类从数据库中获取数据。
DataSet 类
DataSet  包含数据的完整表示形式,其中包括表结构、表之间的关系和数据的排序。 DataSet  类非常灵活,可以将数据库中任何种类的信息存储到扩展标记语言 (XML) 文件中。 DataSet  类是无状态的;即,您无需连接到服务器连接资源即可将这些类从客户端传递到服务器。以下代码演示了如何使用 DataSet  将数据绑定到控件:

 

SqlConnection cnn =  new SqlConnection( " server=(local);
                                       database=pubs;Integrated Security=SSPI " ); 
SqlDataAdapter da =  new SqlDataAdapter( " select * from authors ", cnn); 
DataSet ds =  new DataSet(); 
da.Fill(ds);
MyRepeater.DataSource = ds;
MyRepeater.DataBind(); 

 


DataReader 类

相反,如果您只需显示(而不更改)要呈现的数据,DataReader 类可能是更好的解决方法。例如,对于 DropDownList 控件,最好使用 DataReader,因为 DataReader 是只进数据游标。以下代码演示了如何使用 SqlDataReader 类将数据绑定到控件: 

 

SqlConnection cnn =  new SqlConnection( " server=(local);database=pubs;Integrated Security=SSPI " );
SqlCommand cmd =  new SqlCommand( " select * from authors ", cnn);
cnn.Open();
MyRepeater.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection);
MyRepeater.DataBind();

 

 


列表控件模板中的绑定

您可以使用列表控件中的模板来绑定和自定义数据源的各个记录。本节提供了三种可用于执行此操作的方法。
DataBinder.Eval 方法
当数据源处理从数据库返回的数据时,它可能包含很多份信息。您可以使用通用的  DataBinder.Eval 方法返回数据。在以下代码示例中,“au_id”字段是从容器对象的数据源中返回的:

<%# DataBinder.Eval(Container.DataItem,"au_id") %> 

显式转换
如果您需要更多控件,可使用显式转换。显式转换使用类型转换关键字。这些关键字充当函数,而由编译器生成内联代码。因此,执行速度要比通过调用函数稍快。下列代码示例使用显式转换: 

 

//  DataTable as the DataSource
<%# ((System.Data.DataRowView)Container.DataItem)[ " au_id "] %> 

//  DataReader as the DataSource
<%# ((System.Data.Common.DbDataRecord)Container.DataItem)[ " au_id "] %>

//  DataReader as the DataSource
<%# ((System.Data.Common.DbDataRecord)Container.DataItem)[ 0] %>

 

 请注意,前面的示例将 DataTableDataSet 的子集)或 DataReader 用作数据源。

ItemDataBound 事件
您还可以使用控件的  ItemDataBound  事件来绑定数据。当将某个项目的数据绑定到控件时,就会发生该事件。以下 HTML 代码示例使用  ItemTemplate  定义了一个  Repeater  控件:

 

<asp:repeater id=rptr runat=server>
   <itemtemplate>
      <asp:label id=lblAuthorID runat=server />
   </itemtemplate>
</asp:repeater>

 

 您的页中需要有下列方法:

 public void Page_Init(object sender, System.EventArgs e)

{
   rptr.ItemDataBound +=  new RepeaterItemEventHandler(rptr_OnItemDataBound);
}
public  void Page_Load( object sender, System.EventArgs e)
{
    //  TODO: Retrieve data from a database,
   
//  and bind the data to a list control.
}
public  void rptr_OnItemDataBound( object sender, RepeaterItemEventArgs e)
{
   System.Data.Common.DbDataRecord rec = (System.Data.Common.DbDataRecord)
                                          e.Item.DataItem;
    if(rec!= null// Make sure that you have the data.
   {
      Label l1 = (Label)e.Item.FindControl( " lblAuthorID ");
      l1.Text = rec[ " au_id "].ToString();
   }
}

 

Eval与DataBinder.Eval的区别http://www.cnblogs.com/waitingfor/articles/2220779.html

DataBinder类:http://msdn.microsoft.com/zh-cn/library/system.web.ui.databinder.aspx

Control.DataBind 方法:

 http://msdn.microsoft.com/en-us/library/w5e5992d.aspx

 http://msdn.microsoft.com/zh-cn/library/system.web.ui.control.databind


支持数据绑定的列表控件:

  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox


 

十、事件、事件句柄、事件参数 

public  partial  class AutoEventWireupExample : System.Web.UI.Page

     protected  void Page_Load( object sender, System.EventArgs e)
    {
        Response.Write( " Executing Page_Load ");
    }
     override  protected  void OnLoad(EventArgs e)
    {
         this.Load +=  new System.EventHandler( this.Page_Load);
    }
}

 

在页面的生命周期中,当服务器控制加载进Page对象时,Page对象会调用它的OnLoad方法,这时OnLoad方法会产生一个Load事件。

上述代码中,this.Load是事件,this指的是该页面,Load是事件名称。System.EventHandler直译是事件句柄,其实是C#中的代理,this.Page_Load是Load事件的真正处理程序。本质上三者是同一个东西,因为他们的返回值和参数是相同的,即签名是相同的。

AutoEventWireup:设为true时,ASP.NET页面事件会自动地与事件处理函数关联。每个事件,ASP.NET都会查找与“Page_事件名”这种模式相匹配的方法,如Page_Load,Page_Init。由于AutoEventWireup默认为true,所以实际代码可能是这样:

public  partial  class Items : System.Web.UI.Page 
{
         protected  void Page_Load( object sender, EventArgs e) {
             // get page header and title
            Page.Title = WebUtility.GetProductName(Request.QueryString[ " productId "]);
        }
    }

如果AutoEventWireup设为false了,那代码可能是这样:

public  partial  class Items : System.Web.UI.Page {

         protected  void My_Page_Load( object sender, EventArgs e) {
             // get page header and title
            Page.Title = WebUtility.GetProductName(Request.QueryString[ " productId "]);
        }
         // 重写Page类的OnLoad方法
         protected  override  void OnLoad(EventArgs e)
        {
             base.OnLoad(e);
             this.Load +=  new EventHandler(My_Page_Load); //显式的将事件句柄与事件进行绑定,此时事件句柄指向的方法名称不一定必须是Page_Load,而可以是其他名字。
        }
    }

 

具体请参考AutoEventWireup属性:http://msdn.microsoft.com/en-us/library/system.web.configuration.pagessection.autoeventwireup.aspx 

ASP.NET Web服务器控件事件模型:http://msdn.microsoft.com/en-us/library/y3bwdsh3.aspx

 

 

十一、服务器控件:怎样用一个事件句柄处理多个事件

<asp:Button ID= " Button1 " οnclick= " Button_Click " runat= " server " 
  Text= " Button1 " /> 
<br />
<asp:Button ID= " Button2 " οnclick= " Button_Click " runat= " server "
  Text= " Button2 " />

判断是哪个控制触发了事件,方法如下:

private  void Button_Click( object sender, System.EventArgs e)
{
    Button b = (Button) sender;
    Label1.Text = b.ID;
}

具体参考:http://msdn.microsoft.com/en-us/library/ycbsc785.aspx

 

 

十二、动态绑定事件句柄

如果你是在代码中动态创建的控件,则必须用以下方法给控件动态地绑定事件句柄。因为编译阶段,编译器没有指向控件的引用。

Button b =  new Button;
b.Text =  " Click ";
b.Click +=  new System.EventHandler(ButtonClick);
Placeholder1.Controls.Add(b);

 

Button1.Click +=  new System.EventHandler( this.myEventHandler);


 

十三、规划ASP.NET网站

 

下列主题涉及与网站整体有关的信息,包括以下几个方面: 
1、所选网站的类型。
 
默认的网站项目模型使用文件目录结构来定义项目的内容。 在此模型中不存在项目文件,目录中的所有文件都是项目的组成部分。 
与此相反,在 Web 应用程序项目中,只有在解决方案的项目文件中显式引用的文件才是项目的组成部分。 这些文件显示在“解决方案资源管理器”中,它们是生成过程中唯一进行编译的文件。
2、如何在网站中导航。 
随着网站规模的增大以及在网站中不断移动网页,管理所有链接的工作可能会迅速变得非常困难。 ASP.NET 站点导航包含一系列服务器控件和类,可用于为用户提供一致的站点导航方式。 您可以将所有页面的链接都存储在一个中心位置(通常是一个 XML 文件)。 通过添加 SiteMapDataSource 控件来读取网站信息,可以在每个页面的列表或导航菜单中呈现这些链接。 随后,可以使用导航服务器控件(例如 TreeView 或 Menu 控件)来显示网站信息。 
ASP.NET 站点导航的关键部分是网站地图提供程序。 这是一个结合网站地图数据源公开导航信息的类。 例如,默认的 ASP.NET 网站地图提供程序会从名为 Web.sitemap 的 XML 文件中获取网站地图数据,并将该数据直接传递给 SiteMapPath Web 服务器控件。
3、如何为网站中的所有页面创建一致的外观和布局。 
ASP.NET 的一些功能有助于为网站创建并维护一致的外观和设计,例如 ASP.NET 主题和 ASP.NET 母版页。 在网站开发过程的早期,可以使用这些功能为网站提供一致的外观。 
ASP.NET 主题定义了网站中页面和控件的外观。 ASP.NET 主题可以包含定义 ASP.NET Web 服务器控件的属性设置的外观文件,也可以包含级联样式表文件(.css 文件)和图形。 通过应用主题,可以为网站中的页面提供一致的外观。 当在开发过程的早期为网站创建了一个或一组主题后,便可以将这些主题应用于每个新创建的页面。 有关主题的更多信息,请参见 ASP.NET 主题和外观。 
使用 ASP.NET 母版页,可以创建能应用于网站中选定页面(内容页)的页面布局(母版页)。 创建具有一致外观的网站时,使用母版页可大大地简化任务。 您也可以嵌套母版页。 例如,通过嵌套的母版页可以创建两个母板布局,一个用于网站整体,另一个用于网站的各个部分。 此外,还可以将母版页与主题配合使用。 有关母版页的更多信息,请参见 ASP.NET 母版页和嵌套的 ASP.NET 母版页。 
4、如何访问数据存储区中的数据。
利用 ASP.NET 数据绑定,既可以将组件绑定到源,也可以将组件绑定到简单的属性、集合、表达式和方法。 这在要使用数据库或其他源中的数据时,可提供更大的灵活性。 
如果网站要访问数据存储区,则应考虑使用数据源控件,因为它们是通用数据模式的一部分。 此数据模式可分离构成网站表示层的网页中的数据访问代码和业务逻辑代码。 数据访问层由用于访问数据存储区的方法组成。 业务逻辑层用于向数据访问层添加规则,例如对可以查看或更改数据的人员进行访问限制。 表示层由用户有权查看和修改数据的页面组成。 有关如何创建这些层的更多信息,请参见演练:在 ASP.NET 中创建数据访问和业务逻辑层。 
通过使用 ASP.NET 中的数据源模型将表示层与数据层和业务逻辑层分开,可以实现此通用数据模式。 借助 LinqDataSource、ObjectDataSource 和 SqlDataSource 等控件,可以创建与表示层分离的数据访问层和业务逻辑层。 
此外,还应考虑网站是否必须使用内存中(缓存)的数据。如果大部分应用程序数据并不会频繁更改,且能够在会话或用户之间通用,则可以将数据保留在 Web 服务器上的内存中。 这样可减少向数据库发出的请求数,从而提高用户交互的速度。 内存中的数据库用 DataSet 类创建。 DataSet 对象还有另一个用处,它允许应用程序将数据的子集从一个或多个数据源移至应用程序空间中。 这样,应用程序既能处理内存中的数据,又能保留其关系形状。
5、使用状态管理功能。 
HTTP 是一种无状态协议。 每个请求在到达后即被处理;当请求得到处理后,将丢弃所有数据。 即使是来自同一客户端的请求,也不保留它们的状态。 但是,对于大多数 Web 应用程序而言,保留请求间的状态十分有用。 
ASP.NET 提供了内部状态管理功能,它使您能够存储页请求期间的信息,例如客户信息或购物车的内容。 您可以保存和管理应用程序特定、会话特定、页特定、用户特定和开发人员定义的信息。 ASP.NET 具有多种状态管理形式可供选择,包括使用 cookie、视图状态、会话状态、应用程序状态和配置文件属性。 此信息可以独立于页上的任何控件。 
在规划网站时,应考虑需要使用哪种状态管理形式。
6、安全基础结构。 
除了 .NET Framework 的安全功能外,ASP.NET 还提供了一个安全基础结构,可用于对用户访问进行身份验证和授权,并执行其他与安全相关的任务。 通过 IIS 提供的 Windows 身份验证,可以验证用户的身份。 您也可以使用 ASP.NET Forms 身份验证和 ASP.NET 成员资格来管理身份验证。 此外,通过 Windows 组或使用自定义角色数据库和 ASP.NET 角色,还可以管理对 Web 应用程序资源的访问授权。 您可以根据应用程序的需要方便地移除、添加或替换这些方案。
7、生成各个页和重新编译。 
生成页或网站是开发站点的一部分,旨在帮助您发现站点中随处可能发生的编译时错误。 尽管生成过程会编译页面,但是它不会产生可部署的程序集。 
通过将网站中的所有文件都复制到成品服务器中,无需编译即可部署网站。 当用户从成品服务器请求页时,ASP.NET 将动态编译网站,并有效地执行与 Visual Studio 中的生成过程相同的步骤 (ASP.NET 会缓存得到的输出,这样就不必在每次请求时都重新编译页)。 
如果要将站点编译为可部署的程序集和其他文件,您可以发布站点。 发布执行与生成相同的编译步骤,但是它将输出保存到一个文件夹及其子文件夹中,稍后可将该文件夹及其子文件夹部署到成品服务器中。

 


十四、在 Visual Studio 中创建基本网页

 

 


 

 

十五、知识点参考

ASP.NET参考手册:http://www.w3school.com.cn/aspnet/aspnet_reference.asp

ASP.NET页面对象模型:http://msdn.microsoft.com/zh-cn/library/aa479007.aspx

ASP.NET参考:http://msdn.microsoft.com/zh-cn/library/9k6k3k4a(v=vs.80)

详解IIS是如何处理ASP.NET请求的:http://www.csharpwin.com/dotnetspace/13170r2383.shtml

A low-level Look at the ASP.NET Architecture:http://www.west-wind.com/presentations/howaspnetworks/howaspnetworks.asp

Introduction to IIS Architecture:http://learn.iis.net/page.aspx/101/introduction-to-iis-architecture/

Beginner’s Guide: How IIS Process ASP.NET Request:http://abhijitjana.net/2010/03/14/beginnerâÃÂÃÂs-guide-how-iis-process-asp-net-request/ 


ASP.NET Web Form Model with Partial Rendering and Events:http://www.codeproject.com/Articles/73181/ASP-NET-Web-Form-Model-with-Partial-Rendering-and 
 

Exploring ASP.NET Validators:http://www.codeproject.com/Articles/67269/Exploring-ASP-NET-Validators

CodeProject:http://www.codeproject.com/

CodeProject:http://www.codeproject.com/KB/aspnet/#General 

MSDN 开发人员示例:http://code.msdn.microsoft.com/

MSDN 学习资料:http://msdn.microsoft.com/zh-cn/bb188199.aspx


Delegates Tutorial:http://msdn.microsoft.com/en-us/library/aa288459(v=vs.71)

Events Tutorial:http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

Delegates and Events in C# / .NET:http://www.akadia.com/services/dotnet_delegates_and_events.html

 

ASP.NET Page Life Cycle Overview:http://msdn.microsoft.com/en-us/library/ms178472.aspx

ASP.NET Life Cycle:http://msdn.microsoft.com/en-us/library/ms227435

Akadias Publications:http://www.akadia.com/html/publications.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值