0

I create some Buttons dynamically but when I'm trying to call the event click this doesn't get in, I make some break points to see if something went wrong and doesn't mark an error or something like that...

this is my code where I create and assign some type of data:

/*********************ASSIGN BUTTONS DINAMICALLY***********************/
        protected void Assign_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showAndHide();", true);

            Button Btn_clic = (Button)sender;
            var name = Btn_clic.Text;

            List.ListUsers listArea = new List.ListUsers();
            List<Data.Area> Area = listArea.AreaList();

            List<Data.Area> ListOfEquiposOk = Area.Where(x => x.AREA == name && x.STANDBY == 0).ToList();

            List<Button> Botones = new List<Button>();

            var TeamFCH = ListOfEquiposOk.Select(x => x.TEAM).Distinct().ToList();

            foreach (var team in TeamFCH)
            {
                Button newButton = new Button();
                newButton.CommandName = "Btn" + Convert.ToString(team);
                newButton.ID = "Btn_" + Convert.ToString(team);
                newButton.Text = team;

                newButton.Click += Info_Click;
                newButton.CommandArgument = name;

                newButton.OnClientClick = "return ModalGood();";
                Botones.Add(newButton);

                GoodPanel.Controls.Add(newButton);
                newButton.CssClass = "btn-primary outline separate";
            }
        }
        /**********************************************************************/

and here is where I try to call the event previously declared:

     void Info_Click(object sender, EventArgs e)
    {
        Button Btnclick = (Button)sender;
        var team = Btnclick.Text;
        string name = Btnclick.CommandArgument;

        List.ListUsers listArea = new List.ListUsers();
        List<Data.Area> Area = listArea.AreaList();

        List<Data.Area> ListOfToolsOk = Area.Where(x => x.AREA == name && x.TEAM == team && x.STANDBY == 0).ToList();

        var ToolArea = ListOfToolsOk.Select(x => x.TEAM);
        Grv_Eng.DataSource = ListOfToolsOk;
        Grv_Eng.DataBind();
    }
cesg.dav
  • 320
  • 1
  • 14
  • Try setting property `Enabled` to true. – Michał Turczyn Jul 03 '17 at 14:14
  • 1
    You must add dynamiccontrols in the page's initialization stage. https://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx – Crowcoder Jul 03 '17 at 14:15
  • 1
    Why are you creating the buttons dynamically? Why not create them as part of some repeater in your markup? – mason Jul 03 '17 at 14:15
  • @mason well I never make that before, so I have ignorance over that. what is the benefit of make buttons with that method? – cesg.dav Jul 03 '17 at 14:22
  • Creating buttons dynamically in code behind is tricky and you likely won't get it wired up properly. But if you create them in markup via repeater, it will get wired up to the events properly, not to mention being a lot cleaner than writing code. – mason Jul 03 '17 at 14:23
  • @Crowcoder I can't make the buttons when the page init, I need to make it when the client click over some buttons to get the data from a XML file. – cesg.dav Jul 03 '17 at 14:24
  • @cesg.dav if the page is posting back, you can do it in `page_init` but you may need some code modifications. Bottom line is the web form won't "know" about the dynamic controls when you do it your way so you are going to have to do something differently. – Crowcoder Jul 03 '17 at 14:27
  • @mason in the repeater I can make the buttons dynamically? because the client have an undefined quantity of buttons, that's why I opt by doing the buttons dynamically – cesg.dav Jul 03 '17 at 14:35
  • 1
    Yes. Look up what a [repeater](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater(v=vs.110).aspx) is. You define a template, and it uses that template to render markup for each item in the collection that you bind to it. – mason Jul 03 '17 at 14:39
  • @mason one question... how can I pass the sender value when I make a click over another button? `Button Btn_clic = (Button)sender;` it show an error on `sender` – cesg.dav Jul 03 '17 at 15:23
  • @Crowcoder I change my code into `Page_Init` but what parts of the code I need to change? https://stackoverflow.com/questions/44905518/event-click-doesnt-get-fired?noredirect=1#comment76790267_44905518 – cesg.dav Jul 04 '17 at 16:13
  • I thought `sender` would not be your button, but maybe it is. Regardless, I agree with others that this is the wrong way to accomplish your goal. A control like Repeater will be easier and more stable. – Crowcoder Jul 05 '17 at 10:43
  • how is the better way to use the repeater? I never have used before – cesg.dav Jul 05 '17 at 11:44

0 Answers0