ASP .NET Dynamically adding EventHandlers to button on Page_Load. EventHandler's do not fire the fir
- by John
Background: I am customizing an existing ASP .NET / C# application. It has it's own little "framework" and conventions for developers to follow when extending/customizing its functionality. I am currently extending some of it's administrative functionality, to which the framework provides a contract to enforce implementation of the GetAdministrationInterface() method, which returns System.Web.UI.Control. This method is called during the Page_Load() method of the page hosting the GUI interface.
Problem: I have three buttons in my GUI, each of which have been assigned an Event Handler. My administration GUI loads up perfectly fine, but clicking any of the buttons doesn't do what I expect them to do. However, when I click them a second time, the buttons work.
I placed breakpoints at the the beginning of each event handler method and stepped through my code. On the first click, none of the event handlers are triggered. On the second click, they are triggered.
Any ideas?
Example of Button Definition
Button btn = new Button();
btn.Text = "Click Me Locked Screen";
bth.Click += new EventHandler(Btn_Click);
Example of Event Handler Method Definition
void Btn_Click(object sender, EventArgs e)
{
// Do Something
}