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

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

博客园 - 小新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:内容比较初级,如果大家觉得不适合放在首页,在回复中指出,我转到新手区。