Hello,
I created a new httphandler object that on every call it loads an image and notify to a messae quoe that the image viewed.
Now what I need to do is to notify to the quoe whenever the image being clicked.
How do I do that? here is the relevant code:
public class banner : IHttpHandler {
public void ProcessRequest (HttpContext context) {
int iAffiliateId;
iAffiliateId = Convert.ToInt32(context.Request.QueryString["id"]);
context.Response.ContentType = "application/js";
//-----------------------------------Get Banner Details---------------------------------//
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(context.Server.MapPath("~/bannerexchange/admin/banners.xml"));
string xpath = "//Advertisements//Branch//Ad[Keyword='728x90']";
XmlNodeList root = XmlDoc.SelectNodes(xpath);
Random rnd = new Random();
int iRandom = rnd.Next(root.Count);
string s=root[iRandom].InnerText;
//---------------------Retrieve the ad details------------------------------------------//
string ImageUrl = root[iRandom]["ImageUrl"].InnerText;
string NavigateUrl = root[iRandom]["NavigateUrl"].InnerText;
string AlternateText = root[iRandom]["AlternateText"].InnerText;
string Impressions = root[iRandom]["Impressions"].InnerText;
string Clicks = root[iRandom]["Clicks"].InnerText;
string Keyword = root[iRandom]["Keyword"].InnerText;
//--------------------------------------------------------------------------------------//
context.Response.RedirectLocation = NavigateUrl;
//string path = context.Server.MapPath("~/bannerexchange/Banners/banner01.gif"); //req.PhysicalPath;
string extension = null;
string contentType = null;
extension = Path.GetExtension(ImageUrl).ToLower();
NotifyMessageQue();
ImageUrl=context.Server.MapPath("~/bannerexchange/Banners/banner01.gif");
context.Response.StatusCode = 200;
context.Response.ContentType = contentType;
context.Response.ContentType = "text/plain";
context.Response.Write(string.Format("document.write('<a href=" + NavigateUrl + " target=\"_blank\" border=0><img src=\"http://test.com/bannerexchange/Banners/banner01.gif\" alt=\"\" /></a>');"));
}
}
public bool IsReusable {
get {
return false;
}
}
public void NotifyMessageQue()
{
// Check if queue alreasy exists.
string queuePath = @".\private$\BannerExchange";
MessageQueue queue;
if (!MessageQueue.Exists(queuePath))
// If not, create one.
queue = MessageQueue.Create(queuePath);
else
queue = new MessageQueue(queuePath);
queue.MessageReadPropertyFilter.AppSpecific = true;
using (MessageQueueTransaction mqt = new MessageQueueTransaction())
{
mqt.Begin();
// Send something to queue.
//DateTime dt = DateTime.Now;
string IP = HttpContext.Current.Request.UserHostAddress;
Message sendMsg = new Message(IP, new BinaryMessageFormatter());
//sendMsg.Label = "ViewBanner";
sendMsg.AppSpecific = (int)MessageType.Normal;
queue.Send(sendMsg, "ViewBanner");
// Wait for five seconds.
/*
System.Threading.Thread.Sleep(5000);
// Get sent message from queue.
Message receiveMsg = queue.Peek();
receiveMsg.Formatter = new XmlMessageFormatter(
new Type[] { typeof(DateTime) });
DateTime ret = (DateTime)receiveMsg.Body;
Response.Write("Message Received: " + ret);
*/
// Send the message.
queue.Send(sendMsg, mqt);
mqt.Commit();
queue.Close();
}
}
}
It sounds like you need to use Ajax or do complete postback with redirect to the navigateURL afterwards, but that feels messy. So I would go for Ajax.
One way to do it is:
When your image is generated, add OnClick event with a function call that will use Ajax to call your handler. It almost feels like you would need another handler to process OnClick events, but you could probably use your existing one and add some switches
to differentiate between calls for image generation and onClick calls.
I guess you need some ID to tie the onClick call to the handler with the queue started when it was originally generated. So for example, passing an ImageID back to the queue with the OnClick call will achieve that.
Hope that helps.
Please click "Mark as Answer" button if this post answered your question.
Handlers are unaware of usage - all they know is that something has been requested, so here it is. It's not a web page in and of itself, so it can't capture clicks. You can only get the 'users' of that handler to implement these things. In this case,
it's the web pages which have to handle this.
Member
151 Points
901 Posts
How to detect a OnClick on image in httphandler?
Jul 26, 2009 07:52 PM|megetron|LINK
Participant
1403 Points
333 Posts
Re: How to detect a OnClick on image in httphandler?
Jul 26, 2009 11:42 PM|vora_bhaumik|LINK
Check the below links,
you can try this way <a href="path" > <img /> </a>
http://www.codeproject.com/KB/web-image/ThumbViewerControl.aspx?fid=385368&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=51&select=2067707
http://www.zimbio.com/AJAX+Asyncronous+Javascript+and+XML/articles/65/Use+Javascript+HttpHandler+Load+Image+Database
http://byatool.com/index.php/ui/use-javascript-and-an-httphandler-to-load-an-image-from-a-database/
http://msdn.microsoft.com/en-us/magazine/cc302030.aspx
Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
Regards,
Bhaumik
Member
151 Points
901 Posts
Re: How to detect a OnClick on image in httphandler?
Jul 30, 2009 02:17 PM|megetron|LINK
the answer is not relevant to questn. I couldnt find any relevant data on the links you just send.
Member
10 Points
9 Posts
Re: How to detect a OnClick on image in httphandler?
Aug 06, 2009 09:49 PM|vitaliid|LINK
It sounds like you need to use Ajax or do complete postback with redirect to the navigateURL afterwards, but that feels messy. So I would go for Ajax.
One way to do it is:
When your image is generated, add OnClick event with a function call that will use Ajax to call your handler. It almost feels like you would need another handler to process OnClick events, but you could probably use your existing one and add some switches to differentiate between calls for image generation and onClick calls.
I guess you need some ID to tie the onClick call to the handler with the queue started when it was originally generated. So for example, passing an ImageID back to the queue with the OnClick call will achieve that.
Hope that helps.
None
0 Points
2 Posts
Re: How to detect a OnClick on image in httphandler?
Aug 21, 2009 08:31 PM|vsuser|LINK
right..if you want to handle clicks and do some processing at the backend, then you need to notify the server of the clicks.
Which means, either full postback or make an AJAX request.
If you are already using Atlas then it should be easy otherwise, use a library like jquery Making an AJAX call should be fairly simply
1) In the <a> add OnClick="return addToQueue(someparam);"
2)Psuedo code
Verisoul.com is best .NET based Free Online Dating service
Star
12324 Points
2769 Posts
Re: How to detect a OnClick on image in httphandler?
Aug 22, 2009 09:51 AM|mendhak|LINK
Handlers are unaware of usage - all they know is that something has been requested, so here it is. It's not a web page in and of itself, so it can't capture clicks. You can only get the 'users' of that handler to implement these things. In this case, it's the web pages which have to handle this.