944,082 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 31704
  • ASP.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 12th, 2006
0

Catching Dynamic Button Events

Expand Post »
Hi!

Is there any consensus as to the best way to handle dynamic button clicks? The buttons are being created dynamically and conditionally placed, depending on business logic.

Our problem is that on clicking such a button, the postback calls the Page_Load but not the Button_OnClick. I notice that if I use a static button on the page, this problem does not occur, but that thwarts our business logic.

What we're trying to achieve is to add an "Edit" button. When clicked, it changes the layout of the page, and no longer shows this "Edit" button. (Instead, "Save", "Discard", etc...).

We're trying to avoid making the buttons static, with visibility controlled by logic, as that would be such a limiting factor. e.g. we may want a dynamic array of buttons where the size of the array is unknown at design time. How would their click events be caught?

Any ideas?!
Stewart

ASP.NET Syntax (Toggle Plain Text)
  1.  
  2. Button EditButton;
  3.  
  4. protected void Page_Load(object sender, EventArgs e) {
  5. if (!IsPostBack) {
  6. setupMostOfPage(); // based on business logic
  7. }
  8.  
  9. if (!editMode) {
  10. // creates and places the button in placeholder if not in editMode
  11. setupEditButton();
  12. }
  13. }
  14.  
  15. EditButton_OnClick(object sender, EventArgs e) {
  16. // doesn't go here even when business logic creates button
  17.  
  18. editMode = !editMode;
  19. setupMostOfPage(); // now sets it up based on editMode
  20. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
StewartS is offline Offline
1 posts
since Jul 2006
Jul 12th, 2006
0

Re: Catching Dynamic Button Events

when you create the buton are you setting up the event handler?

ASP.NET Syntax (Toggle Plain Text)
  1. this.EditButton.Click += new System.EventHandler(this.EditButton_OnClick);

Normally this created behind the scenes by visual studio.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jul 17th, 2006
0

Re: Catching Dynamic Button Events

I'm having the same problem with image buttons. The above solution works when done in something like the page_load, but in my case, the assignment of the even handler occurs in the button_click event of a pre-existing button, causing the same problem.
Reputation Points: 10
Solved Threads: 0
Light Poster
ChimpusDupus is offline Offline
47 posts
since Jul 2006
Jul 17th, 2006
0

Re: Catching Dynamic Button Events

The event handler should be in the InitialiseComponent() procedure, inside the Web Form Designer Generated Code region not in the Page_Load() procedure.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jul 20th, 2006
0

Re: Catching Dynamic Button Events

Quote originally posted by hollystyles ...
The event handler should be in the InitialiseComponent() procedure, inside the Web Form Designer Generated Code region not in the Page_Load() procedure.
Ah, but the original post asks abotu event handling of dynamic buttons, not static ones created in the Form Designer.

One partial solution is to recreate the buttons prior to the Page_Load() and place them with the page redraw. This can be done in LoadViewState() if a viewstate entry exists (use a dummy).

However, what if the button logic wants the button not to be placed for the page draw? Maybe use an invisible PlaceHolder for this.

I've had some success trying this, but sometimes the handler still doesn't get called.

Is there any fool-proof way of doing this?

John
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JohnS is offline Offline
6 posts
since Jul 2006
Jul 20th, 2006
0

Re: Catching Dynamic Button Events

I wrote this article to answer issues with dynamic server controls:
ASP.NET Conditional Dynamic Controls. It should answer your questions.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Jul 24th, 2006
0

Re: Catching Dynamic Button Events

Quote originally posted by tgreer ...
I wrote this article to answer issues with dynamic server controls:
ASP.NET Conditional Dynamic Controls. It should answer your questions.
Ok, I think I see what you're doing - conditional controls based on persisted logic. However the eventable control, the "submit" button (linkButton) still isn't conditional, which is what I'm trying to achieve.

A simplified version of what I want to do is as follows:

********************************
Initial page state:

Label->"State 1"
Button1->"Go to State 2"
(No Button2)


After clicking Button1:

Label->"State 2"
(No Button1)
Button2->"go to State 1"
********************************

This means that initially Button1 is placed on the page, while Button2 isn't. When Button1 is clicked, the page is redrawn without it, but with Button2 instead. Persisted boolean for the toggle logic.

OK, a solution to this simple version is to add both buttons and toggle visibility alternately. But what if a button sits inside another control, e.g. a cell of a row of a table, which is itself conditional? What if I want a dynamically allocated array of buttons, e.g. one per tablerow for dynamic data? Hence its placement is conditional, not just the visibility.

I'm happy to persist the entire set of pre-postback buttons so that the postback world can recreate them, but it's placement on the page that's the problem. The page layout will depend on the logic of the event handler, so no drawing should occur 'til after that.

In other words, I want to be able to catch the event of a button that may or may not be placed after the call. However, it seems ASP wants me to redraw the pre-postback page (or at least the eventable controls) in order for the handler to be called at all.

I thought a possible solution would be to place all the buttons in an invisible PlaceHolder prior to the handler call (e.g. during LoadViewState) so that they're on the page (but invisible), and then place only the visible ones after. Which ones are visible will be decided by the handler call. However, I can't get this to work reliably - events just seem to disappear into the aether. I only get reliable events if all buttons are placed once only per postback.

John
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JohnS is offline Offline
6 posts
since Jul 2006
Jul 24th, 2006
0

Re: Catching Dynamic Button Events

Quote ...
What if I want a dynamically allocated array of buttons, e.g. one per tablerow for dynamic data? Hence its placement is conditional, not just the visibility.
Add a button column to the data grid (inside the column tags) in the properties set CommandName to some meaningful string like 'Edit'

In the code behind create a protected function something like
ASP.NET Syntax (Toggle Plain Text)
  1. protected void myDataGrid_ItemCommand(object sender, DataGridCommandEventArgs e)
  2. int key = 0;
  3. {
  4. if(e.CommandName == "Edit")
  5. key = Int32.Parse(myDataGrid.DataKeys[e.Item.ItemIndex].ToString);
  6.  
  7. //do something with key.
  8.  
  9. }

In the datagrid properties set OnItemCommand event to be the handler you just coded above.

http://msdn.microsoft.com/library/de...WebControl.asp
Last edited by hollystyles; Jul 24th, 2006 at 12:25 pm.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jul 27th, 2006
0

Re: Catching Dynamic Button Events

Quote originally posted by hollystyles ...
Add a button column to the data grid (inside the column tags) in the properties set CommandName to some meaningful string like 'Edit'

In the code behind create a protected function something like
ASP.NET Syntax (Toggle Plain Text)
  1. protected void myDataGrid_ItemCommand(object sender, DataGridCommandEventArgs e)
  2. int key = 0;
  3. {
  4. if(e.CommandName == "Edit")
  5. key = Int32.Parse(myDataGrid.DataKeys[e.Item.ItemIndex].ToString);
  6.  
  7. //do something with key.
  8.  
  9. }

In the datagrid properties set OnItemCommand event to be the handler you just coded above.

http://msdn.microsoft.com/library/de...WebControl.asp

Thanks, that will indeed connect the datagrid buttons to a generic handler, but the issue is one have having to redraw the control tree (e.g. nested datagrid) before the event gets handled.

In other words, the pre-postback control tree has to be recreated and redrawn just so that the event handler gets called. This is a problem if the handler wants to change or completely replace the control tree. How do you completely replace what's already been drawn?

I don't think I'm alone in thinking this is a problem; during several years of writing J2EE webapps there was always the presumption that an event would be handled before a page draw, so that the page draw can depend on the results of the handler and isn't tied to how it was laid out before.

In ASP it looks like I'm unable to significantly change the eventable control tree, although I can change the attributes of the various controls.

I'm now going down the route of having alternate visible PlaceHolders to place the alternate control trees and page layouts that I need as a result of handling events. Doable if there are a handful of alternatives, but what if my code logic allows a million variations?

e.g. initially:
Textbox1
Button1
Table1(includes Button2)

After clicking Button1:
textbox1
Button2 (not inside table)
Button3

...while clicking Button2 leads to yet another layout, depending on time of day and the position of Jupiter's moons...:-)

It all feels like a huge kludge to implement this, unless I'm missing something obvious about how this should be done. Any ideas?

John
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JohnS is offline Offline
6 posts
since Jul 2006
Apr 30th, 2009
0

Re: Catching Dynamic Button Events

Hi,

Same problem here...

I have a thumbnail image button that opens a modal popup after a postback: modalPopup1.Show()
The modal popup has a place holder inside in which I conditionally want to place any one of these two controls:
- a static html image; or
- an image button
Which one of these two controls I need to generate I would not know until the thumbnail postback takes place.

Till here everthing is OK.

In the case when I need to create an image button, I create the imagebutton dynamically, then I set the event handler for the click event of that button and then I place the imagebutton in the placeholder. The image button is properly craeted and rendered but unfortunately, the click event is not fired for the image button I create dynamically.

I think that if I create both the static image control and the image button control beforehand (in design view) and then just change visibilty in the thumbnail click event handler logic, the dynamic imagebutton click eventhandler would execute but that is not desirable because my web page would contain extra text to be downloaded by all the clients. (Please note that in reality its not just a choice between html image <IMG> and ImageButton but a much wider choice of controls to create dynamically: html static image, image button, SWF object param and embed tags, Quiktime streaming movie, WMV streaming movie etc... so creating all these controls beforehand is not a good solution for me because the page size would grow drastically).

Any feedback would be much appreciated.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aargus is offline Offline
2 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Rich text in vs 2005
Next Thread in ASP.NET Forum Timeline: Send email if no file exists





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC