Solved

AddHandler not working.

Posted on 2004-05-01
5
942 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
ID: 10966958
0
 
LVL 1

Accepted Solution

by:
CouldNotFindAnyOtherUsername earned 500 total points
ID: 10967053
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
ID: 10967077
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
ID: 10967344
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
ID: 10967463
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Modal Popup Extender control 1 53
Problem to copy file 14 55
orderby list (from Json) 1 32
Broken .resx file generating errors 18 27
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

696 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