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. Dynamically created buttons

Dynamically created buttons

Scheduled Pinned Locked Moved ASP.NET
helpquestion
5 Posts 3 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.
  • A Offline
    A Offline
    antbates
    wrote on last edited by
    #1

    Hi, I have a situation where I need to create a variable number of buttons, all of which need their own event handlers. I see you can use myButton.Click += new System.EventHandler(clickMe); And this works fine when you know how many buttons you will have, as you just write an event handler for each button. But how does this work for a variable number of buttons? I can put them all through the same event handler, but I don't know which button was clicked then. Any help much appreciated Anthony

    P O 2 Replies Last reply
    0
    • A antbates

      Hi, I have a situation where I need to create a variable number of buttons, all of which need their own event handlers. I see you can use myButton.Click += new System.EventHandler(clickMe); And this works fine when you know how many buttons you will have, as you just write an event handler for each button. But how does this work for a variable number of buttons? I can put them all through the same event handler, but I don't know which button was clicked then. Any help much appreciated Anthony

      P Offline
      P Offline
      Paul Riley
      wrote on last edited by
      #2

      antbates wrote: I can put them all through the same event handler, but I don't know which button was clicked then. Yes, you will. Events have an argument called "sender" that you can cast to "Button" and then get the name. eg

      protected void Go_Click(object sender, System.EventArgs e)
      {
      Button btn = sender as Button;
      if (btn == null) throw (new InvalidOperationException(...));
      // use btn.ID, btn.UniqueID, btn.ClientID, depending on your needs
      }
      

      Paul We all will feed the worms and trees
      So don't be shy
      - Queens of the Stone Age, Mosquito Song

      A 1 Reply Last reply
      0
      • P Paul Riley

        antbates wrote: I can put them all through the same event handler, but I don't know which button was clicked then. Yes, you will. Events have an argument called "sender" that you can cast to "Button" and then get the name. eg

        protected void Go_Click(object sender, System.EventArgs e)
        {
        Button btn = sender as Button;
        if (btn == null) throw (new InvalidOperationException(...));
        // use btn.ID, btn.UniqueID, btn.ClientID, depending on your needs
        }
        

        Paul We all will feed the worms and trees
        So don't be shy
        - Queens of the Stone Age, Mosquito Song

        A Offline
        A Offline
        antbates
        wrote on last edited by
        #3

        Worked like a charm! Thanks so much for your help. Have a great weekend. Anthony

        1 Reply Last reply
        0
        • A antbates

          Hi, I have a situation where I need to create a variable number of buttons, all of which need their own event handlers. I see you can use myButton.Click += new System.EventHandler(clickMe); And this works fine when you know how many buttons you will have, as you just write an event handler for each button. But how does this work for a variable number of buttons? I can put them all through the same event handler, but I don't know which button was clicked then. Any help much appreciated Anthony

          O Offline
          O Offline
          OMalleyW
          wrote on last edited by
          #4

          Hay, Hows it going? Good question. Maybe this will help Create a Method that will handel the ButtonClick something like this private void ProcessLineItemCommand(object sender, System.EventArgs e) { } Now like you said, we are going to have to be able to tell what button was clicked in order to do that we will need to do a little casting here. Because I have quite a few Edit buttons and Delete buttons on my page I needed to make the ID unique so here is what the code looks like for the button creation in a loop. Button Del = new Button(); Button Edit = new Button(); Del.CssClass = "Button"; Del.Width = new Unit("30px"); Del.Click += new System.EventHandler(this.ProcessCommand); Del.ID = "Del_" + ItemReader["RequestID"].ToString(); Del.CommandArgument = ItemReader["RequestID"].ToString(); Edit.CssClass = "Button"; Edit.Width = new Unit("30px"); Edit.Click += new System.EventHandler(this.ProcessCommand); Edit.ID = "Edit_" + ItemReader["RequestID"].ToString(); Edit.CommandArgument = ItemReader["RequestID"].ToString(); ok so how the hell do I know what was clicked? private void ProcessLineItemCommand(object sender, System.EventArgs e) { string[] ButtonID = null; int ItemRecNo = 0; Button MyButton = new Button(); // Create new Button and set it to the sender MyButton = ((Button)sender); // now get the text of the button } now you will be able to tell what was clicked and process the information correctly hope this helps Will

          A 1 Reply Last reply
          0
          • O OMalleyW

            Hay, Hows it going? Good question. Maybe this will help Create a Method that will handel the ButtonClick something like this private void ProcessLineItemCommand(object sender, System.EventArgs e) { } Now like you said, we are going to have to be able to tell what button was clicked in order to do that we will need to do a little casting here. Because I have quite a few Edit buttons and Delete buttons on my page I needed to make the ID unique so here is what the code looks like for the button creation in a loop. Button Del = new Button(); Button Edit = new Button(); Del.CssClass = "Button"; Del.Width = new Unit("30px"); Del.Click += new System.EventHandler(this.ProcessCommand); Del.ID = "Del_" + ItemReader["RequestID"].ToString(); Del.CommandArgument = ItemReader["RequestID"].ToString(); Edit.CssClass = "Button"; Edit.Width = new Unit("30px"); Edit.Click += new System.EventHandler(this.ProcessCommand); Edit.ID = "Edit_" + ItemReader["RequestID"].ToString(); Edit.CommandArgument = ItemReader["RequestID"].ToString(); ok so how the hell do I know what was clicked? private void ProcessLineItemCommand(object sender, System.EventArgs e) { string[] ButtonID = null; int ItemRecNo = 0; Button MyButton = new Button(); // Create new Button and set it to the sender MyButton = ((Button)sender); // now get the text of the button } now you will be able to tell what was clicked and process the information correctly hope this helps Will

            A Offline
            A Offline
            antbates
            wrote on last edited by
            #5

            Thanks to both of you, very helpful explanations. Anthony

            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