Home All Groups Group Topic Archive Search About

how to use Click event with programmatically defined button?

Author
28 Dec 2006 3:26 PM
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

Author
28 Dec 2006 3:42 PM
Olaf Rabbachin
Hi,

Bob wrote:

Show quoteHide quote
> 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
Author
28 Dec 2006 3:56 PM
Bob
Great. Thanks

Show quoteHide quote
"Olaf Rabbachin" <Olaf_NoSpam@IntuiDev.com> schreef in bericht
news:e0TW7apKHHA.5000@TK2MSFTNGP03.phx.gbl...
> Hi,
>
> Bob wrote:
>
>> 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
> --
> My .02: www.Resources.IntuiDev.com
Author
28 Dec 2006 3:45 PM
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.

--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006


Show quoteHide quote
"Bob" <.> wrote in message news:Og5CESpKHHA.1252@TK2MSFTNGP02.phx.gbl...
> 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
>
>
Author
28 Dec 2006 4:00 PM
Mark Rae
"Mark Fitzpatrick" <markf***@fitzme.com> wrote in message
news:ucELocpKHHA.3560@TK2MSFTNGP02.phx.gbl...

> 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...