标签:事件与委托
<1>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="事件学习.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" /> </div> </form> </body> </html>
<2>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace 事件学习 { //触发事件的对象称作发送者(sender),捕获事件并且做出响应的对象称作接收者(receiver),一个事件可以存在多个接受者 public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //Button1_Click是一个方法,Button1是Button类的一个对象,Click是Button类里面定义的一个事件 Button1.Click += new EventHandler(Button1_Click); //这里将Button1的Click事件注册一个Button1_Click处理方法。相当于onclick="Button1_Click" //Button1.Click += Button1_Click; //上面的语句也可以改写成这样 } protected void Button1_Click(object sender, EventArgs e) { //if (MyClick != null) //{ // MyClick(sender, e); //} Button1.Text = "我被点击了"; } } }
标签:事件与委托
原文地址:http://blog.csdn.net/fanbin168/article/details/39007307