我想在c#中學習委託概念。代表問題
我在學習時做了一個委託樣本。但我沒有理解我們有效使用它的概念的正確情況。 任何人都可以提出一個容易理解的情況,我們使用委託。
我知道代表的工作。但仍未清除其有效使用的地方。
我在下面發佈我的代碼。如果我在樣本中發生了任何錯誤,請告訴我。
Thankx提前。
ChangePassword.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ChangePassword.ascx.cs" Inherits="User_Controls_ChangePassword" %>
<div style="width:500px;clear:both;">
<div style="width:100%; clear:both;">
<div style="width:150px; float:left;">
<asp:Label ID="newPassword" runat="server" Text="New Password"></asp:Label>
</div>
<div style="width:100px; float:left;">
<asp:TextBox ID="newPassText" runat="server" Width="200"></asp:TextBox>
</div>
</div>
<div style="width:100%; clear:both;padding-top:20px;">
<div style="width:150px; float:left;">
<asp:Label ID="Label1" runat="server" Text="Confirm New Password"></asp:Label>
</div>
<div style="width:100px; float:left;">
<asp:TextBox ID="confirmNewPass" runat="server" Width="200"></asp:TextBox>
</div>
</div>
<div style="width:100%; clear:both;padding-top:20px;">
<div style="width:150px; float:left;">
<asp:Label ID="Label2" runat="server" Text=" "></asp:Label>
</div>
<div style="width:207px; float:left;">
<div style="float:left;">
<asp:Button ID="changePass" runat="server" Text="Change" />
</div>
<div style="float:right;">
<asp:Button ID="cancelButton" runat="server" Text="Cancel" />
</div>
</div>
</div>
<div style="width:100%; clear:both;padding-top:20px;">
<div style="width:350px; float:left;">
<asp:Label ID="successMessage" runat="server" Text="Passwords Changed.." ForeColor="Red" Font-Bold="true" Visible="false"></asp:Label>
</div>
</div>
ChangePassword.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class User_Controls_ChangePassword : System.Web.UI.UserControl
{
public delegate void ChangePasswordDelegate(object sender, ChangePasswordEventArgs e);
public event ChangePasswordDelegate ChangePasswordEvent;
protected void Page_Load(object sender, EventArgs e)
{
changePass.Click += new EventHandler(changePass_Click);
}
void changePass_Click(object sender, EventArgs e)
{
ChangePasswordEvent(sender, new ChangePasswordEventArgs(newPassText.Text, this));
}
}
public class ChangePasswordEventArgs : EventArgs
{
private string _newPassword = "";
private object _parent = null;
public string NewPassword
{
get
{
return _newPassword;
}
set
{
_newPassword = value;
}
}
public object Parent
{
get
{
return _parent;
}
set
{
_parent = value;
}
}
public ChangePasswordEventArgs()
{ }
public ChangePasswordEventArgs(string pass , object parent)
{
_newPassword = pass;
_parent = parent;
}
}
Defau lt.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagName="ChangePasword" TagPrefix="MY" Src="~/User Controls/ChangePassword.ascx" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<MY:ChangePasword ID="passControl" runat="server" />
</asp:Content>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
passControl.ChangePasswordEvent += new User_Controls_ChangePassword.ChangePasswordDelegate(passControl_ChangePasswordEvent);
}
void passControl_ChangePasswordEvent(object sender, ChangePasswordEventArgs e)
{
if (e.Parent != null)
{
User_Controls_ChangePassword cp = (User_Controls_ChangePassword)e.Parent;
cp.FindControl("successMessage").Visible = true;
}
}
}
閱讀[本](http://www.codeproject.com/KB/cs/events.aspx) – Meysam 2012-02-03 11:24:33
謝謝。它是一個關於代表的好文章。 – Arun 2013-01-10 10:25:01