行者

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

/Files/longren629/DelegateDemo.zip

1:操作类

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Demo 的摘要说明
/// </summary>

public class Demo
{
    
内部变量
    
构造函数
}


2:页面
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Delegate学习实例</title>
</head>
<body>
    
<form id="form1" runat="server">
      
<asp:Button ID="Button1" runat="server" Text="添加成都同时利用Delegate添加四川" OnClick="Button1_Click" />
        
<br />
        
<asp:Button ID="Button2" runat="server" Text="移除四川同时利用Delegate移除成都" OnClick="Button2_Click" /><br />
        
<asp:ListBox ID="ListBox1" runat="server" Width="330px" Height="328px"></asp:ListBox>
      
       
   
    
</form>
</body>
</html>

3,页面代码隐藏类
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        Demo bll 
= new Demo("四川""成都");
        bll.AddTown 
+= new Demo.AddTownDelegate(AddProvince);
        
this.ListBox1.Items.Add(bll.Town);
        bll.AddNewTown();   
    
    }

    
protected void AddProvince(string Province)
    
{
        
this.ListBox1.Items.Add(Province);
    }


    
protected void RemovedCity(string City)
    
{
        
this.ListBox1.Items.Remove(new ListItem(City));
    }


    
protected void Button2_Click(object sender, EventArgs e)
    
{
        Demo bll 
= new Demo("四川""成都");
        bll.RemoveProvince 
+= new Demo.RemoveProvinceDelegate(RemovedCity);
        
this.ListBox1.Items.Remove(new ListItem(bll.Province));
        bll.RemoveOldProvince();
    }

}

posted on 2007-06-07 17:45  汪洋怡舟  阅读(251)  评论(0编辑  收藏  举报