By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,717 Members | 2,069 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,717 IT Pros & Developers. It's quick & easy.

AddHandler Problem

P: n/a
DJ
Good morning,

Still new at this so please bear with me. I am creating a table dynamically
using webcontrols based on the output of a sproc from my database.The table
represents test instances that the test proctor who is viewing the page has
currently assigned to him or heir. Each row of the table corresponds to a
single test instance and includes required info about that test.

At the end of the row I create a button control. Based on whether or not the
test is currently active, this button will either allow the user to activate
the test or change the test date.

This all works fine, up to the point of using the AddHandler. Here is the
button code:

dim newBtn as new Button()
'Text, class assignment, etc.
if bIsActive = true then
AddHandler newBtn.Click, AddressOf change_test_date
else
AddHandler newBtn.Click, AddressOf activate_test
end if

So ok, this works, except I have to pass the test number with this event to
make sure the correct test is addressed and updated. I tried

AddHandler newBtn.Click, AddressOf change_test_date(objReader("test_num"))

But I get the following error:

'AddressOf' operand must be the name of a method; no parentheses are needed.

The question is how do I pass the test number as an argument for this event?
I am sure its something simple, but I can't find it. Any help would be really
appreciated. Thanks in advance.

Jim Hawley
Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
DJ
I take part of it back, the AddHandler throws errors, but when I use just
plain

AddHandler newBtn.Click, AddressOf change_test_date

and have the following sub:

sub change_test_date(Sender as Object, e as EventArgs)
'Whatever
end sub

and execute the page and then check the source, there is no onclick event
for the button. Yet if I do like referenced below, I get an error. Go
figure...help!

Jim Hawley

"DJ" wrote:
Good morning,

Still new at this so please bear with me. I am creating a table dynamically
using webcontrols based on the output of a sproc from my database.The table
represents test instances that the test proctor who is viewing the page has
currently assigned to him or heir. Each row of the table corresponds to a
single test instance and includes required info about that test.

At the end of the row I create a button control. Based on whether or not the
test is currently active, this button will either allow the user to activate
the test or change the test date.

This all works fine, up to the point of using the AddHandler. Here is the
button code:

dim newBtn as new Button()
'Text, class assignment, etc.
if bIsActive = true then
AddHandler newBtn.Click, AddressOf change_test_date
else
AddHandler newBtn.Click, AddressOf activate_test
end if

So ok, this works, except I have to pass the test number with this event to
make sure the correct test is addressed and updated. I tried

AddHandler newBtn.Click, AddressOf change_test_date(objReader("test_num"))

But I get the following error:

'AddressOf' operand must be the name of a method; no parentheses are needed.

The question is how do I pass the test number as an argument for this event?
I am sure its something simple, but I can't find it. Any help would be really
appreciated. Thanks in advance.

Jim Hawley

Nov 19 '05 #2

P: n/a
DJ wrote:
I take part of it back, the AddHandler throws errors, but when I use just
plain

AddHandler newBtn.Click, AddressOf change_test_date

and have the following sub:

sub change_test_date(Sender as Object, e as EventArgs)
'Whatever
end sub

and execute the page and then check the source, there is no onclick event
for the button. Yet if I do like referenced below, I get an error. Go
figure...help!

Jim Hawley


If "test_num" is a simple integer, then when you assign your Button an ID,
you could append the test_num to the end of the ID. Something like:

newBtn.ID = "btn" & objReader("test_num")

Then in your event handler, you can get the test_num value by checking the
ID of the 'Sender' parameter.

Another option is to store the test_num value and the Button's ID in
ViewState:

ViewState(newBtn.ID) = objReader("test_num")

Then when your event handler is called, you can extract the test_num value
out of ViewState using the ID of the 'Sender' parameter.

Good luck,
Ben
Nov 19 '05 #3

P: n/a
DJ
Sounds good but I can't get the event handler to fire. I checked the source
and its not there. I did code the IDs of the buttons like you said before
hand so I wouldn't get the error where two or more objects have the same ID,
so I could use it if I could just get the event handler to fire.

I even thought about doing it as follows:

sub Page_Load(Sender as Object, e as EventArgs)
dim sTempText as string
If IsPostBack then
sTempText = Sender.ID
end if
end sub

but all I get back there is ASP.pagename.

I've put just a label.Text = "hi there" in the event handler to show that
its firing and nothing.

Any ideas? I think my base is that the AddHandler is not functioning.

Jim

"Ben Amada" wrote:
DJ wrote:
I take part of it back, the AddHandler throws errors, but when I use just
plain

AddHandler newBtn.Click, AddressOf change_test_date

and have the following sub:

sub change_test_date(Sender as Object, e as EventArgs)
'Whatever
end sub

and execute the page and then check the source, there is no onclick event
for the button. Yet if I do like referenced below, I get an error. Go
figure...help!

Jim Hawley


If "test_num" is a simple integer, then when you assign your Button an ID,
you could append the test_num to the end of the ID. Something like:

newBtn.ID = "btn" & objReader("test_num")

Then in your event handler, you can get the test_num value by checking the
ID of the 'Sender' parameter.

Another option is to store the test_num value and the Button's ID in
ViewState:

ViewState(newBtn.ID) = objReader("test_num")

Then when your event handler is called, you can extract the test_num value
out of ViewState using the ID of the 'Sender' parameter.

Good luck,
Ben

Nov 19 '05 #4

P: n/a
I created a small example which demonstates what you're trying to do and it
appears to work on my end. Also, you shouldn't have to worry about a
missing "onclick" attribute where the button is defined in the rendered
HTML. I'm fairly certain the reason for this is because ASP.NET stores the
button's click event to your event handler in ViewState. Below is the
rendered HTML I see for a dynamically created button which has an event
handler tied to it:

<input type="submit" name="btn1" value="Button 1" id="btn1" />

Below is the code I used. You might want to compare what you have to the
code below or try the code below and see what happens on your end.

Private Sub Page_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load

Dim newBtn As Button

For i As Integer = 1 To 5
newBtn = New Button
newBtn.ID = "btn" & i.ToString
newBtn.Text = "Button " & i.ToString
PlaceHolder1.Controls.Add(newBtn)
AddHandler newBtn.Click, AddressOf MyButtonHandler
Next

End Sub

Private Sub MyButtonHandler( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs)

Dim sID As String
sID = CType(sender, Button).ID.Substring(3)

End Sub
Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.