?
Solved

VB.NET AddHandler problem

Posted on 2003-03-19
7
Medium Priority
?
505 Views
Last Modified: 2007-12-19
I have a web page that displays data from a database. The data can be over multiple pages. I want to add page buttons so the user can go directly to a specific page. For some reason the link buttons that I add do not call the procedure that I associated them with when they are clicked.

Here is my code to add the buttons:

  Private Sub ShowPageButtons(ByVal PageCount As Integer)
    Dim i As Integer
    Dim myButton As System.Web.UI.WebControls.LinkButton
    Dim Row As TableRow
    Dim Cell As TableCell

    Row = New TableRow()

    For i = 1 To PageCount
      myButton = New System.Web.UI.WebControls.LinkButton()
      myButton.Text = i.ToString
      If i = CurrentPage Then myButton.Enabled = False
      Cell = New TableCell()
      Cell.Controls.Add(myButton)
      AddHandler myButton.Click, New System.EventHandler(AddressOf btnPage_Click)
      Row.Cells.Add(Cell)
    Next

    tblPage.Rows.Add(Row)
  End Sub

And here is my procedure that is supposed to be called:
  Private Sub btnPage_Click(ByVal sender As Object, ByVal e As EventArgs)
    CurrentPage = CInt(sender.text)
    ViewState("CurrentPage") = CurrentPage
    Refresh()
  End Sub


I put a breakpoint at the start of btnPage_Click and it's never called, can anyone tell me why?
0
Comment
Question by:Pennywisdom
[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
7 Comments
 
LVL 10

Expert Comment

by:NetDeveloper
ID: 8167849
change Private to Protected in

Private Sub btnPage_Click(ByVal sender As Object, ByVal e As EventArgs)
....
0
 

Author Comment

by:Pennywisdom
ID: 8167923
Nope even if I declare it like this:
Protected Sub btnPage_Click(ByVal sender As Object, ByVal e As EventArgs)

it doesn't work
0
 

Expert Comment

by:jfrossard
ID: 8168161
change
AddHandler myButton.Click, New System.EventHandler(AddressOf btnPage_Click)

to

AddHandler myButton.Click, AddressOf btnPage_Click
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:Pennywisdom
ID: 8168422
I tried

AddHandler myButton.Click, AddressOf btnPage_Click

instead of

AddHandler myButton.Click, New System.EventHandler(AddressOf btnPage_Click)

with

Protected Sub btnPage_Click(ByVal sender As Object, ByVal e As EventArgs)

or

Private Sub btnPage_Click(ByVal sender As Object, ByVal e As EventArgs)

Still doesn't work, sorry.
0
 

Author Comment

by:Pennywisdom
ID: 8210269
I think I have found why the event was not raised.
If a button created at runtime raises an event, the button has to be recreated before the Page_Load is finished for the event procedure to be called.

So it seems there was nothing wrong with my code, but there was a problem with when I was calling it.
0
 

Accepted Solution

by:
Chmod earned 0 total points
ID: 8213537
Pennywisdom has requested this question is closed as s/he found the solution. If there are no further comments within 72 hours, I will close it and refund points.

Chmod
Community Support Moderator @Experts Exchange


0
 

Expert Comment

by:Chmod
ID: 8233592
PAQ'd and points refunded, as proposed

Chmod
Community Support Moderator @Experts Exchange


0

Featured Post

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

801 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