473,756 Members | 1,770 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 8469
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
9054
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
7075
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
4319
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
62094
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
22114
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
19177
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...
1
9843
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
9713
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
8713
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
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...
1
3805
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 we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.