Create event handler for componenent that's created programaticall

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 Design?


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);
}
 
R

Richard Blewett [DevelopMentor]

Do you want to hook up an existing method or create a new one at runtime?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

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 Design?


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);
}
 
G

Guest

At this point, I'm not sure.

I'd very much appreciate your providing the code for both cases, including
the code for the eventhandler itself.

I'm still confused about how this all works.
 
G

Guest

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)'

???


Peter Jausovec said:
Hi,

Use ImageButtton1.[EventName] += new EventHandler ([eventhandler])

Regards,
Peter Jausovec
(http://blog.jausovec.net)

mg said:
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 Design?


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);
}
 
P

Peter Jausovec

Sorry, I pasted wrong code. Here is the correct one:
ImageButton btn = new ImageButton ();
btn.Click += new ImageClickEventHandler(btn_Click);

and the handler:

private void btn_Click(object sender, ImageClickEventArgs e)




Regards,
Peter Jausovec
(http://blog.jausovec.net)

mg said:
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)'

???


Peter Jausovec said:
Hi,

Use ImageButtton1.[EventName] += new EventHandler ([eventhandler])

Regards,
Peter Jausovec
(http://blog.jausovec.net)

mg said:
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 Design?


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);
}
 
G

Guest

The following code ran without error, but the alert did not appear. Can you
see the problem?

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>");
}

Peter Jausovec said:
Sorry, I pasted wrong code. Here is the correct one:
ImageButton btn = new ImageButton ();
btn.Click += new ImageClickEventHandler(btn_Click);

and the handler:

private void btn_Click(object sender, ImageClickEventArgs e)




Regards,
Peter Jausovec
(http://blog.jausovec.net)

mg said:
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)'

???


Peter Jausovec said:
Hi,

Use ImageButtton1.[EventName] += new EventHandler ([eventhandler])

Regards,
Peter Jausovec
(http://blog.jausovec.net)

"mg" <[email protected]> je napisal v sporoeilo
...

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 Design?


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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top