Solved

C# and this.Events.AddHandler

Posted on 2004-08-28
2
6,759 Views
Last Modified: 2008-01-09
I am writing a program that adds images to the web page at runtime
by using the following commands

(Namespace : System.Web.UI.Page)

HtmlImage h=new HtmlImage();
h.Attributes.Add("style","position:absolute; left:150; top:200");
h.Src="bb/00000.jpg";
this.Controls.Add(h);

When the user clicks on the image i added i want to redirect the page to www.x.com

I have written the function below to handle this event, but i need to add an event handler to
the image "h" so that the function is executed

private void h_Click(object sender, System.EventArgs e)
{
 Response.Redirect("www.x.com");
}

this.Events.AddHandler(...............);

Please Can you tell me exactly what parameters to pass to the above procedure

Thanx.
0
Comment
Question by:BrettLombardo
2 Comments
 
LVL 10

Accepted Solution

by:
jnhorst earned 500 total points
ID: 11924219
Got a better idea:

HtmlImage h = new HtmlImage();
h.Attributes.Add("style","position:absolute; left:150; top:200");
h.ATtributes.Add("onclick", "javascript:window.location.href=\"http://www.x.com\";");
h.Src="bb/00000.jpg";
this.Controls.Add(h);

This will accomplish exactly the same thing, unless you need to something else before calling Response.Redirect().

John
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 11924790
To capture events, you should use the asp WebControls instead of the HtmlControls.

Try using an ImageButton, which has an OnClick event.



(Also, a side note, avoid setting the style attribute explicitly.  You can use the Style property of the control instead to add name/value paris of styles to the controls style collection.  Even better, use css classes, so you don't need to recompile your code just to change the style of an image.)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now