Question

Im using UpdatePanel for some controls specially for captchas so, when a AsyncPostBack is performed triggered by a button "btnQuery", How can I tell to the .ashx (Handler for Captcha) to refresh it self?

Im using session to validate the Image on Captcha to the Num on the input below the image

this is the Handler :

<%@ WebHandler Language="C#" Class="captcha" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;

public class captcha : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/GIF";

        Bitmap imagen_GIF = new System.Drawing.Bitmap(80, 30);
        Graphics grafico = System.Drawing.Graphics.FromImage(imagen_GIF);
        grafico.Clear(Color.Gainsboro);

        Font tipo_fuente = new Font("Comic Sans", 12, FontStyle.Bold);

        string randomNum = string.Empty;
        Random autoRand = new Random();

        for (int x = 0; x < 5; x++)
        {
            randomNum += System.Convert.ToInt32(autoRand.Next(0, 9)).ToString();
        }
        int i_letra = System.Convert.ToInt32(autoRand.Next(65, 90));

        string letra = ((char)i_letra).ToString();
        randomNum += letra;

        context.Session["RandomNumero"] = randomNum;

        grafico.DrawString(randomNum, tipo_fuente, Brushes.Black, 5, 5);

        imagen_GIF.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

        tipo_fuente.Dispose();
        grafico.Dispose();
        imagen_GIF.Dispose();
    }

    public bool IsReusable { get { return false; } }
}

I want to refresh the image.. not just doing this :

   public void postbackear()
    {
        string script = string.Format("Sys.WebForms.PageRequestManager.getInstance()._doPostBack('{0}', '');",
                            btnConsulta.ID);
        ScriptManager.RegisterStartupScript(this.Page, typeof(string), "Refresh", script, true);
    }
Was it helpful?

Solution

Try caching options for handlers such as

context.Response.Cache.SetExpires(DateTime.Now); context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.Cache.SetValidUntilExpires(false);

If above not works only idea I got left is calling handler with queryString so that image source is not same any time you call
Image1.ImageUrl = "Handler.aspx?guid=" + Guid.NewGuid();

OTHER TIPS

Is the image inside the UpdatePanel? I'd put it there if it isn't. Also, each time the panel updates, make sure the url for the image unique so that clients (browsers) or proxies won't use a cached image. A Guid tacked on as a querystring parameter should do the trick (e.g. YourImageHandler.ashx?xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

Angel,

The image is not being requested again from the server, so there is no opportunity for the handler to execute. To get that to execute again, you must make the browser retrieve the image again.

That said, in your async postback, if nothing about the image is updated, the browser will have no reason to fetch it again regardless of cache settings, etc.

You could solve this with script, but an easier way is to take Myra's advice and append a GUID to the query string of the image and change that GUID as part of your async postback. This will update URL of the image needed on the client and force it to go back to the server to get the new image.

If this does not solve the problem, please post the section of your web.config that maps requests to the image handler as well as the code you use to add the image to the page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top