手柄ContentPage母版事件 [英] Handle MasterPage event on ContentPage
问题描述
我有我的母版一个按钮,我想ContentPage以处理按钮的Click事件。这是我到目前为止有:
I have a button on my MasterPage and I want the ContentPage to handle the Click event of the button. Here's what I have so far:
//Event in MasterPage
public event EventHandler Search_Submit;
//ButtonClick event in MasterPage
protected void SearchButton_Click(object sender, EventArgs e)
{
if (Search_Submit != null)
{
Search_Submit(this, EventArgs.Empty);
}
}
//EventHandler in Page_Init on ContentPage
Master.Search_Submit += new EventHandler(Master_Search_Submit);
//Search_Submit event in ContentPage
private void Master_Search_Submit(object sender, EventArgs e)
{
//Perform appropriate action...
}
这所有的作品 - 不过,我想传递一个值与EventArgs的。我相信我需要创建一个自定义的EventArgs类继承于EventArgs并有我的自定义属性。然而,我当我这样做,我得到以下错误:对'Master_Search_Submit'匹配委托System.EventHandler不超载
This all works - however, I would like to pass a value with the EventArgs. I believe I need to create a custom EventArgs class that inherits from EventArgs and has my custom properties. However, my when I do that I get the following error: No overload for 'Master_Search_Submit' matches delegate 'System.EventHandler'.
推荐答案
您已经定义了你的活动,使用EventHandler委托。委托是指向一个功能,它定义了应对方法必须具有签名。为EventHandler委托的签名是:
You have defined your event to use the EventHandler delegate. A delegate is a pointer to a function, it defines the signature the responding method must have. The signature for the EventHandler delegate is:
delegate void EventHandler(object sender, EventArgs e);
在.NET 2.0有一个通用的EventHandler委托,允许您指定的EventArgs输入你想使用。
In .NET 2.0 there is a generic EventHandler delegate that allows you to specify the EventArgs type you would like to use.
public event EventHandler<MyEventArgs> Search_Submit;
现在你可以使用一个事件处理程序签名
Now you can use an event handler with the signature
void EventArgs<TArgs>(object sender, TArgs e) where TArgs: EventArgs
在你的情况,这将是:
void Master_Search_Submit(object sender, MyEventArgs e)
这篇关于手柄ContentPage母版事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!