【发布时间】:2012-02-03 10:50:03
【问题描述】:
我正在尝试研究 c# 中的委托概念。
我在学习时做了一个代表样本。但是我不理解我们有效使用其概念的正确情况。 任何人都可以建议我们使用委托的简单易懂的情况。
我知道代表的工作。但仍然没有清除它有效使用的地方。
我在下面发布了我的代码。如果我在示例中犯了任何错误,请告诉我。
提前谢谢。
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;
}
}
Default.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;
}
}
}
【问题讨论】:
-
阅读this
-
谢谢。这是一篇关于代表的好文章。