Web User Control og Event
Posted
by Mcoroklo
on Stack Overflow
See other posts from Stack Overflow
or by Mcoroklo
Published on 2010-04-21T09:18:51Z
Indexed on
2010/04/21
9:23 UTC
Read the original article
Hit count: 284
I have a web user control, CreateQuestion.ascx, in ASP.NET. I want an event "QuestionAdded" to fire when a specific button is clicked. I don't want to send any data, I just want to know WHEN the button is fired.
My implementation:
CreateQuestion.ascx:
public event EventHandler QuestionAdded;
protected virtual void OnQuestionAdded(EventArgs e)
{
if (QuestionAdded != null)
QuestionAdded(this, e);
}
/// This is the button I want to know when is fired
protected void SubmitButton_Click(object sender, EventArgs e)
{
//question.Save();
this.OnQuestionAdded(new EventArgs());
}
On the page AnswerQuestion.aspx, I use this:
private void SetupControls(int myId)
{
CreateQuestionControl.QuestionAdded += new EventHandler(QuestionAdded);
}
private void QuestionAdded(object sender, EventArgs e)
{
Response.Write("HEJ KARL?");
}
My problem No matter what, the event is never fired. I know that both SetupControls() is being run, and the code behind the button which should fire the event is run.
When I debug, I can see the event QuestionAdded always are null.
How do I make this work?
Thanks a lot
© Stack Overflow or respective owner