• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

VB.NET AddHandler problem

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
Pennywisdom
Asked:
Pennywisdom
1 Solution
 
NetDeveloperCommented:
change Private to Protected in

Private Sub btnPage_Click(ByVal sender As Object, ByVal e As EventArgs)
....
0
 
PennywisdomAuthor Commented:
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
 
jfrossardCommented:
change
AddHandler myButton.Click, New System.EventHandler(AddressOf btnPage_Click)

to

AddHandler myButton.Click, AddressOf btnPage_Click
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
PennywisdomAuthor Commented:
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
 
PennywisdomAuthor Commented:
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
 
ChmodCommented:
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
 
ChmodCommented:
PAQ'd and points refunded, as proposed

Chmod
Community Support Moderator @Experts Exchange


0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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