H
haile
Anyone know how to load a button and its event handler to a web page as a
result of a user action? This can't be done from Page_Load( ), because the
Page_Load event fires before the user action can set a property. And yet, if
the event handler is added at any time after Page_Load, the button doesn't
respond to events. Example:
protected void btnUserClicksMe(o,e)
{
// User has just clicked me, instructing a dynamic button to appear on the
page.
Button btnDynamic = new Button();
btnDynamic += new System.EventHandler(btnDynamic_Click);
this.Controls.Add(btnDynamic); // In a practical application this would
be in a table cell or panel.
}
protected void btnDynamic_Click(o,e)
{
// This code should run if user clicks on the dynamically created button.
// But if event handler is declared at any time after Page_Load, this code
never runs,
// yet no compiler errors or exceptions are thrown.
Response.Write("Hi there!");
}
I have been attempting various workarounds to this behavior, but the only
methods that have worked have involved client-side code that forces a
multiple round trip.
Has anyone found a way to make this work?
Haile
result of a user action? This can't be done from Page_Load( ), because the
Page_Load event fires before the user action can set a property. And yet, if
the event handler is added at any time after Page_Load, the button doesn't
respond to events. Example:
protected void btnUserClicksMe(o,e)
{
// User has just clicked me, instructing a dynamic button to appear on the
page.
Button btnDynamic = new Button();
btnDynamic += new System.EventHandler(btnDynamic_Click);
this.Controls.Add(btnDynamic); // In a practical application this would
be in a table cell or panel.
}
protected void btnDynamic_Click(o,e)
{
// This code should run if user clicks on the dynamically created button.
// But if event handler is declared at any time after Page_Load, this code
never runs,
// yet no compiler errors or exceptions are thrown.
Response.Write("Hi there!");
}
I have been attempting various workarounds to this behavior, but the only
methods that have worked have involved client-side code that forces a
multiple round trip.
Has anyone found a way to make this work?
Haile