Solved

AddHandler not working.

Posted on 2004-05-01
5
943 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
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

Technology Partners: 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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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