473,471 Members | 4,648 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to use Click event with programmatically defined button?

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
Dec 28 '06 #1
4 8436
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
Dec 28 '06 #2
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
"Bob" <.wrote in message news:Og**************@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


Dec 28 '06 #3
Bob
Great. Thanks

"Olaf Rabbachin" <Ol*********@IntuiDev.comschreef in bericht
news:e0**************@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

Dec 28 '06 #4
"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:uc**************@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...
Dec 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JCE | last post by:
I need to programmatically invoke an asp:Button click event from a javascript function. The page containing the script and the button is the HTML page associated with a WebUserControl-derived...
3
by: TCORDON | last post by:
Why does the click event of an image button that submitts a form does not get fired when ENTER is pressend on a textbox of that form? How can I make this happen? Thanks
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
5
by: TS | last post by:
for some reason, it posts to the server, but no click events of any buttons on form fire. the button is the first one on the form. when the focus is inside the textbox, it doesnt' work. if i click...
5
by: kai | last post by:
Hi, In ASP.NET , what is the difference between OnClick and Click events for a button? Because we have button click event, it can trigger events, why we still need OnClick? Please help. ...
15
by: Oleg Subachev | last post by:
I need to programmatically invoke from other class Click event of the Button on my Form. Button.OnClick method is protected, not public. How to perform this ? Oleg Subachev
18
by: eliss.carmine | last post by:
Is it possible to simulate a mouse click in the window I made (it's a Form), but not give it focus? I tried using WinAPI's mouseevent and SendMessage of WM_LBUTTONDOWN/WM_LBUTTONUP as suggested...
3
by: Andreas Wöckl | last post by:
HI Group! I have to programmatically create a user input form with various Checkbox and RadioButton lists - Beside every List I have to place an image button that is able to reset the...
8
by: =?Utf-8?B?UmljaA==?= | last post by:
My from contains a "Move Next" button. When the user clicks on the "Move Next" button - several procedures get invoked and eventually, the dataset underlying the form will display main data from...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.