ASP.NET中的事件

我们写一个简单的web页面
ContractedBlock.gif ExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page language="c#" src="CodeBehind1.cs" AutoEventWireup="false" Inherits="XXin.WebForm" %>
None.gif
<HTML>
None.gif    
<HEAD>
None.gif        
<title>XXin's WebForm</title>
None.gif    
</HEAD>
None.gif    
<body>
None.gif        
<form id="Form1" method="post" runat="server">
None.gif
None.gif        
</form>
None.gif    
</body>
None.gif
</HTML>
在CodeBehind1.cs中写
Code1
None.gif using  System;
None.gif
using  System.Web;
None.gif
None.gif
namespace  XXin
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(
"<script>alert('hello')</script>");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
我们载入页面,会发现什么事情都没有发生。,那是因为事件处理方法没有通过委托注册到Page的Load事件里去,我们修改以后:
Code2
None.gif using  System;
None.gif
using  System.Web;
None.gif
None.gif
namespace  XXin
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(
"<script>alert('hello')</script>");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
protected override void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif           
InBlock.gif            
this.Load += new EventHandler(Page_Load);
InBlock.gif            
base.OnInit(e);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

这时候就可以正常运作了。从这里我们注意到我们重写了OnInit方法,那么我们很容易可以联想到,重写以下OnLoad虚方法,应该也能达到我们的目的:
Code3
None.gif using  System;
None.gif
using  System.Web;
None.gif
None.gif
namespace  XXin
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>
InBlock.gif    public class WebForm : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected override void OnLoad(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(
"<script>alert('hello')</script>");
InBlock.gif            
base.OnLoad(e);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

那么那种方法好呢, Dflying Chen 给出了他的回答
在ASP.NET页面中推荐使用覆写(Override)而不是事件处理(Event Handler)

那如果我们想偷个小懒,不想自己显式的自己绑定(wire up)事件处理方法,可不可以实现呢,答案是可以的,只要:
AutoEventWireup="true"
那么我们 Code1中的代码也能正常运作了。

对于Html Control我们改写一下aspx页面:
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" src="CodeBehind1.cs" AutoEventWireup="false" Inherits="XXin.WebForm"  %>
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > XXin's WebForm </ title >
None.gif    
</ HEAD >
None.gif    
< body >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif        
< input  type ="button"  value ="Click"  id ="Button1"  runat ="server"   />
None.gif     
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
然后代码改成:
None.gif using  System;
None.gif
using  System.Web;
None.gif
None.gif
namespace  XXin
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
InBlock.gif    
public class WebForm : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.HtmlControls.HtmlInputButton Button1;
InBlock.gif        
protected void OnClick(object sender,EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(
"<script>alert('You Have Clicked ME!')</script>");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Button1.ServerClick 
+= new EventHandler(OnClick);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

这是在Page的OnInit方法里注册了事件委托,当然我们还可以这么写:
页面文件:
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" src="CodeBehind1.cs" AutoEventWireup="false" Inherits="XXin.WebForm"  %>
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > XXin's WebForm </ title >
None.gif    
</ HEAD >
None.gif    
< body >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif        
< input  type ="button"  value ="Click"  id ="Button1"   onserverclick ="OnClick"  runat ="server"   />
None.gif     
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
代码文件:
None.gif using  System;
None.gif
using  System.Web;
None.gif
None.gif
namespace  XXin
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
InBlock.gif    
public class WebForm : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.HtmlControls.HtmlInputButton Button1;
InBlock.gif        
protected void OnClick(object sender,EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(
"<script>alert('You Have Clicked ME!')</script>");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
 这里我们需要注意到网页总是发送一个普通的post请求,如果有多个button,那么服务器又如何判断是哪个button发送了请求呢?我们可以查看页面原代码:
 
None.gif < script > alert( ' You Have Clicked ME! ' ) </ script >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > XXin ' s WebForm</title>
None.gif
     </ HEAD >
None.gif    
< body >
None.gif        
< form name = " Form1 "  method = " post "  action = " kk1.aspx "  id = " Form1 " >
None.gif
< input type = " hidden "  name = " __EVENTTARGET "  value = ""   />
None.gif
< input type = " hidden "  name = " __EVENTARGUMENT "  value = ""   />
None.gif
< input type = " hidden "  name = " __VIEWSTATE "  value = " dDwxOTI0MjI4NTEyOzs+2eWzZjKhFYZfiQCamngPwH+1sg8= "   />
None.gif
None.gif
< script language = " javascript " >
None.gif
<!--
ExpandedBlockStart.gifContractedBlock.gif    function __doPostBack(eventTarget, eventArgument) 
dot.gif {
InBlock.gif        var theform;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (window.navigator.appName.toLowerCase().indexOf("netscape"> -1dot.gif{
InBlock.gif            theform 
= document.forms["Form1"];
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
else dot.gif{
InBlock.gif            theform 
= document.Form1;
ExpandedSubBlockEnd.gif        }

InBlock.gif        theform.__EVENTTARGET.value 
= eventTarget.split("$").join(":");
InBlock.gif        theform.__EVENTARGUMENT.value 
= eventArgument;
InBlock.gif        theform.submit();
ExpandedBlockEnd.gif    }

None.gif
//  -->
None.gif
</ script >
None.gif
None.gif        
< input language = " javascript "  onclick = " __doPostBack('Button1','') "  name = " Button1 "  id = " Button1 "  type = " button "  value = " Click "   />
None.gif     
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >

我们可以发现另外两个隐藏的字段_EVENTTARGET跟_EVENTTARGUMENT用于传送事件所需要的参数,在服务期端,ASP.NET会检查_EVENTTARGET的内容,激活匹配ID控件的事件。

 参考:《Essential ASP.NET with Examples in C#》

PS:内容比较初级,如果大家觉得不适合放在首页,在回复中指出,我转到新手区。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值