Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

AddHandler not working.

Posted on 2004-05-01
5
Medium Priority
?
945 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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