<%@ 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">
</form>
</body>
</HTML>
Code1
using
System;
using System.Web;
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事件里去,我们修改以后:
using System.Web;
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>");
}
}
}
Code2
using
System;
using System.Web;
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>");
}
protected override void OnInit(EventArgs e)
{
this.Load += new EventHandler(Page_Load);
base.OnInit(e);
}
}
}
using System.Web;
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>");
}
protected override void OnInit(EventArgs e)
{
this.Load += new EventHandler(Page_Load);
base.OnInit(e);
}
}
}
这时候就可以正常运作了。从这里我们注意到我们重写了OnInit方法,那么我们很容易可以联想到,重写以下OnLoad虚方法,应该也能达到我们的目的:
Code3
using
System;
using System.Web;
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);
}
}
}
using System.Web;
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 >
然后代码改成:
< 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;
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>");
}
protected override void OnInit(EventArgs e)
{
Button1.ServerClick += new EventHandler(OnClick);
}
}
}
using System.Web;
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>");
}
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 >
代码文件:
< 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;
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发送了请求呢?我们可以查看页面原代码:
using System.Web;
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>");
}
}
}
<
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= " />
< 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 >
< input language = " javascript " onclick = " __doPostBack('Button1','') " name = " Button1 " id = " Button1 " type = " button " value = " Click " />
</ form >
</ body >
</ HTML >
< 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= " />
< 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 >
< input language = " javascript " onclick = " __doPostBack('Button1','') " name = " Button1 " id = " Button1 " type = " button " value = " Click " />
</ form >
</ body >
</ HTML >
我们可以发现另外两个隐藏的字段_EVENTTARGET跟_EVENTTARGUMENT用于传送事件所需要的参数,在服务期端,ASP.NET会检查_EVENTTARGET的内容,激活匹配ID控件的事件。
参考:《Essential ASP.NET with Examples in C#》
PS:内容比较初级,如果大家觉得不适合放在首页,在回复中指出,我转到新手区。