H
haile
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
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