Home All Groups Group Topic Archive Search About
Author
1 Nov 2005 10:47 AM
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

Author
1 Nov 2005 11:16 AM
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

Show quoteHide quote
"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
>
>
Are all your drivers up to date? click for free checkup

Author
1 Nov 2005 11:36 AM
Ben Amada
DJ wrote:

Show quoteHide quote
> 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
Author
1 Nov 2005 12:48 PM
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

Show quoteHide quote
"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
>
>
>
Author
2 Nov 2005 2:23 AM
Ben Amada
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



Post Thread options