我们写一个简单的web页面
在CodeBehind1.cs中写
Code1
using System;
using System.Web;
![](/Images/OutliningIndicators/None.gif)
namespace XXin
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
Response.Write("<script>alert('hello')</script>");
}
}
}我们载入页面,会发现什么事情都没有发生。,那是因为事件处理方法没有通过委托注册到Page的Load事件里去,我们修改以后:
Code2
using System;
using System.Web;
![](/Images/OutliningIndicators/None.gif)
namespace XXin
{
/**/
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
Response.Write("<script>alert('hello')</script>");
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
protected override void OnInit(EventArgs e)
{
this.Load += new EventHandler(Page_Load);
base.OnInit(e);
![](/Images/OutliningIndicators/InBlock.gif)
}
}
}
这时候就可以正常运作了。从这里我们注意到我们重写了OnInit方法,那么我们很容易可以联想到,重写以下OnLoad虚方法,应该也能达到我们的目的:
Code3
using System;
using System.Web;
![](/Images/OutliningIndicators/None.gif)
namespace XXin
{
/**/
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
Response.Write("<script>alert('hello')</script>");
base.OnLoad(e);
}
}
}
那么那种方法好呢, Dflying Chen 给出了他的回答
在ASP.NET页面中推荐使用覆写(Override)而不是事件处理(Event Handler)
那如果我们想偷个小懒,不想自己显式的自己绑定(wire up)事件处理方法,可不可以实现呢,答案是可以的,只要:
AutoEventWireup="true"
那么我们Code1中的代码也能正常运作了。
对于Html Control我们改写一下aspx页面:
<%@ Page language="c#" src="CodeBehind1.cs" AutoEventWireup="false" Inherits="XXin.WebForm" %>
<HTML>
<HEAD>
<title>XXin's WebForm</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<input type="button" value="Click" id="Button1" runat="server" />
</form>
</body>
</HTML>然后代码改成:
using System;
using System.Web;
![](/Images/OutliningIndicators/None.gif)
namespace XXin
{
public class WebForm : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputButton Button1;
protected void OnClick(object sender,EventArgs e)
{
Response.Write("<script>alert('You Have Clicked ME!')</script>");
}
![](/Images/OutliningIndicators/InBlock.gif)
protected override void OnInit(EventArgs e)
{
Button1.ServerClick += new EventHandler(OnClick);
}
}
}
这是在Page的OnInit方法里注册了事件委托,当然我们还可以这么写:
页面文件:
<%@ Page language="c#" src="CodeBehind1.cs" AutoEventWireup="false" Inherits="XXin.WebForm" %>
<HTML>
<HEAD>
<title>XXin's WebForm</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<input type="button" value="Click" id="Button1" onserverclick="OnClick" runat="server" />
</form>
</body>
</HTML>代码文件:
using System;
using System.Web;
![](/Images/OutliningIndicators/None.gif)
namespace XXin
{
public class WebForm : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputButton Button1;
protected void OnClick(object sender,EventArgs e)
{
Response.Write("<script>alert('You Have Clicked ME!')</script>");
}
}
} 这里我们需要注意到网页总是发送一个普通的post请求,如果有多个button,那么服务器又如何判断是哪个button发送了请求呢?我们可以查看页面原代码:
<script>alert('You Have Clicked ME!')</script>
<HTML>
<HEAD>
<title>XXin's WebForm</title>
</HEAD>
<body>
<form name="Form1" method="post" action="kk1.aspx" id="Form1">
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" value="dDwxOTI0MjI4NTEyOzs+2eWzZjKhFYZfiQCamngPwH+1sg8=" />
![](/Images/OutliningIndicators/None.gif)
<script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
</script>
![](/Images/OutliningIndicators/None.gif)
<input language="javascript" onclick="__doPostBack('Button1','')" name="Button1" id="Button1" type="button" value="Click" />
</form>
</body>
</HTML>
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code1
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
Code2
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这时候就可以正常运作了。从这里我们注意到我们重写了OnInit方法,那么我们很容易可以联想到,重写以下OnLoad虚方法,应该也能达到我们的目的:
Code3
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
那么那种方法好呢, Dflying Chen 给出了他的回答
在ASP.NET页面中推荐使用覆写(Override)而不是事件处理(Event Handler)
那如果我们想偷个小懒,不想自己显式的自己绑定(wire up)事件处理方法,可不可以实现呢,答案是可以的,只要:
AutoEventWireup="true"
那么我们Code1中的代码也能正常运作了。
对于Html Control我们改写一下aspx页面:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这是在Page的OnInit方法里注册了事件委托,当然我们还可以这么写:
页面文件:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
我们可以发现另外两个隐藏的字段_EVENTTARGET跟_EVENTTARGUMENT用于传送事件所需要的参数,在服务期端,ASP.NET会检查_EVENTTARGET的内容,激活匹配ID控件的事件。
参考:《Essential ASP.NET with Examples in C#》
PS:内容比较初级,如果大家觉得不适合放在首页,在回复中指出,我转到新手区。