0

I created a button from code behind:

Panel dynamicPanel = new Panel();
Button dynamicButton = new Button();
dynamicButton.Text = "View";            
dynamicButton.Click += new EventHandler(dynamicButton_Click);
dynamicPanel.Controls.Add(dynamicButton);
dynamicDiv.Controls.Add(dynamicPanel);

and the OnClick method:

protected void dynamicButton_Click(object sender, EventArgs e)
{
    Response.Write("view button response");
    string script = "alert(\"view clicked.\");";
    ScriptManager.RegisterStartupScript(this, GetType(), 
        "ServerControlScript", script, true);
}  

When I click the button, there is postback (IsPostback with Javascript alert) but the EventHandler is not fired. I can say that content that was visible on the page disappears if that is a clue.

I need to create this in a custom protected void method.

Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142
matt2605
  • 216
  • 1
  • 3
  • 18
  • 2
    maybe you should use JavaScript instead of C# for such dynamic. And if you use MVC, implementing dynamic is also easier. – Lei Yang Feb 10 '17 at 00:48
  • @LeiYang Don't know MVC. Can you give Javascript example? The problem is that dynamicPanel is created for each item in a list. So there are several of these on the page. – matt2605 Feb 10 '17 at 01:34
  • Webforms is out of date, it is not Winform so better start with MVC now. – Lei Yang Feb 10 '17 at 01:36
  • @LeiYang Sorry, cannot do. The website is already running. This work here is just adding custom features. – matt2605 Feb 10 '17 at 01:38
  • @matt2605: What is the reason that you use code behind instead of writing your event handler on the client side? – Quality Catalyst Feb 10 '17 at 02:43
  • @QualityCatalyst I populate the page with content in rows of panels generated from code behind. The content are from a list object, with each list item displayed in each panel. For example, a page list of customer names and their customer number. The button generated is to trigger a method call from the c# page to open this second panel to display more content. For example, their full details. If I can generate CollapsiblePanelExtender I would but cannot do this for button yet. – matt2605 Feb 10 '17 at 03:54
  • it seems the webpage doesn't know about the event getting created dynamically. you must have to set dynamic controls before events are getting intialised – Dirty Developer Feb 10 '17 at 05:35
  • http://stackoverflow.com/questions/6187944/how-can-i-create-dynamic-button-click-event-on-dynamic-button – Dirty Developer Feb 10 '17 at 05:37

1 Answers1

1

Your code looks a bit weird to me, but as for the problem "When I click the button, the method is not fired": Try renaming the method dynamicREGButton_Click() to dynamicButton_Click().

I assume you have a method dynamicButton_Click() in your code already; otherwise, you could not compile. However, the method you register via dynamicButton.Click += new EventHandler(...); is the method you call. The code you showed us doesn't match in that regard.

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
  • I've made the edit dynamicREGButton_Click() should have been dynamicButton_Click(). And you're right that it would not compile otherwise. I just want the button to work first before I set the code properly. – matt2605 Feb 10 '17 at 01:13
  • I've edited it again to clarify the "fired" meaning. – matt2605 Feb 10 '17 at 20:10