How to add click event for dynamically added button

I added a button dynamically:

Dim btn As New Button()
btn.ID =
btn.Text = "ButtonJohan"
btn.OnClientClick = "redirect()"
btn.Style.Add("display", "none")


How can I add the click event for this button?

Protected Sub btnSaveSettings_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveSettings.Click
End Sub

Johan Theunissen
MCPD, MCSE, MCTS BizTalk 2006


Please mark the most helpful reply/replies as "Answer".
10/15/2007 9:09:56 AM 93655 articles. 6 followers. Follow

3 Replies

Similar Articles

[PageSpeed] 40
Get it on Google Play
Get it on Apple App Store

You can use the AddHandler method e.g. 

Website Design Darlington - - Experts, Information, Ideas & Knowledge - An online resource for professional ASP.NET developers

Please remember to click "Mark as Answer" on this post if it helped you

10/15/2007 9:19:19 AM


AddHandler btn.Click, AddressOf btnSaveSettings_Click
 before the line Controls.Add(btn)

10/15/2007 9:30:16 AM

Hai Johan,

U can try this code..... 

To add a server-side event handler, use the following code:
MyButton.Click += new System.EventHandler(MyButton_Click)[/CODE]

where MyButton_Click is as follows:
private void MyButton_Click (object sender, System.EventArgs e)
// Your button click code here.
}[/CODE]If you want to do something client-side in the onClick event, use:
myButton.Attributes.Add("onClick", "fnClientSideThing()");[/CODE]
This code will fire the client-side javascript function called
fnClientSideThing() when the button is clicked.


u can also go  through the concerned link on adding Click event for dynamically added button


Thanks and Regards



Mark as "Answered" if this post is helpful...................

Thanks and Regards
10/15/2007 9:46:20 AM

Similar Artilces:

Dynamically adding buttons to web form and wiring up the onclick event.
I have a web form that is generated at runtime with a number of dynamically created buttons. Creating the buttons is easy, but wiring them up is not. I would love to use the cold behind and a single onclick event. Then based on the id, I can call a single method that does an operation. Each button will perform the same tasks. The user can chose one of three tasks. I though I would either have a dropdown or three radio buttons for this. Alternatively, I could have some javascript, but I am not quiet sure how to post back with javascript and if all the buttons will make it more difficult. Does...

I Want to create a tool box,toolbox contains image button and fileupload.These two buttons can be added at runtime(user can add any number of image button and fileupload button dynamically by click)
I Want to create a tool box,toolbox contains image button and fileupload buttons.These two buttons can be added at runtime(user can add any number of image button and fileupload button dynamically by click).for every image user will browse images using fileuploader and he will add the image to that imagebutton at runtime. how to write server side coding to upload the selected image and to set that image to imagebutton at runtime? is it possible? any other way to do it?  advance thanks ask123: I Want to create a tool box,toolbox contains image button and fileupload b...

button click event not firing for dynamic buttons added on PreRender of the updatepanel
Hi,i have a webpage with an updatepanel updatepanel1.i am adding controls dynamically to the updatepanel on the prerender event.i am using the prerender event as i want to get the latest bata from thedatabase which include the data i have inserted on my last button click.But the button click event is not getting trigged on clicking the button.Here is the sample code in PreRenderSqlCommand cmd = new SqlCommand("SELECT Heading, ID FROM tblNews ORDER BY ID;", conn);conn.Open();SqlDataReader sqlData =cmd.ExecuteReader(CommandBehavior.CloseConnection);while (sqlData.Read()){lblHeading =...

Dynamically adding a button click event
If I create a button dynamically, how can i add an event handler for the click event?This is a section of my code:  Button btnDelete = new Button();//........... newCell = new TableCell();newRow.Cells.Add(newCell);newCell.Attributes.Add("align", "center");btnDelete.Text = "Delete";btnDelete.ID = "m" + newMessage.ID;btnDelete.Click += new EventHandler(btnDelete_Click);btnDelete.CssClass = "admin_button";newCell.Controls.Add(btnDelete);//........... private void btnDelete_Click(object sender, EventArgs e){ //.............. }...

Dynamically loaded Web User Control .ascx button click event handler not called on second time click
I have an ascx control (that performs actions for an entity) being added dynamically to accordion panes, and then the panes are added to an ajax accordion control, so that the accordion can contain multiple panes with each pane containing one ascx control. The ascx control contains a delete button which removes the entity from the database. Once the entity is removed, I need to reflect this deletion in the parent page by removing the accordion pane from the accordion. To do this I created a delegate function and which is called from the parent page to rebind the accordion and hence not d...

How to add click event to a dynamic generated button?
I am adding a button to a webform dynamically and adding a click event to this button,I need button.text as the parameter of the event, how can I do it as EventArgs can't pass any parameters.System.Web.UI.WebControls.Button btn1 = new System.Web.UI.WebControls.Button();btn1.text = "1"; += new System.EventHandler(btn1_click) // How can I pass btn1.txt to click event as a parameter?private void btn1_Click(object sender, EventArgs e) { }Hi, Teemu Keiski, It works! Thanks a lot!!! Hi,It passes all you need in this case via method arguments. :-) ...

how to add click event to dynamically created button
I am developing web application and in my web form I am creating Button programatically(dynamically or fly) and I want to add Addhander event to this button. How can I do this? Or in other words I want to show message box(or any other tasks) when users clicks on dynamically created button. Tell me how can I do this? thankyou maxmax Is this control extending System.Web.UI.WebControls.Button or is it a System.Web.UI.WebControls.Button? If so, you can use it's OnClick event.Ryan OlshanASPInsider | Microsoft MVP, ASP.NEThttp://ryanolshan.comHow to ask a question Somethin...

accessing click event of button added dynamically
Hi my problem is this - I want to dynamically include a button in the cell of a table I have created on an aspx page in VWD, and be able to refer to the button's click event in code. However, I can't seem to access the click event in code, as I haven't physically placed the button on the aspx page. How do I access the button events without physically placing the button on the form. I am coding in VB.Net.  Here's what I've managed to work out so far: Protected WithEvents btnView As ButtonProtected Sub DisplayRecords(ByVal recShort As ShortRecord)        Dim...

How could add a javascript function to a button click event as a part of its click event handler?
Hi, The title might be confusing. What I want is simply two pages, Page1 and Page2, when I click on a linkbutton on page1, page2 pops up. I used to use a little bit of javascript to do this, and it worked just fine. I just added the following line of code to the Page_Load event of Page1:   protected void Page_Load(object sender, EventArgs e) { LinkButton1.Attributs.Add("onclick","'Page2.aspx',null,'height=500,width=650,status=no,resizable=yes,scrollbars=yes,toolbar=no,location=no,menubar=no')"); } But now, I have...

Adding click event handler to a dynamically created button
This question has been asked many times in different forums but none of the answers have solved my issue yet. There must be something I'm missing. I am creating buttons on-the-fly and want to assign an eventhandler to each of their click events (the same eventhandler, as I assume that I'll be able to determine the unique sender from the input param). Here's the code that I've tried:Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load    ' ..... do some stuff .....     While oDataReader.Read() ...

How to add event to Web Form from the designer
 Hi there, I know that when i double click a web form I'm taking to the page_load event.  However, I want to create an unload event by I don't know how to create it from the designer.  Does anybody know how to create a page unload event from the designer? Many thanks  Page events are not exposed in the design view. If you are using VB, you should be able to use dropdowns on top of the code editor -> select Page in the left one and the designed event in the right one and editor will generate the code. Unfortunately, C# editor does not p...

Dynamically adding User Control instances on Button click event
Hi All:  I have a user control which has three text boxes in it. Now I have a page where there is a place holder and a button. On the click event of the button I add an instance of the user control to the place holder. It works fine the first time and the user control is added to the place holder. However when I click again, a new user control instance is added and the first instance is not there... same thing is repeated if I click instance is added and none of the previous ones are ther ein the place holder. The code for the web page is as follows:int iTextBoxUserControlC...

How to dynamic add new tab panel on button click event
Hi, and sorry for an old question.I am creating a webchat site and I need to create a chat window as a tab for every chat session. For example, when I double click on my friend's nick name in the friends list, the tab container will create a new tab panel and enable me and my friend chatting in that panel.But, when I try to create new tab panel with a button click event, I will have "out of range" error in the second click (the first click will create a new tab without any error). I think the problem is my tab container object missing in the next page load event, and I nee...

Button click event not fired for a custom webpart where controls are added dynamically.
We are migrating a Sharepoint 2003 site to MOSS 2007 and ASP.NET 2.0. We have also migrated the webparts but they are still inheriting from Sharepoint.WebPart. We have a webpart where controls are added dynamically at runtime. On this webpart a button is added dynamically and a click event handler is also attached. The button on the webpart is displayed only when there is some data. The button deletes the selected row of data when clicked. But when we click on the button, the page gets posted but the event handler is not called. We analysed and found that the code where we add the button to ...

Web resources about - How to add click event for dynamically added button -

iPhone 5 A6 chip to dynamically up-clock up to 1.3GHz (and downclock too)?
Interesting video from our friends at TLDToday: If this is not just a Geekbench app glitch, it could indicate why that A6 processor is so 2X ...

Apple's Lightning port dynamically assigns pins to allow for reversible use
... a closer look at how Apple's new Lightning connector works, and has come to the conclusion that the 8 pins on each side of the plug are dynamically ...

iPhone 5 Lightning port dynamically reassigns pins
... of Apple's Lightning USB cable shows that the pins on the plug aren't arranged symmetrically, suggesting that the Lightning port can dynamically ...

Urlbox Launches Screenshots-as-a-Service Platform to Dynamically Update Galleries
... changes. Launched yesterday, Urlbox allows anyone with a gallery displaying website screenshots to automate the process and keep them dynamically ... launcher dynamically adapts to what you’re doing right now
... phone according to what you’re doing. Perform a search for ‘football’ and your background will change to the NFL logo. A launcher that dynamically ...

A YouTube Experiment Resizes the Player Dynamically
There's a new YouTube experiment that changes the video player's size, depending on the browser window's size. When resizing Chrome's window, ...

Apple Granted “Dynamically Changing Cursor for User Interface” Patent, Originally Filed in 2003
... a utility patent that covers the use of the mouse cursor that changes according to the context of the task it is engaged in. Called a “Dynamically ...

Atmel's FPSLIC II Dynamically Reconfigurable SoC Supports "Silicon-Sharing" For Peripherals & Interfaces ...
Silicon-Sharing Fits 200K Gate Design into 40K Gate FPSLIC II's, Cuts Power Drain by 97% Launcher Wants to Dynamically Change Phones Based on Location, Interests, and Mental State ...
What if your phone’s launcher dynamically changed based upon interests, your location, or topics you had in mind at a specific moment? A new ...

Resources last updated: 1/13/2016 3:28:42 PM