D
Donald Xie
Hi,
I noticed an interesting effect when working with controls that are
dynamically loaded. For instance, on a web form with a PlaceHolder control
named ImageHolder, I dynamically add an image button at runtime:
//----- Code snippet
protected System.Web.UI.WebControls.PlaceHolder ImageHolder;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
LoadDynamicImageButton();
}
private void ImageButton_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
Response.Write("Image button clicked");
}
private void LoadDynamicImageButton()
{
ImageButton btn = new ImageButton();
btn.ImageUrl = "Images/Minus.png";
btn.Click += new ImageClickEventHandler(ImageButton_Click);
ImageHolder.Controls.Add(btn);
}
//----- End code snippet
The first time the page loads, the image button is created. When I click it,
it reloads the page but doesn't trigger its ImageClickEventHandler. If I
forcefully run LoadDynamicImageButton(), i.e., remove the IsPostBack check
in Page_Load() to recreate the image, its ImageClickEventHandler is
triggered. This happens with the Command event as well.
It seems that on post-back, if a dynamically loaded control is not loaded,
then its event handler is not wired. Its properties such as CommandName and
CommandArgument, OTOH, persists from the last load.
The problem is that if I dynamically load up the PlaceHolder control with
different controls according to the button clicked, this behavior forces all
those controls to be loaded twice - first to load all default controls just
to wire up their event handlers, then to reload them again according to the
event. There are also other factors, such as the assigning a differnet
ImageUrl, that seem to affect whether or not the event handler is triggered.
I'm still working on it to hopefully isolate the problem.
Is there a workaround?
Thanks,
Donald Xie
I noticed an interesting effect when working with controls that are
dynamically loaded. For instance, on a web form with a PlaceHolder control
named ImageHolder, I dynamically add an image button at runtime:
//----- Code snippet
protected System.Web.UI.WebControls.PlaceHolder ImageHolder;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
LoadDynamicImageButton();
}
private void ImageButton_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
Response.Write("Image button clicked");
}
private void LoadDynamicImageButton()
{
ImageButton btn = new ImageButton();
btn.ImageUrl = "Images/Minus.png";
btn.Click += new ImageClickEventHandler(ImageButton_Click);
ImageHolder.Controls.Add(btn);
}
//----- End code snippet
The first time the page loads, the image button is created. When I click it,
it reloads the page but doesn't trigger its ImageClickEventHandler. If I
forcefully run LoadDynamicImageButton(), i.e., remove the IsPostBack check
in Page_Load() to recreate the image, its ImageClickEventHandler is
triggered. This happens with the Command event as well.
It seems that on post-back, if a dynamically loaded control is not loaded,
then its event handler is not wired. Its properties such as CommandName and
CommandArgument, OTOH, persists from the last load.
The problem is that if I dynamically load up the PlaceHolder control with
different controls according to the button clicked, this behavior forces all
those controls to be loaded twice - first to load all default controls just
to wire up their event handlers, then to reload them again according to the
event. There are also other factors, such as the assigning a differnet
ImageUrl, that seem to affect whether or not the event handler is triggered.
I'm still working on it to hopefully isolate the problem.
Is there a workaround?
Thanks,
Donald Xie