473,769 Members | 5,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to use Click event with programmaticall y defined button?

Bob
Hi,

I'm working with VWD and i defined programmaticall y 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
programmaticall y.

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.EventArg s)
Handles Me.Load
Dim b As New Button
b.ID = "b1"
form1.Controls. Add(b)
End Sub
Dec 28 '06 #1
4 8471
Hi,

Bob wrote:
I'm working with VWD and i defined programmaticall y 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
programmaticall y.
[...]
The code:
---------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s)
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.EventArg s)
'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******** ******@TK2MSFTN GP02.phx.gbl...
Hi,

I'm working with VWD and i defined programmaticall y 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
programmaticall y.

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.EventArg s) 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*********@In tuiDev.comschre ef in bericht
news:e0******** ******@TK2MSFTN GP03.phx.gbl...
Hi,

Bob wrote:
>I'm working with VWD and i defined programmaticall y 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
programmatical ly.
[...]
The code:
---------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventAr gs)
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.EventArg s)
'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******@fitzm e.comwrote in message
news:uc******** ******@TK2MSFTN GP02.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
9055
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 object (both the HTML and control are built in the same .ascx file) The target button simply fires an event in the WebUserControl-derived object. So, alternatively, if there was a mechanism to call a method (explicitly or using an event of some...
3
7077
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
4321
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 on some initialization information obtained elsewhere. Basically, I'm going to create my own dynamic toolbar where the toolbarbuttons can change. I'm not using the VB toolbar because of limitations in changing things like backcolor (I can't get...
5
8000
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 the form, it works fine. how do i make it fire the click event of this button? (i have it on a diff. page and it works fine...can't figure out why it is diff. here) thanks to all! (i tried tab order)
5
5848
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. Thanks kai
15
62098
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
22115
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 by the folks in #winapi on EFnet, but I think those require the window to have focus. Either that, or I'm doing something else wrong. The click actually seems to work once, and then stops working... I have no idea what is the problem actually....
3
2459
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 checkboxlists and radiobutton lists. if i declare a variable with private withEvents img as ImageButton
8
19181
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 the next record and will display detail data in a datagridview also on the form which is related to the current main record in view. Another button ("Transfer Account") on the form can modify what detail data is related to the current main...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5299
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3562
muto222
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.