Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to create an array of buttons then access attributes in event handler

How to create an array of buttons then access attributes in event handler

Scheduled Pinned Locked Moved ASP.NET
helpcsharpdesigndata-structurestutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    scjsb
    wrote on last edited by
    #1

    I am trying to learn C# etc by making this code work, its a mix of examples I've found on the web. The problem is the event handler never triggers. I would also like to change the text of the button that was clicked in the event handler. In a nutshell, I want to create an array of buttons, then change attributes as the text or color of the button selected. Eventually I want to add an array of labels which correstpond to these buttons and change the text or color depending on which button is pushed. Any help is appreciated. public partial class _Default : System.Web.UI.Page { static Button[] btn_arr = new Button[14]; static int btn_count; protected void Page_Load(object sender, EventArgs e) { try { if (btn_arr[0] is Button) { foreach (Button button in btn_arr) { add_button(button); } } else { for (int i = 0; i < 14; i++) { Button new_button = new Button(); new_button.ID = "btn" + Convert.ToString(i); new_button.Text = "Button" + Convert.ToString(i); new_button.Click += new EventHandler(btn_Click); btn_arr[btn_count++] = new_button; add_button(new_button); } } } catch (Exception ex) { lblStatus.Text += ex.Message.ToString(); } } protected void add_button(Button button) { try { panelLineA.Controls.Add(button); } catch (Exception ex) { lblStatus.Text += ex.Message.ToString(); } } // this is never triggered void btn_Click(object sender, EventArgs e) { int btnIndex = Convert.ToInt32(((Button)sender).ID.Substring(3, 1)); lblStatus.Text = "Button " + btnIndex + " was pushed." + ((Button)sender).ID; } }

    S 1 Reply Last reply
    0
    • S scjsb

      I am trying to learn C# etc by making this code work, its a mix of examples I've found on the web. The problem is the event handler never triggers. I would also like to change the text of the button that was clicked in the event handler. In a nutshell, I want to create an array of buttons, then change attributes as the text or color of the button selected. Eventually I want to add an array of labels which correstpond to these buttons and change the text or color depending on which button is pushed. Any help is appreciated. public partial class _Default : System.Web.UI.Page { static Button[] btn_arr = new Button[14]; static int btn_count; protected void Page_Load(object sender, EventArgs e) { try { if (btn_arr[0] is Button) { foreach (Button button in btn_arr) { add_button(button); } } else { for (int i = 0; i < 14; i++) { Button new_button = new Button(); new_button.ID = "btn" + Convert.ToString(i); new_button.Text = "Button" + Convert.ToString(i); new_button.Click += new EventHandler(btn_Click); btn_arr[btn_count++] = new_button; add_button(new_button); } } } catch (Exception ex) { lblStatus.Text += ex.Message.ToString(); } } protected void add_button(Button button) { try { panelLineA.Controls.Add(button); } catch (Exception ex) { lblStatus.Text += ex.Message.ToString(); } } // this is never triggered void btn_Click(object sender, EventArgs e) { int btnIndex = Convert.ToInt32(((Button)sender).ID.Substring(3, 1)); lblStatus.Text = "Button " + btnIndex + " was pushed." + ((Button)sender).ID; } }

      S Offline
      S Offline
      sudhanvag
      wrote on last edited by
      #2

      scjsb wrote:

      The problem is the event handler never triggers.

      Check this out.

      public partial class Default3 : System.Web.UI.Page
      {
      protected void Page_Load(object sender, EventArgs e)
      {
      ButtonArray();
      }

      protected void ButtonArray()
      {
          try
          {
              for (int i = 0; i < 14; i++)
              {
                  Button new\_button = new Button();
                  new\_button.ID = "btn" + Convert.ToString(i);
                  new\_button.Text = "Button" + Convert.ToString(i);
                  //new\_button.Click += new EventHandler(btn\_Click);
                  new\_button.Click += new EventHandler(new\_button\_Click);
                  panelLineA.Controls.Add(new\_button);
      
              }
      
          }
          catch (Exception ex)
          {
              lblStatus.Text += ex.Message.ToString();
          }
      }
      
      void new\_button\_Click(object sender, EventArgs e)
      {
          int btnIndex = Convert.ToInt32(((Button)sender).ID.Substring(3, 1));
          lblStatus.Text = "Button " + btnIndex + " was pushed." + ((Button)sender).ID;
      }
      

      }

      Cheers, Sudhanva

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups