Button Click event not firing for dynamically created button

Imad

I have created dynamic controls on DropdownList's SelectedIndexChanged event. Button is one of those controls. I have also assigned event to that button but debugger is not coming on that click event. Following is my code.

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            token = Session["LoginToken"].ToString();
            if (!IsPostBack)
            {
                BindData();
                fsSearch.Visible = false;
                btnDownload.Visible = false;
            }
            else
            {
                foreach (HtmlTableRow row in (HtmlTableRowCollection)Session["dynamicControls"])
                {
                    tblSearch.Rows.Add(row);
                }
            }
        }
        catch
        {
        }
    }

    private void BindData()
    {
        ddlReportName.DataSource = svcCommon.GetReports(token, out message);
        ddlReportName.DataValueField = "Key";
        ddlReportName.DataTextField = "Value";
        ddlReportName.DataBind();
        ddlReportName.Items.Insert(0, "--Select--");
    }

    protected void ddlReportName_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            string[] reportInfo = ddlReportName.SelectedValue.Split('|');

            Session["dynamicControls"] = null;
            tblSearch.Rows.Clear();

            HtmlTableRow row = new HtmlTableRow();
            HtmlTableCell cellFieldNameLbl = new HtmlTableCell();
            HtmlTableCell cellFieldNameDdl = new HtmlTableCell();
            HtmlTableCell cellOperatorLbl = new HtmlTableCell();
            HtmlTableCell cellOperatorDdl = new HtmlTableCell();
            HtmlTableCell cellValueLbl = new HtmlTableCell();
            HtmlTableCell cellValueTxt = new HtmlTableCell();
            HtmlTableCell cellOperatorRbtn = new HtmlTableCell();
            HtmlTableCell cellAddMoreFilter = new HtmlTableCell();
            Button btnAddMore = new Button();

            DropDownList ddlColumn = new DropDownList();
            DropDownList ddlOperator = new DropDownList();
            TextBox txtValue = new TextBox();
            RadioButtonList rbtnOperator = new RadioButtonList();

            List<string> filterValues = svcCommon.GetSearchColumns(Convert.ToInt64(reportInfo[0]), token, out message);

            fsSearch.Visible = btnDownload.Visible = filterValues.Count > 0 ? true : false;

            ddlColumn.ID = "_ddlColumn0";
            ddlOperator.ID = "_ddlOperator0";
            txtValue.ID = "_txtValue0";
            rbtnOperator.ID = "_rbtnOperator0";
            btnAddMore.ID = "_btnAddMore0";


            rbtnOperator.Items.Add("AND");
            rbtnOperator.Items.Add("OR");
            rbtnOperator.RepeatDirection = RepeatDirection.Horizontal;

            btnAddMore.Text = "Add More";

            btnAddMore.Click +=btnAddMore_Click;

            ddlColumn.DataSource = filterValues;
            ddlColumn.DataBind();

            ddlOperator.DataSource = new List<string>() 
                                        {   
                                            "Equal",
                                            "Not Equal",
                                            "Less Than",
                                            "Less Than Or Equal",
                                            "Greater Than",
                                            "Greater Than Or Equal",
                                            "Start With",
                                            "Not Start With",
                                            "End With",
                                            "Not End With",
                                            "Contains",
                                            "Not Contains",
                                            "Between",
                                            "Not Between",
                                            "In",
                                            "Not In"
                                        };
            ddlOperator.DataBind();

            cellFieldNameLbl.InnerText = "Field Name:";
            cellFieldNameDdl.Controls.Add(ddlColumn);
            cellOperatorLbl.InnerText = "Operator";
            cellOperatorDdl.Controls.Add(ddlOperator);
            cellValueLbl.InnerText = "Value";
            cellValueTxt.Controls.Add(txtValue);
            cellOperatorRbtn.Controls.Add(rbtnOperator);
            cellAddMoreFilter.Controls.Add(btnAddMore);

            row.Cells.Add(cellFieldNameLbl);
            row.Cells.Add(cellFieldNameDdl);
            row.Cells.Add(cellOperatorLbl);
            row.Cells.Add(cellOperatorDdl);
            row.Cells.Add(cellValueLbl);
            row.Cells.Add(cellValueTxt);
            row.Cells.Add(cellOperatorRbtn);
            row.Cells.Add(cellAddMoreFilter);

            tblSearch.Rows.Add(row);
            Session["dynamicControls"] = tblSearch.Rows;
        }
        catch (Exception ex)
        {
        }
    }

    protected void btnAddMore_Click(object sender, EventArgs e)
    {
        try
        {

        }
        catch
        {
        }
    }
Imad

I hadn't enough time to try so many thing. The thing I did is stored the container on which dynamic controls are added into Session and on Page_Init event I have bound event and it is working fine now. :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Button click event not firing in user control, created dynamically in another user control

From Java

jquery - Click event not working for dynamically created button

From Dev

click event for dynamically created button in c#

From Dev

click() for dynamically created button

From Dev

on click event not firing for dynamically added button inside tab

From Dev

Jquery button click event not firing

From Dev

Dynamically created element not firing click event

From Dev

Dynamically created element not firing click event

From Dev

How to fire dynamically created button click event inside TableRow?

From Dev

Adding click event for a button created dynamically using jQuery

From Dev

How to catch click event with jQuery, on dynamically created button

From Dev

ng-click event is not firing on button click?

From Dev

on click function to dynamically created button

From Dev

Event handler for dynamically created Button

From Dev

Jquery event not firing for a button created in javascript with innerHTML

From Dev

Jquery event not firing for a button created in javascript with innerHTML

From Dev

Event handler not firing on dynamic button click

From Dev

Backbone js button click event not firing

From Dev

Jquery ajax button click event firing twice?

From Dev

My Button_click event is not firing in datalist

From Dev

jQuery click event not firing for dialog button

From Dev

button click event is not firing in a pop up window

From Dev

Event handler not firing on dynamic button click

From Dev

Button onclick event not firing on first click

From Dev

C# Dynamic button not firing click event

From Dev

how to get click button event of dynamically created button C# winforms

From Dev

Dynamic button not firing click event or page life cycle for button(not dynamic)

From Dev

Jquery click event not firing on the element created dynamically using jquery

From Dev

c# dynamically created buttons not firing click event

Related Related

  1. 1

    Button click event not firing in user control, created dynamically in another user control

  2. 2

    jquery - Click event not working for dynamically created button

  3. 3

    click event for dynamically created button in c#

  4. 4

    click() for dynamically created button

  5. 5

    on click event not firing for dynamically added button inside tab

  6. 6

    Jquery button click event not firing

  7. 7

    Dynamically created element not firing click event

  8. 8

    Dynamically created element not firing click event

  9. 9

    How to fire dynamically created button click event inside TableRow?

  10. 10

    Adding click event for a button created dynamically using jQuery

  11. 11

    How to catch click event with jQuery, on dynamically created button

  12. 12

    ng-click event is not firing on button click?

  13. 13

    on click function to dynamically created button

  14. 14

    Event handler for dynamically created Button

  15. 15

    Jquery event not firing for a button created in javascript with innerHTML

  16. 16

    Jquery event not firing for a button created in javascript with innerHTML

  17. 17

    Event handler not firing on dynamic button click

  18. 18

    Backbone js button click event not firing

  19. 19

    Jquery ajax button click event firing twice?

  20. 20

    My Button_click event is not firing in datalist

  21. 21

    jQuery click event not firing for dialog button

  22. 22

    button click event is not firing in a pop up window

  23. 23

    Event handler not firing on dynamic button click

  24. 24

    Button onclick event not firing on first click

  25. 25

    C# Dynamic button not firing click event

  26. 26

    how to get click button event of dynamically created button C# winforms

  27. 27

    Dynamic button not firing click event or page life cycle for button(not dynamic)

  28. 28

    Jquery click event not firing on the element created dynamically using jquery

  29. 29

    c# dynamically created buttons not firing click event

HotTag

Archive