Event click doesn't fire with dynamically buttons created

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cesgdav
    New Member
    • Jul 2017
    • 1

    Event click doesn't fire with dynamically buttons created

    I'm creating dynamically buttons with some click event but when I click it this doesn't work


    the page_init is before my Page_Load
    Code:
    public bool AssignClicked
            {
                get
                {
                    return Convert.ToBoolean(ViewState["AssignClicked"]);
                }
                set
                {
                    ViewState["AssignClicked"] = value;
                }
            }
    Code:
    protected void Page_Init(object sender, EventArgs e)
            {
                if (AssignClicked)
                {
                    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 DButton = new Button();
                        DButton.CommandName = "Btn" + Convert.ToString(team);
                        DButton.ID = "Btn_" + Convert.ToString(team);
                        DButton.Text = team;
                        DButton.CommandArgument = name;
    
                        DButton.Click += new EventHandler(DynamoButton_Click);
    
                        Botones.Add(DButton);
    
                        GoodPanel.Controls.Add(DButton);
                        DButton.CssClass = "btn-primary outline separate";
                    }
                }
            }
    Code:
    these is the button that create them
    
            protected void DButton(object sender, EventArgs e)
            {
                AssignClicked = true;
                Page_Init(sender, e);
            }
    This is the event that I want to get fired

    Code:
    protected void DynamoButton_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 == "ENG" && x.TEAM == team && x.STANDBY == 0).ToList();
    
                var ToolArea = ListOfToolsOk.Select(x => x.TEAM);
                Grv_Eng.DataSource = ListOfToolsOk;
                Grv_Eng.DataBind();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "ModalGood();", true);
            }
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    The ViewState isn't loaded during the Initialization life cycle stage. The ViewState isn't loaded until the Load stage which occurs after the Initialization stage.

    I recommend saving the AssignClicked into a hidden field (or a cookie if you prefer) that you can retrieve from the request object to determine what controls need to be dynamically created during the Initialization event... so that the view state (and other information) about the controls can be loaded during the Load stage.

    This is a link to the MSDN documentation for the ASP.NET Page Life Cycle Overview

    -Frinny

    Comment

    Working...