Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > How to Add CommandEventHandler Dynamically AND CONDITIONALLY!

Reply
Thread Tools

How to Add CommandEventHandler Dynamically AND CONDITIONALLY!

 
 
haile
Guest
Posts: n/a
 
      11-24-2004
Hello all.

The body of literature in MS-Press and in this Newsgroup provides numerous
examples as to how to add event handlers dynamically by means of delegates,
namely:

private void Page_Load (o,e)
{
...
btn1.Command += new System.EventHandler(btn1_Command);
...
}

However, this does not solve the whole problem. We need a way to add
handlers conditionally. Why? Perhaps buttons are to be added to a dynamic
<asp:table> whose row count is to be determined by some user action (such as
a filter).

Since object_load events fire before the postback handlers that would
register a user action, placing the dynamic handlers in the page_load method
(or any other object load method, for that matter) does not work.

Does anyone know of a way to get a dynamic event handler to work from, say,
a button click event?

SUGGESTION:

One way to work around the problem could be to put the dynamic object
creation in the page_load event, but to make it conditional on a boolean
property, which would be set by the user action:

protected bool bMakeDynamicButton = false;

private void Page_Load (o,e)
{
if (IsPostBack && bMakeDynamicButton)
{
...
btn1.Command += new System.EventHandler(btn1_Command);
...
}
}

protected void btnUserActionButton(o,e)
{
// User has clicked to initiate construction of a dynamic control.
bMakeDynamicButton = true;
// Here's where we have to force a postback to cycle back to the page load!
}

The code above gets us halfway there, because the instruction to create the
dynamic button is set to true, but we need another postback! If there is a
way to make a button force a second postback, this workaround could succeed.

Still, even if this method can be made to work, it is a kludge. A clean
solution would be to have a method of registering a new event handler from
anywhere.

Ideas, anyone?

Happy thanksgiving,
Haile







 
Reply With Quote
 
 
 
 
haile
Guest
Posts: n/a
 
      11-24-2004
Pardon, wrote the above in a rush. Just to be clear,
the delegate in my example would be a WebControls handler, not a System
handler. Correct:

btn1.Command += System.Web.UI.WebControls.CommandEventHandler()

But the problem still stands, and is relevant to both types of delegates.

haile
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Problem with AddHandler (New CommandEventHandler) under framework 2.0 Fabrice ASP .Net 7 06-27-2009 12:26 PM
Serialize CommandEventHandler to ViewState? DC ASP .Net 2 04-15-2008 07:37 AM
dynamically add new rows and save data typed in these rows to a database Arjen Hoekstra ASP .Net 0 08-02-2005 12:15 PM
dynamically add controls and validators - always false Christian H ASP .Net 2 07-23-2003 09:07 AM



Advertisments