1

I'm trying to get fire a click event but wen I click over the dynamically button created this doesn't fire the event, I make some break points and this don't show an error or something else, I see the console of IE and doesn't show an error.

this is my code in page_load:

protected void Page_Load(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 newButton = new Button();
                    newButton.CommandName = "Btn" + Convert.ToString(team);
                    newButton.ID = "Btn_" + Convert.ToString(team);
                    newButton.Text = team;
                    newButton.CommandArgument = name;

                    newButton.Click += new System.EventHandler(newButton_Click);

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

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

before the Page_load I make this:

public bool AssignClicked
        {
            get
            {
                return Convert.ToBoolean(ViewState["AssignClicked"]);
            }
            set
            {
                ViewState["AssignClicked"] = value;
            }
        }

Here is where I make the click that change the AssignClicked value to true:

protected void Assign_Click(object sender, EventArgs e)
        {
            AssignClicked = true;
            Page_Load(sender, e);
        }

And this is the Event of the dynamicalliy button created:

protected void newButton_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();
        }

I was guided by this example LinkButton onclick event is not triggering for dynamically created buttons c#

The button that fire the event Assign_Click:

<asp:Button ID="Btn_mch" class="btn btn-primary area" Text="MCH" runat="server" Width="110px" OnClick="Assign_Click"></asp:Button>

Here is where the dynamically buttons get insert:

<asp:UpdatePanel ID="UpdatePanel3" runat="server" ControlID="Btn_eng">
            <ContentTemplate>
                <div id="tools_info" role="dialog">
                    <div class="modal-dialog modal-sm" style="width: 450px">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h3 id="title-info"><span class="glyphicon glyphicon-list-alt"></span>&nbsp Tools information</h3>
                            </div>
                            <div>
                                <%--<asp:Button ID="Btn_good" Text="Good condition" CssClass="btn-link" runat="server" />--%>
                                <button id="Btn_good" class="btn-link" onclick='TableGood();'" type="button">Good Condition</button>
                                <asp:Button ID="Btn_bad" Text="Bad condition" CssClass="btn-link" runat="server" OnClientClick="TableBad();" />
                            </div>
                            <br />
                            <div class="container">
                                <div id="tgood">
                                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                        <ContentTemplate>
                                            <asp:Panel ID="GoodPanel" runat="server">**HERE IS WHERE BUTTONS GET ADD**</asp:Panel>
                                        </ContentTemplate>
                                        <Triggers>
                                            <asp:AsyncPostBackTrigger ControlID="Btn_eng" EventName="Click"></asp:AsyncPostBackTrigger>
                                        </Triggers>
                                    </asp:UpdatePanel>
                                </div>
                                <div id="tbad">
                                    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                                        <ContentTemplate>
                                            <asp:Panel runat="server" ID="BadPanel"></asp:Panel>
                                        </ContentTemplate>
                                        <Triggers>
                                            <asp:AsyncPostBackTrigger ControlID="Btn_bad" EventName="Click" />
                                        </Triggers>
                                    </asp:UpdatePanel>
                                </div>
                            </div>
                            <br />
                        </div>
                    </div>
                </div>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Btn_eng" EventName="Click"></asp:AsyncPostBackTrigger>
            </Triggers>
        </asp:UpdatePanel>
cesg.dav
  • 320
  • 1
  • 14
  • Does it work if you comment out the line `newButton.OnClientClick = "return ModalGood();";` ? – Dan Dumitru Jul 04 '17 at 12:20
  • if I comment that line actually it show an error `Unable to cast object of type 'ASP.dashboard_aspx' to type 'System.Web.UI.WebControls.Button'.` this is over the 3 line on the `page_load` `Button Btn_clic = (Button)sender;` – cesg.dav Jul 04 '17 at 12:32
  • Well, of course, in `Page_Load` `sender` is not a `Button`. That code shouldn't be there. – Dan Dumitru Jul 04 '17 at 12:44
  • and where it can be? although in reality I remove the button, and insert some data in the linq query and doesn't fire the event :/ – cesg.dav Jul 04 '17 at 12:54
  • Can you share the which creates the button and attaches the handler to the click event if that button? Is this button part of some other control such as gridview or listview? – Chetan Jul 04 '17 at 12:57
  • the button isn't part of something, I add more info to the question I don't know if that is what you required me – cesg.dav Jul 04 '17 at 13:07

0 Answers0