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() 

        Dim oInnerButton = New Button() 

        oInnerButton.click = New System.EventHandler(AddressOf btnAddtoCart_Click)

    End While 

    ' ..... do some stuff .....

End Sub

 

The above code builds/compiles but displays this error when run: Expression 'Click' is not a procedures, but occurs as the target of a procedure call.


I have also tried the following code: AddHandler oInnerButton.click, AddressOf btnAddtoCart_Click

instead of the one line above but it won't even build/compile because of: 'click' is not an event of 'Object'.

 

Any help would be greatly appreciated.

 

Justin 

 

0
justinsamsel
11/15/2007 10:37:24 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

4 Replies
793 Views

Similar Articles

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

This is the way to do it:

AddHandler myButton.Click, AddressOf MyClick

I don't know if it's because you use click instead of Click.


/Fredrik Normén - fredrikn @ twitter

Microsoft MVP, MCSD, MCAD, MCT

ASPInsiders
My Blog
0
Fredrik
11/15/2007 10:47:35 PM

Fredrik N,

 Thanks for the reply. I had tried the capitalization to no avail before, but your post triggered an "aha" that I tested with a successful result. So, if I may add an addendum to my original question and describe the real reason for the error that I was receiving.

Previously I had this code:

    Dim oInnerButton = new Button()

    AddHandler oInnerButton.Click, AddressOf btnAddtoCart_Click

 

Well, declaring and instantiating a new Button in a single line generated the 'Click' is not an event of 'Object', but when I declare and subsequently instantiate on separate lines it works.

    Dim oInnerButton As Button
    oInnerButton = New Button()
     AddHandler oInnerButton.Click, AddressOf btnAddtoCart_Click

 

Why is this? And what is the most proper manner to declare and instantiate objects?

 

Thanks again 

0
justinsamsel
11/15/2007 11:33:42 PM

I'm not a VB.Net programmer I use C#, but if to instantiate a object shouldn't you write:

Dim oInnerButton As New Button()

I think that is the right way, or at least it was in VB6 and I think it's the same in VB.Net ;)


/Fredrik Normén - fredrikn @ twitter

Microsoft MVP, MCSD, MCAD, MCT

ASPInsiders
My Blog
0
Fredrik
11/15/2007 11:40:05 PM

 Well, I've seen several different ways to declare/instantiate objects in VB.NET. I have been using, without any problems, Dim oMyObject = new MyObject()

Although this hasn't caused me any problems until the one stated in the above posts, I spent a little time just looking at various code samples and I've learned that typically the object is declared and then on a different line it becomes instantiated. Such as:

Dim oMyObject as MyObject

oMyObject = new MyObject()

And to combine the two I mostly see:

Dim oMyObject as MyObject = new MyObject()

Which is different than my shorthand that I had been using. This is how I'll be doing it for now on. Anyhow, thanks for the help.

 

Justin 

0
justinsamsel
11/16/2007 12:29:47 AM
Reply:

Similar Artilces:

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...

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...

Problem with dynamically created Link Buttons click events handlers.
Hi Friends, Here I am creating LinkButtons dynamically on an aspx form. And when I click on one of those Linkbuttons another set of LinkButtons1 appear. When I click on LinkButton1 I need to create another set of Link buttons2 on the same form in which all the three sets of LinkButtons should be visible to the user. On Page Load I am generating the first set of LinkButtons. When I click on them the LinkButtons1 set is been created. Till here it works 100%. But when I click on LinkButtons1 it dosent generate LinkButtons2 on the form. Could anyone please help me with this? here is my...

Registering an event handler for a button control which is dynamically created and added to a PlaceHolder control
Task: Create a form to add computer to database. Problem:  I have a button (Submit) which is dynamically/programmatically created in an event handler method and added to a PlaceHolder.  The page is render correctly but when I click the submit buttom, the event handler method is not being called.  Code:  The 'table' is then added to the PlaceHolder. protected void AddComputer_Click(object sender, EventArgs e){// ... More code ...#region Add Submit button rowTableRow tbRowSubmitBtn = new TableRow();TableCell tbCellEmptySubmit = new TableCell();tbCellEmptySubmit.I...

Dynamicly created buttons don't call the event handler on click
Hello,I don't know why, but my first program had to be kinda hard from the start. It's for a mod project where the need arised to effectively handle, view and comment on files. (Like models, art). Listing the files is not a problem. However, each row that lists a file has another collumn with a linkbutton control in it. Whenever I click one of those, nothing happens. Inserting a break statement into the event handler didn't work as well :/. Due to my handicap that I like to solve things myself, I've been tobbing over this for 2 weeks now, it just doesn't make sense lol, not to me anyway.Here...

Please guide
Hi, I am creating dynamic panels and each of these panels have an image and a button. I would like to show a modal popup when the button of the panel is clicked. Contents of popup depend on which button was clicked. I would sincerely appreciate if some one could guide me if this is possible with Ajax and AjaxControltoolkit. Till now I tried to create modalpopupextended dynamically in the click event of button and page fails with an error message that TargetControl could not be found. Please guide. Regards & thanks Kapil    Hi Kapil, Unfortunately, the ModalPopup must h...

Dynamically created button & event handler for the button
I know that this question has been addressed a few times on these forums. I have looked over the posts that are refering to this process but I am still very confused. So I am posting here to see if someone could clear it up for me, I am a beginner at this so I must not be understanding previous posts. Here is the run down of what I am trying to accomplish. I have a page that has one button on it (button1) and a panel (panel1). When I click button1 I would like it to display another button (button2) on panel1. Then when you click button2 it will display a label on the panel. Panel1 a...

Adding Event Handlers Dynamically to Dynamically Created DropDownLists
I currently build DropDownList controls dynamically based on information from a database. The number of controls varies depending on what is returned from the database. I would like to be able to dynamically create Event Handlers for each of these DropDownLists (selectedIndexChanged event to be specific), and then to code against each of those events. some code is below private void CreateDropDownLists(DataSet ds) foreach (DataRow dr in ds.Tables[0].Rows) { DropDownList myddl = new DropDownList() myddl.ID = dr["ddlName"]; myddl.Width = unit.pixel(140); } I have another ...

how to create button controls dynamically .. then i need that click event of buttons
how to create button controls  dynamically .. then i need that click event of buttons  urgent ???????Vino  Add dynamic controls with eventsWebsite Design Darlington - http://mdssolutions.co.ukhttp://lessthandot.com - Experts, Information, Ideas & Knowledgehttp://aspnetlibrary.com - An online resource for professional ASP.NET developersPlease remember to click "Mark as Answer" on this post if it helped you refer below snippet..public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)  ...

Adding a javascript button click event to create user button
Hi:  I have a screen to create a username. In the create user screen i ahve put my terms and conditions for the site. In order to create a username the user has to accept the terms and conditions, for that he needs to check the checkbox accepting the terms and conditions. My question is:     Where do i add the javascript(client side) button click event in order to chck whether the user has checked the checkbox or not. If he did not check the checkbox and username should not be created.Can anybody please help with this!Thanks....

How to create and handle click event of dynamically created Link Buttons??
Hello Can anyone tell me how to create and handle events of dynemicallly created Link buttons?? Thanx for help..   Hi,take a look at this article: Adding a dynamic control to a placeholder control and wire up the event.Grz, Kris. Read my blog. Handy Firefox plugins for web developers.Workaround for non working Mark as answer buttons.  I think u have created button in page load event    like Button btn = new Button();        btn.ID = "btnId";        btn.Text = "Submit...

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 =...

I double click a button but no event handle in source view why, go to button properties,events,onclick, try to create event handle but its where. Only the button markup.
I have a button, if double click it no handle is created. Iam only taken to the buttons' markup in source view. This is happening on all pages through out the whole solution. Possible cause is, this project was not ajax enabled, i installed ajax, edited the web.config file of this project to make the site ajax enabled. I had to build the solution and got no errors, every thing is ok a part from double clicking all controls not being able to get to their event handles.--------------------------------------------------No Project Too Eazy.-------------------------------------------------- ...

Web resources about - Adding click event handler to a dynamically created button - asp.net.web-forms

Placebase team at Apple file “Schematic Maps” patent dynamically detailing important data
In a report from Computerworld way back in 2009, we learned that Apple had quietly acquired Maps API company Placebase. Then, founder of Placebase ...

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 ...

In a dynamically typed language, is it a bad idea to return different data types?
In some cases, returning different types from a single function works.

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 ...

Everything.me 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%

Everything.me 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/18/2016 9:40:53 AM