Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 946
  • Last Modified:

AddHandler not working.

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
DColin
Asked:
DColin
1 Solution
 
CouldNotFindAnyOtherUsernameCommented:
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
 
CouldNotFindAnyOtherUsernameCommented:
If you want something detailed regarding dynamic control creation, visit the following link:

http://devcenter.infragistics.com/Articles/ArticleTemplate.ASPX?ArticleID=2149
0
 
tovvenkiCommented:
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
 
DColinAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now