How to detect a OnClick on image in httphandler? RRS feed

  • Question

  • User522004677 posted


    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(); 
        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();
                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);
                queue = new MessageQueue(queuePath);
            queue.MessageReadPropertyFilter.AppSpecific = true;
            using (MessageQueueTransaction mqt = new MessageQueueTransaction())
                // 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.
                // 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);

    Sunday, July 26, 2009 7:52 PM

All replies

  • User522004677 posted


    the answer is not relevant to questn. I couldnt find any relevant data on the links you just send.

    Thursday, July 30, 2009 2:17 PM
  • User-1055694378 posted

    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.

    Thursday, August 6, 2009 9:49 PM
  • User-132226094 posted

    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

    function addToQueue(someparam)
        url: 'clickhandler.ashx',
        type: 'POST',
        timeout: 1000,
        error: function(){
           //do whatever
        success: function(xml){
            // do whatever

    Friday, August 21, 2009 8:31 PM
  • User1696475319 posted

    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. 

    Saturday, August 22, 2009 9:51 AM