On click event not working

G

Guest

Can anyone help with this problem.
I’ve started using Visual Studio 2005. In 2003 if I wanted to create a
button dynamically in ASP.NET with a click event I just did this

Button MyButton = new Button();
MyButton.Click += new EventHandler(MyButton_Click);

And the resulting on click methos worked fine.

Now in Visual studio 2005 if I use the same technique Vis Studio creates a
method with the folowing

void MyButton_Click(object sender, EventArgs e)
{
throw new Exception("The method or operation is not
implemented.");
}


And the event will not fire. Does anyone know why this has changed and what
can I do to make it fire?
 
G

Guest

Peter_A,
the event should indeed fire. What you need to do is remove the stub "not
implemented exception code" and put real business logic code there.
Peter
 
J

John B

Peter_A said:
Can anyone help with this problem.
I’ve started using Visual Studio 2005. In 2003 if I wanted to create a
button dynamically in ASP.NET with a click event I just did this

Button MyButton = new Button();
MyButton.Click += new EventHandler(MyButton_Click);

And the resulting on click methos worked fine.

Now in Visual studio 2005 if I use the same technique Vis Studio creates a
method with the folowing

void MyButton_Click(object sender, EventArgs e)
{
throw new Exception("The method or operation is not
implemented.");
}


And the event will not fire. Does anyone know why this has changed and what
can I do to make it fire?
Are you storing MyButton somewhere so it doesnt go out of scope and get
GC'd?
i.e. in the form's controls collection.

JB
 
G

Guest

Chaps, my appologise. I have found the reason why the event was not firing.
It's because I was putting the code in the OnPreRender of an ASCX control
that I have been building.

When I moved the code into the OnInit section the event fired correctly.

Thank you for your efforts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top