473,224 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

Click-Event by dynamical Button

Hallo NG,

I created a Button with Click-Event dynamically:
System.Web.UI.WebControls.Button bt_1 = new Button();
bt_1.Click += new EventHandler(bt_1_click);

and I implemented the Funktion bt_1_click in which I created a second
Button dynamically too.

System.Web.UI.WebControls.Button bt_2= new Button();
bt_2.Click += new EventHandler(bt_2_click);

The two Buttons display themselves well.
But the Funktion bt_2_click would never be fired.
I can not understand it. I used the same mechanism, but why the first Button
worked while the second one not.

Thanks a lot

Liqun

Nov 18 '05 #1
2 2320
Liqun,

For an event to fire on postback the object that triggers the event has to
first be recreated.

Here's what's happening.

You are creating Button1 every time your page is loaded. This is fine
because when you click the button the page is reloaded. The button is
recreated and the click event is fired showing Button2.

But when you click Button2, the page is reloaded, Button1 is recreated but
Button1 wasn't clicked this time so Button2 is not recreated. If Button2
isn't recreated then it's event will never fire.

In this case what you'd have to do is put Button2's creation code into a
subroutine.

'Add a view state variable to the subroutine like this:
ViewState("CreateButton2") = True

'Inside of the Button1 click event call The code to create Button2.

'Also add an if then to your page load routine:

Dim CreateButton2 As Boolean = CType(ViewState("CreateButton2"), Boolean)
If CreateButton2 Then
'---Call Button2 Creation Subroutine here
End If

Now after you create Button2 the first time by clicking Button1 it will be
recreated from then on and your event will fire if it's clicked.
--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Liqun Xu" <lx*@abm-soft.de> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
Hallo NG,

I created a Button with Click-Event dynamically:
System.Web.UI.WebControls.Button bt_1 = new Button();
bt_1.Click += new EventHandler(bt_1_click);

and I implemented the Funktion bt_1_click in which I created a second
Button dynamically too.

System.Web.UI.WebControls.Button bt_2= new Button();
bt_2.Click += new EventHandler(bt_2_click);

The two Buttons display themselves well.
But the Funktion bt_2_click would never be fired.
I can not understand it. I used the same mechanism, but why the first Button worked while the second one not.

Thanks a lot

Liqun


Nov 18 '05 #2
Dynamic controls must be re-created before their events can be fired.

As the second button is created and wired up in the bt_1_click postback
event of the first button, its onClick will never fire, because when the
page postback occures for the bt_2_click, bt_1_click is not fired, and
therefore the second button is not created, causing the event to be ignored.

Aren't you better off creating both buttons together, and changing the
Visible property instead?

Mun

"Liqun Xu" <lx*@abm-soft.de> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
Hallo NG,

I created a Button with Click-Event dynamically:
System.Web.UI.WebControls.Button bt_1 = new Button();
bt_1.Click += new EventHandler(bt_1_click);

and I implemented the Funktion bt_1_click in which I created a second
Button dynamically too.

System.Web.UI.WebControls.Button bt_2= new Button();
bt_2.Click += new EventHandler(bt_2_click);

The two Buttons display themselves well.
But the Funktion bt_2_click would never be fired.
I can not understand it. I used the same mechanism, but why the first Button worked while the second one not.

Thanks a lot

Liqun

Nov 18 '05 #3

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

Similar topics

1
by: The Roys | last post by:
I want to be able to detect if an image has been right-clicked. I have 5 images in my program. If I Click the image it gets selected by my program. But I want to be able to Right-Click one of the...
3
by: MFC | last post by:
Hi I'm from Denmark so I hope you understand my English. Well, I have a filebox and when I click on a picture-file, then I have a small picturebox, where I can preview the file. When I...
1
by: Lazlo Woodbine | last post by:
I'm trying to implement a dynamic menu using CSS/DHTML/JavaScript. The menu bar is implemented as hyperlinks so I can use the :hover :active etc. pseudo-styles. When moving from one item to...
6
by: Richard A. Lowe | last post by:
I'm using P/Invoke to call SendInput (using code culled from these newsgroups!) to send mouse events to a window. But I'm unsure how to send double-clicks. A VB6 article I saw on SendInput...
11
by: Thom Little | last post by:
I would like three states on an icon ... Left Click Right Click Double Click Left Click is fired at least once on a Double Click Is there a good example that shows how to determine if the...
6
by: Martijn Coppoolse | last post by:
Hello everyone, I've got two little apps which are able to create a window that displays something on the desktop, but are not clickable. Instead, when you click on the window, the item...
0
by: marko svaco | last post by:
Hi, I have .Net VB and have worked with it, so I now some stuff in general. It woud be great if someone could help me, or give me some advice. I'm visiting a web page, so all the buttons and so...
12
by: cj | last post by:
I would like to have menu items a main menu bar that represent the days of the week. When you click on them they alternate from checked to unchecked. Right now I have 7 subs that look like this...
3
by: SteveMac | last post by:
I am using VS2003 VC++ .NET. I want to make a tray icon with a popup menu (like a context menu) that pops up for a left click. I can make the notifyicon with a context menu and that works fine,...
3
by: | last post by:
I have a control that need to perform one behavior when it's Clicked and a different behavior when it's DoubleClicked. It seems that you can't get a DoubleClick without first getting a Click. So...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.