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 9132669
In Process

The Archive Base Latest Questions

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

I have following code that adds dynamic LinkButton on the click event of a

  • 0

I have following code that adds dynamic LinkButton on the click event of a normal button. The LinkButtons need a EventHandler “LinkButton_Click“. Since the event handler need to be registered while Page_Load/Page_Init itself, I am first adding all the possible links within Page_Load (in postback scenario). (There are totally fours linkbuttons; only two will be displayed in the screen based on business scenario)

The code is working fine and the event handlers are getting invoked on click event.

Note: The business scenario is – if the current time’s second portion is less than 30, the first two LinkButtons need to be displayed; if greater than 30 the last two LinkButtons need to be displayed.

Note: The controls are cleared and added again inside PopulateLinksBasedOnCriteria() method

QUESTIONS

  1. In the PopulateLinksBasedOnCriteria() method, I am newly creating the link button instances. Still the event handler is firing. This is working because the ID that got added in the Page_Load is same. Is there any MSDN reference that substantiate this behavior? (I.e. the event handler will be based on the IDs registered while Page_Load/Page_Init. This will work even if the controls are cleared and added again)
  2. There is duplicate code in creating LinkButtons – one inside Page_Load and other inside PopulateLinksBasedOnCriteria() method. Is there a better way to achieve this business task without duplicate code?

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);
        }
    }
  • 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-17T08:21:07+00:00Added an answer on June 17, 2026 at 8:21 am

    I would suggest detecting what causes the PostBack in Page_Load Event On postback, how can I check which control cause postback in Page_Init event and then

     if (Page.IsPostBack && *PostBackControl.Name=="btnAdd"*)
                {
    if (DateTime.Now.Second < 30)
            {
                    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);
        } else
            {
                    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);
        }
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that adds an onmouseover event to a bullet onload
I have the following code that adds a button of type UIBarButtonItem on UIToolbar
I have the following code that adds a record to my MySQL database via
I have the following code that adds data to a rapidjson::Document, declared thus: rapidjson::Document
i have the following code that adds data to unordered lists. The troublesome code
I have a ugly piece of code that adds event handlers. The problem is,
I have built the following code with the Firefox add-on SDK that successfully adds
I have added the following code to a php script that adds users to
I have the following code that adds a background worker into a VB.net WPF
I have the following code that adds an observer in the loading of the

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.