Solved

AddHandler not working.

Posted on 2004-05-01
5
938 Views
Last Modified: 2008-10-03
Can anyone tell me why the code below is not working. When the button is clicked control does not seem to pass to the SubmitBtn_Click routine. Thanks.

Dim SubmitBtn as Button
SubmitBtn = New Button
SubmitBtn.Text = "OK"
SubmitBtn.Id = "OKButton"
AddHandler SubmitBtn.Click, AddressOf SubmitBtn_Click
Panel1.Controls.Add(SubmitBtn)
0
Comment
Question by:DColin
5 Comments
 
LVL 23

Expert Comment

by:rama_krishna580
Comment Utility
0
 
LVL 1

Accepted Solution

by:
CouldNotFindAnyOtherUsername earned 500 total points
Comment Utility
Hi DColin,
     What I can guess from this post and your last post regarding this problem is that the Click Event works when the page loads for the first time, but after a postback is made to the page, the click event stops working for the said button... Am i Right.. If this be the problem here's the solution:

Dynamically generated buttons should be generated in the Page_Load Function, so that after a postback, they are re-generated and the event handler re-associated.


Try changing your code as follows:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     ' Don't put Page.IsPostBack check here
     GenerateButton

     If not IsPostBack then
         'Your other code goes here
     End
End Sub

Private Sub GenerateButton()
     Dim SubmitBtn as Button
     SubmitBtn = New Button
     SubmitBtn.Text = "OK"
     SubmitBtn.Id = "OKButton"
     AddHandler SubmitBtn.Click, AddressOf SubmitBtn_Click
     Panel1.Controls.Add(SubmitBtn)
End Sub
0
 
LVL 1

Expert Comment

by:CouldNotFindAnyOtherUsername
Comment Utility
If you want something detailed regarding dynamic control creation, visit the following link:

http://devcenter.infragistics.com/Articles/ArticleTemplate.ASPX?ArticleID=2149
0
 
LVL 21

Expert Comment

by:tovvenki
Comment Utility
Hi,

try this modified code
Dim SubmitBtn as new Button
SubmitBtn.Text = "OK"
SubmitBtn.Id = "OKButton"
AddHandler SubmitBtn.Click, AddressOf SubmitBtn_Click
Panel1.Controls.Add(SubmitBtn)

it should now work.

hope that this helps you.

regards,
venki
0
 

Author Comment

by:DColin
Comment Utility
CouldNotFindAnyOtherUsername

Taking into account my previous questions is what I call a truly holistic service. Your solution has got me one step closer to my goals. No doubt I will be back tomorrow with another question.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now