Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9158861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:21:05+00:00 2026-06-17T13:21:05+00:00

I have referred Error with the event handlers of dynamic linkbutton . It says

  • 0

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);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:21:06+00:00Added an answer on June 17, 2026 at 1:21 pm

    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);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have referred this for creating error message tool tips, to be displayed continuously
I am referred to Hudson today. I have heard about continuous integration before, but
I have some code where, if a user has referred X number of people,
I have the following code which I'm using to populate form fields. This code
I would like to use CasperJS in node.js. I have referred to the following
I have developed a resizable panel in GWT. For this i have referred the
I have heard of types being referred to as boxed in some languages. In
I have been reading about Quicksort and found that sometimes it' s referred to
Im newbie here.I have a problem with codeigniter segment() method.I referred 6th segment of
I have an ASP.NET website, but this question isn't really about technology, it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.