EventHandler is not working for dynamic control

I have referred Error with the event handlers of dynamic linkbutton . It says to add event handlers in Page_Init or Page_Load. I tired following code. But the event handler is not fired when I click on the dynamic added link buttons. What need to be corrected here?

Note: The dynamic LinkButton controls are added in the click event of a button after some business validations (the business code is not given for brevity)

Markup

<form id="form1" runat="server">
<div>
    <asp:LinkButton ID="lnkTest" runat="server" OnClick="LinkButton_Click">Static LinkButton</asp:LinkButton>
    <br />
    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
    <br />
    <asp:PlaceHolder ID="plhDynamicLinks" runat="server"></asp:PlaceHolder>
</div>
</form>

Code Behind

    protected void Page_Load(object sender, EventArgs e)
    {

        foreach (Control ctrl in plhDynamicLinks.Controls)
        {
            LinkButton dynamicButton = (LinkButton)ctrl;
            dynamicButton.Click += new EventHandler(LinkButton_Click);
        }

        if (Page.IsPostBack)
        {

        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        int x = 0;

        foreach (Control ctrl in plhDynamicLinks.Controls)
        {
            LinkButton dynamicButton = (LinkButton)ctrl;
            dynamicButton.Click += new EventHandler(LinkButton_Click);
        }
    }

    protected void LinkButton_Click(object sender, EventArgs e)
    {
        LinkButton clickedControl = (LinkButton)sender;
        Response.Write(clickedControl.ID +" Link Button Clicked");
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        plhDynamicLinks.Controls.Clear();

        LinkButton button1 = new LinkButton();
        button1.ID = "D1";
        button1.Text = "1";
        plhDynamicLinks.Controls.Add(button1);

        LinkButton button2 = new LinkButton();
        button2.ID = "D2";
        button2.Text = "2";
        plhDynamicLinks.Controls.Add(button2);
    }

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

It is mandatory to register all the required dynamic controls’ event handlers in the Page_Load/ Page_Init itself. One working example can be seen at Dynamic Control’s Event Handler’s Working

MarkUp

<form id="form1" runat="server">
<div>
    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
    <br />
    <asp:PlaceHolder ID="plhDynamicLinks" runat="server"></asp:PlaceHolder>
</div>
</form>

CODE BEHIND

    protected void Page_Load(object sender, EventArgs e)
    {

        if (Page.IsPostBack)
        {
            LinkButton lnk1 = new LinkButton();
            lnk1.ID = "D1";
            lnk1.Text = "A";
            //Event handler must be registered in the Page_Load/Page_Init
            lnk1.Click += new EventHandler(LinkButton_Click);
            plhDynamicLinks.Controls.Add(lnk1);

            LinkButton lnk2 = new LinkButton();
            lnk2.ID = "D2";
            lnk2.Text = "B";
            lnk2.Click += new EventHandler(LinkButton_Click);
            plhDynamicLinks.Controls.Add(lnk2);

            LinkButton lnk3 = new LinkButton();
            lnk3.ID = "D3";
            lnk3.Text = "C";
            lnk3.Click += new EventHandler(LinkButton_Click);
            plhDynamicLinks.Controls.Add(lnk3);

            LinkButton lnk4 = new LinkButton();
            lnk4.ID = "D4";
            lnk4.Text = "D";
            lnk4.Click += new EventHandler(LinkButton_Click);
            plhDynamicLinks.Controls.Add(lnk4);

        }
    }

    protected void LinkButton_Click(object sender, EventArgs e)
    {
        PopulateLinksBasedOnCriteria();
        LinkButton clickedControl = (LinkButton)sender;
        Response.Write(DateTime.Now.ToString()+"___"+ clickedControl.ID + " Link Button Clicked" );
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        PopulateLinksBasedOnCriteria();
    }

    private void PopulateLinksBasedOnCriteria()
    {
        plhDynamicLinks.Controls.Clear();

        if (DateTime.Now.Second < 30)
        {
            LinkButton linkButton1 = new LinkButton();
            linkButton1.ID = "D1";
            linkButton1.Text = "1";
            plhDynamicLinks.Controls.Add(linkButton1);

            LinkButton linkButton2 = new LinkButton();
            linkButton2.ID = "D2";
            linkButton2.Text = "2";
            plhDynamicLinks.Controls.Add(linkButton2);
        }
        else
        {
            LinkButton linkButton3 = new LinkButton();
            linkButton3.ID = "D3";
            linkButton3.Text = "3";
            plhDynamicLinks.Controls.Add(linkButton3);

            LinkButton linkButton4 = new LinkButton();
            linkButton4.ID = "D4";
            linkButton4.Text = "4";
            plhDynamicLinks.Controls.Add(linkButton4);
        }
    }

Solution 2

Dynamic controls must be re-created on every postback, this Article is a good link about how to persist dynamic controls and their state.

Solution 3

Add javascript onClick attribute to the dymanic control and set hidden field values which is required for the control event. Onclick of the dymanic grid, the will postback and will get the hidden field value. In page load call a method to do the job if the hidden field has value and make it null after doing the job.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply