Solved

C# and this.Events.AddHandler

Posted on 2004-08-28
2
6,785 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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