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