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

  • Home
  • SEARCH
  • 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 6037779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:07:26+00:00 2026-05-23T06:07:26+00:00

I generated two buttons and put them into a list of buttons.. List<Button> buttons

  • 0

I generated two buttons and put them into a list of buttons..

       List<Button> buttons = new List<Button>();
       Button pgs=new Button
            for(int i=0;i<2;i++)
                        pgs.Width = 20;
                        pgs.Command += obtainTopicsPerPage_Click;
                        pgs.CommandName = i.ToString();
                        pgs.Text =i.ToString();

                        btns.Add(tPage.ToString());
                        buttons.Add(pgs);

I added the buttons to a placeholder, and they appear on the page.

The event that they have is the following:

   void obtainTopicsPerPage_Click(Object sender, CommandEventArgs e)
    {
        foreach (var item in tPages)
        {
            if (item.Key == e.CommandName)
            {
                foreach (var posts in item.Value)
                {
                    posts.ExecuteAll();
                }
            }
        }
         MyButtonTable();
    }

It doesn’t matter what the functions do (they simply create tables and make it look like posts in a forum page)..

Now when the user clicks any one of the buttons that appear on the screen..none of the events are being triggered.. I put a breaking point inside the eventhanlder method and the web application doesn’t reach there.

All I am concerned is why the buttons aren’t attached to the event handling method that I gave them..why when I set the break point in the event it never triggers.

What should happen, when a button is clicked.. the event should should be triggered first, and the page should load second. But that doesn’t happen..what happens is the button event being skipped, and the page load event is triggered with every postback after the button click..

Updated:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    string[] d;
    protected void Page_Load(object sender, EventArgs e)
    {
         d = new string[] { "dfadfas", "daads", "dasda", "dads" };
        Buttons();
    }
    List<Button> btns;
    public void Buttons()
    {
        btns = new List<Button>();
        for (int i = 0; i < 20; i++)
        {
            Button d = new Button();
            d.Text = "Click me";
            d.Click += Me_Click;
            btns.Add(d);

        }
       function();

    }
    public void function()
    {
        foreach (var item in btns)
        {
            PlaceHolder1.Controls.Add(item);
        }

    }
    public int i { get{object o=ViewState["i"];return (o==null)?0:(int)o;} set{ViewState["i"]=value;} }
    public void Me_Click(object sender, EventArgs e)
    {

        foreach (var item in d)
        {
            Label da= new Label();
            da.Text = "d"+i+++"<br/>";
            this.Controls.Add(da);

        }


    }
}

The working code above..

I tested by saving the buttons list with Session. Here I recreate the buttons at PreInit

 void Page_PreInit(object sender, EventArgs e)
{
    List<Button> btn = (List<Button>)Session["Buttons"];//debugging shows 2 buttons
    if (btn != null)
    {
        foreach (var item in btn)
        {
            item.Width = 20;
            item.Command += obtainTopicsPerPage_Click;
            item.CommandName = tPage.ToString();
            item.Text = tPage.ToString();
        }
    }
  • 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-05-23T06:07:27+00:00Added an answer on May 23, 2026 at 6:07 am

    This works for me, and should hopefully give you an idea. I know it’s very simplistic but follows on from what you’ve provided. When you click the buttons the page Reloads therefore recreating the buttons and setting everything up again. To avoid that happening check that Page.IsPostBack is false.

    public partial class GuessNumber : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            lblResult.Text += "Written in by Page_Load <br />";
        }
    
    
        protected void LoveMyButtons_click(object sender, EventArgs e) {
            string response = txtGuess.Text;
    
            lblResult.Text += string.Format("LoveMybuttons " + response + " <br />");
        }
    
        protected void Page_PreInit(object sender, EventArgs e) {
            CreateButtons();
        }
    
    
        List<Button> btns;
        public void CreateButtons() {
    
            btns = new List<Button>();
    
            for (int i = 0; i < 6; i++) {
                Button butt = new Button();
                butt.Text = "Click me";
                butt.Click += LoveMyButtons_click;
                btns.Add(butt);
    
            }
            AddMyButtonsToAPlaceHolder();
        }
    
        public void AddMyButtonsToAPlaceHolder() {
            foreach (var item in btns) {
                plhButtonStore.Controls.Add(item);
            }
    
        }
    
    }
    
    
    
    
    <body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtGuess" runat="server" />
        <asp:Button ID="btnPeanut" runat="server" Text="Guess" />
    
        <br /><br />
        <asp:Label ID="lblNumberOfGuesses" runat="server" />
        <br />
        <asp:Label ID="lblResult" runat="server" />
        <br />
        <asp:PlaceHolder ID="plhButtonStore" runat="server" />
    </div>
    </form>
    </body>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a asp:radio button with two list items asp:RadioButtonList id=Myradio asp:listitem1 value=li1 asp:listitem2
I have a class split across two files. One of these is generated, the
I generated drop down menus for my site in Vista Buttons. When it exports
I've generated some JSON and I'm trying to pull it into an object in
I just pasted some generated javadoc into an eclipse project, to discover none of
I am trying to put some jquery UI code into an existing asp.net app,
I am using Page.Isvalid function in my web page.I have two buttons in my
I have a button that has its location generated dynamically via jquery: <a id=final_location
If i have two signals that i generated before, how can i make one
I have to generate two random sets of matrices Each containing 3 digit numbers

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.