目 录
1、页面生命周期
一、页面生命周期
页面框架通过如下过程处理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
CodeFile = " Default.aspx.cs " Inherits = " _Default " %>
2、代码声明块
代码声明块定义嵌入 ASP.NET 应用程序文件内使用 runat="server" 属性标记的 script 块中的服务器代码节。
Code goes here...
</ script >
< 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 方法的快捷方式。
< font size ="<%=i %>" > Hello World! </ font >
<% } %>
4、服务器端注释
5、自定义服务器控件语法
自定义服务器控件语法用于将用户控件和自定义服务器控件声明为 ASP.NET 应用程序文件中的标记元素,包括网页、用户控件和母版页。此语法几乎与用于声明所有 ASP.NET 服务器控件的语法相同,其中的差别仅在于:对于自定义和用户控件,您通常会声明一个唯一的标记前缀和一个与您的控件相对应的标记名称。
attributename ="value"
eventname ="eventhandlermethod"
runat ="server" />
OR
runat ="server" />
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 方法时,数据绑定表达式创建服务器控件属性和数据源之间的绑定。可以将数据绑定表达式包含在服务器控件开始标记中属性/值对的值一侧,或页中的任何位置。
runat ="server" />
- or -
< 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 对象
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" />
< 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 文件。
< 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事件的处理代码。
<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文件,程序放在另一个文件。
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.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. | |
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. | |
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. | |
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. | |
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. | |
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. | |
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. |
从Application Status读值:
{
DateTime myAppStartTime = (DateTime)Application[ " AppStartTime "];
}
读写Cookie
{
string userSettings;
if (Request.Cookies[ " UserSettings "][ " Font "] != null)
{ userSettings = Request.Cookies[ " UserSettings "][ " Font "]; }
}
Response.Cookies[ " UserSettings "][ " Color "] = " Blue ";
Response.Cookies[ " UserSettings "].Expires = DateTime.Now.AddDays(1d);
myCookie[ " Font "] = " Arial ";
myCookie[ " Color "] = " Blue ";
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);
处理应用程序级的错误:
{
// 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();
}
处理页面级的错误:
{
// 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 lastName = " Smith ";
string city = " Seattle ";
Session[ " FirstName "] = firstName;
Session[ " LastName "] = lastName;
Session[ " City "] = city;
string lastName = ( string)(Session[ " Last "]);
string city = ( string)(Session[ " City "]);
参考System.Web命名空间:http://msdn.microsoft.com/zh-cn/library/w7f1y429
类 说明
五、Page类的继承模型
六、ASP.NET Web页面中的客户端脚本
当脚本依赖于运行时才能获取的信息时,采用动态创建客户端脚本。动态创建客户端脚本:
{
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);
}
}
{
String scriptText =
" return confirm('Do you want to submit the page?') ";
ClientScript.RegisterOnSubmitStatement( this.GetType(),
" ConfirmSubmit ", scriptText);
}
七、给服务器控件添加客户端脚本
<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>
{
String displayControlName = " spanCounter ";
TextBox1.Attributes.Add( " onkeyup ", displayControlName +
" .innerText=this.value.length; ");
}
<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 Web 服务器控件未提供的功能。 在这种情况下,您可以创建自己的控件。 有两个选择。 您可以创建:
-
用户控件。 用户控件是能够在其中放置标记和 Web 服务器控件的容器。 然后,可以将用户控件作为一个单元对待,为其定义属性和方法。
-
自定义控件。 自定义控件是编写的一个类,此类从 Control 或 WebControl 派生。
创建用户控件要比创建自定义控件方便很多,因为可以重用现有的控件。
ASP.NET Web 用户控件与完整的 ASP.NET 网页(.aspx 文件)相似,同时具有用户界面页和代码。 可以采取与创建 ASP.NET 页相似的方式创建用户控件,然后向其中添加所需的标记和子控件。 用户控件可以像页面一样包含对其内容进行操作(包括执行数据绑定等任务)的代码。
用户控件与 ASP.NET 网页有以下区别:
-
用户控件的文件扩展名为 .ascx。
-
用户控件不能作为独立文件运行。 而必须像处理任何控件一样,将它们添加到 ASP.NET 页中。
-
用户控件中没有 html、body 或 form 元素。 这些元素必须位于宿主页中。
可以在用户控件上使用与在 ASP.NET 网页上所用相同的 HTML 元素(html、body 或 form 元素除外)和 Web 控件。 例如,如果您要创建一个将用作工具栏的用户控件,则可以将一系列Button Web 服务器控件放在该控件上,并创建这些按钮的事件处理程序。
二、 如何:将 Web 窗体页转换为 ASP.NET 用户控件
将单文件 ASP.NET 网页转换为用户控件
将代码隐藏 ASP.NET 网页转换为用户控件
-
重命名 .aspx 文件,使其文件扩展名为 .ascx。
-
根据代码隐藏文件使用的编程语言,重命名代码隐藏文件使其文件扩展名为 .ascx.vb 或 .ascx.cs。
-
打开代码隐藏文件并将该文件继承的类从 Page 更改为 UserControl。
-
在 .aspx 文件中,执行以下操作:
-
在 @ Control 指令中包含 className 特性。 这允许将用户控件添加到页面时对其进行强类型化。
<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>
<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网页中包含用户控件
-
在包含 ASP.NET 网页中,创建一个 @ Register 指令,在其中包括:
-
一个 TagPrefix 特性,该特性将前缀与用户控件相关联。 此前缀将包括在用户控件元素的开始标记中。
-
一个 TagName 特性,该特性将名称与用户控件相关联。 此名称将包括在用户控件元素的开始标记中。
-
一个 Src 特性,该特性定义包括的用户控件文件的虚拟路径。
注意 Src 特性值既可以是相对路径,也可以是从应用程序的根目录到用户控件源文件的绝对路径。 为灵活使用,建议使用相对路径。 代字号 (~) 表示应用程序的根目录。 用户控件不能位于 App_Code 目录中。
-
-
在网页主体中,请在 form 元素内部声明用户控件元素。
-
(可选)如果用户控件公开公共属性,请以声明方式设置这些属性。
<%@ 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用户控件的实例
-
在用户控件中,请确保 @ Control 指令包含将类分配给用户控件的 ClassName 特性。
下面的示例对 ClassName 特性进行设置以强类型化用户控件。
<%@ Control className="MyUserControl" %>
-
在要使用该用户控件的页中,通过 @ Reference 指令创建对该用户控件的引用。
当以编程方式创建用户控件时,只有创建了对该控件的引用之后,ASP.NET 网页才能使用该用户控件的强类型。 例如,下面的代码创建对 MyUserControl.ascx 文件中所创建用户控件的引用。
<%@ Reference Control="MyUserControl.ascx" %>
注意 打算以编程方式加载控件时,请使用 @ Reference。 打算以声明方式向页添加用户控件时,请使用 @ Register 指令。 有关详细信息,请参见 如何:在 ASP.NET 网页中包括用户控件。
-
使用用户控件的类名创建该控件的实例变量。 该类将成为 ASP 命名空间的一部分。
例如,如果希望创建声明为 Spinner 类的用户控件的实例,请使用如下所示的语法:
Protected ASP.Spinner Spinner1;
-
通过在代码中调用 LoadControl 方法创建用户控件的实例。
-
根据需要分配属性值,然后向页上容器的 ControlCollection 集合添加控件,如 PlaceHolder 控件。
注意 当使用 Add 方法向 ControlCollection 对象添加控件时,这些控件将按被处理的顺序放置在集合中。 如果希望将控件添加到集合中的特定位置,请使用 AddAt 方法并指定要在其中存储该控件的索引位置。
<%@ 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) %>
<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 这一列表控件显示数据:
<%@ 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 将数据绑定到控件:
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 类将数据绑定到控件:
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") %>
显式转换
如果您需要更多控件,可使用显式转换。显式转换使用类型转换关键字。这些关键字充当函数,而由编译器生成内联代码。因此,执行速度要比通过调用函数稍快。下列代码示例使用显式转换:
<%# ((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] %>
请注意,前面的示例将 DataTable(DataSet 的子集)或 DataReader 用作数据源。
ItemDataBound 事件
您还可以使用控件的 ItemDataBound 事件来绑定数据。当将某个项目的数据绑定到控件时,就会发生该事件。以下 HTML 代码示例使用 ItemTemplate 定义了一个 Repeater 控件:
<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
十、事件、事件句柄、事件参数
{
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,所以实际代码可能是这样:
protected void Page_Load( object sender, EventArgs e) {
// get page header and title
Page.Title = WebUtility.GetProductName(Request.QueryString[ " productId "]);
}
}
如果AutoEventWireup设为false了,那代码可能是这样:
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
十一、服务器控件:怎样用一个事件句柄处理多个事件
Text= " Button1 " />
<br />
<asp:Button ID= " Button2 " οnclick= " Button_Click " runat= " server "
Text= " Button2 " />
判断是哪个控制触发了事件,方法如下:
{
Button b = (Button) sender;
Label1.Text = b.ID;
}
具体参考:http://msdn.microsoft.com/en-us/library/ycbsc785.aspx
十二、动态绑定事件句柄
如果你是在代码中动态创建的控件,则必须用以下方法给控件动态地绑定事件句柄。因为编译阶段,编译器没有指向控件的引用。
b.Text = " Click ";
b.Click += new System.EventHandler(ButtonClick);
Placeholder1.Controls.Add(b);
十三、规划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