惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 小新0574

PayPal使用介绍 - 注册篇 VB6.0 IDE即将退出历史舞台 工作一年半记录 随便说说Silverlight 最近在学英语 真正理解ViewState - Part2 真正理解ViewState - part1 本地模式使用ReportViewer控件 编程使用资源文件实现多语言页面(In Action) ADO.NET2.0跟ADO.NET3.0的一些新特性简要介绍 关于Membership的设置 本地生成RDL报表文件的创建工具 使用WebService动态生成DataSet绑定到Reporting Services 可爱的上海人 Just a Test 回到了学校 到了上海 打算近期去深圳找工作 第一天学习《Essential ASP.NET...》情况
ASP.NET中的事件
小新0574 · 2006-04-09 · via 博客园 - 小新0574

我们写一个简单的web页面

在CodeBehind1.cs中写
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事件里去,我们修改以后:
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);

        }

    }

}

这时候就可以正常运作了。从这里我们注意到我们重写了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);
        }

    }

}

那么那种方法好呢, 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;

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>

代码文件:

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发送了请求呢?我们可以查看页面原代码:

<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>

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

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

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