ASP.NET (Visual Studio 2005, .NET 2.0): AddHandler ..., AddressOf ...: order of execution problem

V

Vassili King

Hi:

I was wondering if anybody had a similar problem and could help me out
with a situation I ran into. I would really appreciate any advice.

I've created an .aspx page for my Web Application in Visual Studio
2005. I'm using .NET version 2.0. The situation narrows down to the
following:

-- I have a Private Sub FillTable(ByVal parameter As String) that
programmatically generates as many <tr>...</tr> (TableRows) as there
are records in my database and adds them to a placeholder
(placeholder.controls.add(TableRow)).

-- Inside of the FilLTable subroutine amoung all other stuff I
generate
LinkButtons (one per record) like so:
Dim btnMyLinkButton As LinkButton
btnMyLinkButton = New LinkButton
btnMyLinkButton.Text = [getting a value from my
database]
btnMyLinkButton.ID = [first part of the ID] & "!"
& [second part of the ID] 'this is to be used
later
AddHandler btnMyLinkButton.Click, AddressOf
btnMyLinkButton_Click
...

-- Somewhere below the FillTable subroutine I have another sub:
Private Sub btnMyLinkButton_Click(ByVal s As Object, ByVal e
As System.EventArgs)
Dim B As LinkButton = DirectCast(s, LinkButton)
Dim ItemInfo() As String = B.ID.Split("!"c)

Dim OrderNumber As Int32 = Convert.ToInt32(ItemInfo(0))
Dim LineNumber As Int32 = Convert.ToInt32(ItemInfo(1))
Me.ViewState("PartNumber") = Nothing
AddPart(LineNumber, OrderNumber)
txtPartLookupControl.Text = String.Empty
End Sub

-- When I'm running my project, the link buttons are created normally,
but the onclick event does not catch btnMyLinkButton_Click, but is a
regular PostBack. That is, when a link button is clicked, the page
refreshes whithout even running btnMyLinkButton_Click sub. It looks
like AddHandler...AddressOf... is trying to get the sub, but doesn't
find it (probably because the sub isn't built yet or something like
that). I've tried to place btnMyLinkButton_Click() sub above
FillTable(), but that was silly anyway and sure didn't help.

Any suggestion would be highly appreciated.
Thank you very much for your time!
 
B

bruce barker

on postback you have to call the filltable routine (or the postback
class instance will not have the controls nor the handler hookups). this
should be done in oninit. also you should not use "!" in and id as its
not legal char.


-- bruce (sqlwork.com)
 
V

Vassili King

Bruce,

Thank you for your response. A small problem with this approach would
be passing a parameter that is entered in a text box on the same page.
It is not a hard thing to solve though (capture it in a hidden field,
viewstate or similar stuff).



Actually, I've just found another solution:

I've created a hidden field with ID="hfLinkButton". Inside of the
FilLTable subroutine I have btnInvoice.OnClientClick =
"document.forms.aspnetForm._ctl0_ContentPlaceHolder1_hfLinkButton.value='"
& [first part of the ID] & "!" & [second part of the ID] & "';"

Page_Load catches the value of the hidden field and when it is <> 0,
sends the value to the modified btnMyLinkButton_Click() sub like so:

Dim strLinkButtonValue As String = hfLinkButton.Value
If strLinkButtonValue <> "" Then
btnMyLinkButton_Click(strLinkButtonValue)
End If

That's it.
Thank you very much for your time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top