473,388 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

AddHandler Problem

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
4 2356
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Supra | last post by:
how do i do get menuitem "Connect" in addhandler ? Private Sub mnuIrc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuIrcConnect.Click, mnuIrcPref.Click,...
0
by: Jeffrey A. Voigt | last post by:
Can someone take a quick glace at my code and tell me why my AutoPostBackHandler function does not get fired off at all? What I'm trying to do is get all of the Buttons and DropDownList controls...
3
by: Nathan Sokalski | last post by:
I am using the AddHandler statement to add a CheckedChanged event handler to a series of RadioButtons that I create in a loop. However, the handler is not being called for a reason I cannot...
1
by: Luis Esteban Valencia Muñoz | last post by:
Have a dropdownlist created in my LoadMain() which is called from the Page_load: ************************PAGE LOAD********************8Private Sub Page_Load(ByVal sender As System.Object, ByVal e...
2
by: Just Me | last post by:
When a document is to be printed I call a method that contains an AddHandler statement. I just realized that if a second copy is to be printed the method is called and the AddHandler is executed...
6
by: Carlo3030 | last post by:
Been reading Dino Esposito's Article "A DetailsView Control for ASP.NET 1.x" found in the msdn Library. His example was created using VB.NET. I am trying to convert the example (DetailsView...
5
by: Slim | last post by:
i have a simple page, with one button button1. when click it creates a new button button 2 and adds a event handler to it. but when button 2 is clicked nothing happens, why? Partial Class...
5
by: ugavnholt | last post by:
Hi All, I'm having a problem with a small bit of code: Dim deleteButton = New System.Web.UI.WebControls.ImageButton deleteButton.imageURL = "images/icondelete.gif" deleteButton.ToolTip =...
15
by: Nathan Sokalski | last post by:
I have a section of my code that dynamically creates LinkButtons to allow the user to go to the page containing a question they have not answered. The code that creates the LinkButton is called, as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.