C# 自定义事件始终为空
C# Custom event is always null
我知道现在在 Whosebug 上有一些关于这个问题的问题,但是 none 解决了我的问题。
我使用 "asp.net webform",在我的 UserControl
中,我希望当我点击 Button
时,在使用此 UserControl
的页面中触发一个事件。所以这是我的代码。
// in user control
public delegate void OnConversationSubmitDelegate(object sender, EventArgs e);
public event OnConversationSubmitDelegate OnConversationSubmitEvenet;
protected void btnUserSubmit_Click(object sender, EventArgs e)
{
if (OnConversationSubmitEvenet != null) //This is always null
{
OnConversationSubmitEvenet(this, new EventArgs());
}
}
// in main page
protected void Page_Load(object sender, EventArgs e)
{
UserControls.ConversationBox m = new UserControls.ConversationBox();
m.OnConversationSubmitEvenet += new UserControls.ConversationBox.OnConversationSubmitDelegate(Test_Event);
}
public static void Test_Event(object sender, EventArgs e)
{
string g = "sdfsd";
}
问题是 OnConversationSubmitEvenet
始终为 null 而 Test_Event
方法永远不会 运行。
您应该在表单中将 'm' 声明为字段,因为现在 'm' 是局部变量,在 'Load' 方法执行后消失。
您似乎正在 Page_Load 中创建一个新的 ConversationBox
,但我怀疑由于您尚未将其添加到页面控件中,所以您还在aspx?
如果您确实在 aspx 中添加了一个 ConversationBox,那么不要这样:
UserControls.ConversationBox m = new UserControls.ConversationBox();
m.OnConversationSubmitEvenet += new UserControls.ConversationBox.OnConversationSubmitDelegate(Test_Event);
您应该使用您在 aspx 中添加的 ConversationBox:
myConversationBox.OnConversationSubmitEvenet += new UserControls.ConversationBox.OnConversationSubmitDelegate(Test_Event);
或者您可以删除在 aspx 中添加的那个,而是将 m 添加到页面控件:
UserControls.ConversationBox m = new UserControls.ConversationBox();
m.OnConversationSubmitEvenet += new UserControls.ConversationBox.OnConversationSubmitDelegate(Test_Event);
Page.Controls.Add(m); // This line adds the control to the page
我知道现在在 Whosebug 上有一些关于这个问题的问题,但是 none 解决了我的问题。
我使用 "asp.net webform",在我的 UserControl
中,我希望当我点击 Button
时,在使用此 UserControl
的页面中触发一个事件。所以这是我的代码。
// in user control
public delegate void OnConversationSubmitDelegate(object sender, EventArgs e);
public event OnConversationSubmitDelegate OnConversationSubmitEvenet;
protected void btnUserSubmit_Click(object sender, EventArgs e)
{
if (OnConversationSubmitEvenet != null) //This is always null
{
OnConversationSubmitEvenet(this, new EventArgs());
}
}
// in main page
protected void Page_Load(object sender, EventArgs e)
{
UserControls.ConversationBox m = new UserControls.ConversationBox();
m.OnConversationSubmitEvenet += new UserControls.ConversationBox.OnConversationSubmitDelegate(Test_Event);
}
public static void Test_Event(object sender, EventArgs e)
{
string g = "sdfsd";
}
问题是 OnConversationSubmitEvenet
始终为 null 而 Test_Event
方法永远不会 运行。
您应该在表单中将 'm' 声明为字段,因为现在 'm' 是局部变量,在 'Load' 方法执行后消失。
您似乎正在 Page_Load 中创建一个新的 ConversationBox
,但我怀疑由于您尚未将其添加到页面控件中,所以您还在aspx?
如果您确实在 aspx 中添加了一个 ConversationBox,那么不要这样:
UserControls.ConversationBox m = new UserControls.ConversationBox();
m.OnConversationSubmitEvenet += new UserControls.ConversationBox.OnConversationSubmitDelegate(Test_Event);
您应该使用您在 aspx 中添加的 ConversationBox:
myConversationBox.OnConversationSubmitEvenet += new UserControls.ConversationBox.OnConversationSubmitDelegate(Test_Event);
或者您可以删除在 aspx 中添加的那个,而是将 m 添加到页面控件:
UserControls.ConversationBox m = new UserControls.ConversationBox();
m.OnConversationSubmitEvenet += new UserControls.ConversationBox.OnConversationSubmitDelegate(Test_Event);
Page.Controls.Add(m); // This line adds the control to the page