Create event handler for componenent that's created programaticall

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

The code below creates an ImageButton when a LinkButton is clicked.

How can I create an event handler for this ImageButton which can't be seen
in the Design pane?


private void LinkButton1_Click(object sender, System.EventArgs e)
{
System.Web.UI.WebControls.ImageButton ImageButton1 = new ImageButton();
ImageButton1.Height = 18;
ImageButton1.Width = 100;
ImageButton1.ImageUrl = @"\images\continue.gif";
Panel1.Controls.Add(ImageButton1);
}
 
C# should be like this:

this.ImageButton1.Click += new System.EventHandler(this.ImageButton1_Click);
 
this.ImageButton1.Click += new System.EventHandler(this.ImageButton1_Click);

resulted in the following error message:

'Page' does not contain a definition for 'ImageButton1'

I then rewrote the line as follows:

ImageButton1.Click += new System.EventHandler(ImageButton1_Click);

This code resulted in the following error message:

ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
does not match delegate 'void System.EventHandler(object, System.EventArgs)'

???
 
The code below runs without error but the Alert does not appear. Can you
tell me how to make the Alert popup when I click the ImageButton?

private void LinkButton1_Click(object sender, System.EventArgs e)
{
System.Web.UI.WebControls.ImageButton ImageButton1 = new ImageButton();
ImageButton1.Height = 18;
ImageButton1.Width = 100;
ImageButton1.ImageUrl = @"\images\continue.gif";
Panel1.Controls.Add(ImageButton1);
ImageButton1.Click += new ImageClickEventHandler(ImageButton1_Click);
}

private void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Response.Write("<script language='javascript'>alert('TEST');</script>");
}
 
Back
Top