EventHandler, delegue different ?
Bonjour,
quelle est la difference entre ces 2 facon de faire :
Code:
1 2 3 4 5 6 7 8 9 10 11
|
ublic class OnLoGonEventArgs : EventArgs
{
public string _title;
public OnLoGonEventArgs(string title)
{
_title = title;
}
} |
EventHandler :
Dans un webusercontrol
Code:
1 2 3 4 5 6 7 8
|
public EventHandler<OnLoGonEventArgs> OnLogon;
protected void Button1_Click(object sender, EventArgs e)
{
if (OnLogon != null)
OnLogon(sender, new OnLoGonEventArgs(TextBox1.Text));
} |
Page aspx
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
protected void Page_Load(object sender, EventArgs e)
{
Uc1_1.OnLogon += OnLoGon;
}
private void OnLoGon(object sender, OnLoGonEventArgs e)
{
if (e._title != "")
{
Label1.Text = e._title;
}
} |
Delegue:
Dans un webusercontrol
Code:
1 2 3 4 5 6 7 8 9
|
public delegate void getInfo(object sender, OnLoGonEventArgs e);
public event getInfo getInfoEvent;
protected void Button1_Click(object sender, EventArgs e)
{
OnLoGonEventArgs args = new OnLoGonEventArgs(TextBox1.Text);
getInfoEvent(this, args);
} |
Page aspx
Code:
1 2 3 4 5 6 7 8 9 10
|
protected void Page_Load(object sender, EventArgs e)
{
Uc2_1.getInfoEvent += new uc2.getInfo(Uc2_1_getInfoEvent);
}
void Uc2_1_getInfoEvent(object sender, OnLoGonEventArgs e)
{
Label1.Text = e._title;
} |
Quelle est la difference ? cela donne le meme resultat.
Merci