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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:29:36+00:00 2026-06-01T04:29:36+00:00

I have associated a Button ID while generating Buttons dynamically and created a Button

  • 0

I have associated a Button ID while generating Buttons dynamically and created a Button event handler as follows:

btn.Click += new EventHandler(btn_Click);

Button Click event:

protected void btn_Click(object sender,EventArgs e)
{
    Button b = new Button();
    b = (Button)sender;
    string i = b.ID.Substring(b.ID.Length - 1, 1);
    int j1 =Convert.ToInt32(i);
    id1 = to[j1];
    Page.ClientScript.RegisterClientScriptBlock(Type.GetType("System.String"), 
        "addScript", "PassValues('" + id1 + "')", true);
    ScriptManager.RegisterStartupScript(this, this.GetType(), 
       "sendMessage", "javascript:sendMessage(); ", true);
}

However this event is not getting called i have placed all my controls inside an UpdatePanel

EDIT: What is happening is on button click somehow a function is getting called which is present on Page.Load

protected void getEntriesRight()
{
    j = (int)Session["j"];

    int n1 = j + 3;

    for (; j <= n1; j++)
    {
        if (j < fr.data.Length)
        {
            HtmlGenericControl listItem = new HtmlGenericControl("li");
            HtmlGenericControl a1 = new HtmlGenericControl("a");
            Label anchor = new Label();
            Image im = new Image();

            btn = new Button();
            im.ImageUrl = fr.data[j].pic_square;
            im.Height = 45;
            im.Width = 47;


            btn.CssClass = "btn-add";
            btn.Text = "Invite";
            to[j] = fr.data[j].uid;

            btn.ID = "btn" + j;


            a1.Attributes.Add("href", "#");
            anchor.Text = fr.data[j].name;
            a1.Controls.Add(btn);
            a1.Controls.Add(im);
            a1.Controls.Add(anchor);
            listItem.Controls.Add(a1); 
            list.Controls.Add(listItem);

                btn.Click += new EventHandler(btn_Click);

        }
    }

    Session["j"] = j;
}

Help!

  • 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-01T04:29:37+00:00Added an answer on June 1, 2026 at 4:29 am

    Dynamically generated controls have to be recreated on every postback in order for their events to fire. This is one of the most commonly-encountered problems with generating controls programmatically in .NET, and it is discouraged if you can find a way around it.

    For example, if you have a button that should only be present when another button is clicked, have the button in the page to begin with, and use the Visible property to control whether it is shown or not.


    Regarding your edit. Every post-back to the server, even inside an UpdatePanel, is going to call Page_Load. If you want to detect whether a request has come from an UpdatePanel then you need to check !Page.IsAsync before calling the function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 'click' event associated with 6 image buttons. It is currently possible
I'm new to Android development, I have created an animation which I have associated
I have a page with multiple textboxes and each has a button associated with
I just noticed a strange behaviour in IE7. I have radio buttons with associated
I have a dynamically created table and in some of the cells i have
What to look for when I have associated a CSS file with my Default.aspx
Currently I have a UITabBarController with 4 items on it that all have associated
I have and associated array looking like this: array(225) { [0]=> array(3) { [id]=>
I have a table where I have datetimes associated with an ID: ┌────────────────┬──────────────────────┐ │
I have a User class which may or may not have an associated Department.

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.