how to use Click event with programmatically defined button?

B

Bob

Hi,

I'm working with VWD and i defined programmatically a button (in
code-behind) with an ID. So I expect to see beside "Page Events" and "Form1"
my button "b1" in order to use the Click event. But it doesnt' appear. After
saving the code-behind file, i only see "Page Events" and "Form1".
If i define the button declarativally, i see it. But i need to define it
programmatically.

Can anybody tell me how to do? I also tried with Attributes.add but that's
only for client script.
Thanks
Bob

The code:
---------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Dim b As New Button
b.ID = "b1"
form1.Controls.Add(b)
End Sub
 
O

Olaf Rabbachin

Hi,
I'm working with VWD and i defined programmatically a button (in
code-behind) with an ID. So I expect to see beside "Page Events" and "Form1"
my button "b1" in order to use the Click event. But it doesnt' appear. After
saving the code-behind file, i only see "Page Events" and "Form1".
If i define the button declarativally, i see it. But i need to define it
programmatically.
[...]
The code:
---------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Dim b As New Button
b.ID = "b1"
form1.Controls.Add(b)
End Sub

first of all, the page's Init-event actually is the place where dynamic
controls should be created. That is because this way you can actually use
the so created controls in the course of events afterwards. With a button,
this will not matter much though as you don't need to retrieve any
information from clients during postbacks.
In order to be able to consume your button's click-event, you'll need to
define a handler and, once the control has been added to the page, tell
your app to actually fire it when your button is clicked. In other words,
create a handler ...

Protected Sub MyButton_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
'Your code goes here
End Sub

.... and then link your dynamic button to that handler:

'...
form1.Controls.Add(b)
AddHandler b.Click, AddressOf MyButton_Click

Cheers,
Olaf
 
M

Mark Fitzpatrick

You'll never see your new button added to the intellisense when you create
it dynamically in this fashion. The button only exists at run-time and
therefore cannot be accessed at designtime, especially since it's scope as a
variable is limited to the pageload. Why not add the click event handler at
the same time you are adding the new button control tot he form. You don't
have to have it in the designer to add the click event to it. You can add
the event in the line before you add b to the the form controls, or right
after. I've heard a number or recomendations for setting things like a click
event after you add it to the page's control hierarchy so that it is better
tracked in the viewstate.

Now, your big problem is going to be timing. The real issue here is since
the button isn't created until the page_load event is called you won't be
able to receive a click event if the postback occurs before the control is
recreated. What you'll need to do is experiment with placing the button
creation in an earlier event such as the init or preload. That way they
button will be created before the postback event is called.
 
B

Bob

Great. Thanks

Olaf Rabbachin said:
Hi,
I'm working with VWD and i defined programmatically a button (in
code-behind) with an ID. So I expect to see beside "Page Events" and
"Form1"
my button "b1" in order to use the Click event. But it doesnt' appear.
After
saving the code-behind file, i only see "Page Events" and "Form1".
If i define the button declarativally, i see it. But i need to define it
programmatically.
[...]
The code:
---------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Handles Me.Load
Dim b As New Button
b.ID = "b1"
form1.Controls.Add(b)
End Sub

first of all, the page's Init-event actually is the place where dynamic
controls should be created. That is because this way you can actually use
the so created controls in the course of events afterwards. With a button,
this will not matter much though as you don't need to retrieve any
information from clients during postbacks.
In order to be able to consume your button's click-event, you'll need to
define a handler and, once the control has been added to the page, tell
your app to actually fire it when your button is clicked. In other words,
create a handler ...

Protected Sub MyButton_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
'Your code goes here
End Sub

... and then link your dynamic button to that handler:

'...
form1.Controls.Add(b)
AddHandler b.Click, AddressOf MyButton_Click

Cheers,
Olaf
 
M

Mark Rae

What you'll need to do is experiment with placing the button creation in
an earlier event such as the init

Dynamic controls should always be created in Page_Init...
 

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,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top