I'm using a foreach loop to create a list of users which all have a modify button. These are generated when they are loaded from the database.
For now the function only creates the modify button, but the problem is the .Click action:
foreach (XElement user in xml.Element("users").Elements())
{
Button modifyThisUser = new Button();
modifyThisUser.Text = "Modify " + user.Attribute("LoginName").Value;
modifyThisUser.Click += new EventHandler(ModifyThisUser_click);//only works when activated at Page_Load
modifyThisUser.Attributes.Add("username", user.Attribute("LoginName").Value);
form1.Controls.Add(modifyThisUser);
}
This function creates the buttons.
When the function is called from the Page_Load the .Click action works, but if the function is triggert by a button the .Click won't work.
Why won't the .Click work if it is generated when the page is already loaded? The button itself shows and all the options work perfect, only the .Click isn't set. (so the EventHandler isn't fired when clicked)
How can I fix this problem?