1

我将 UpdatePanel 用于一些专门用于验证码的控件,因此,当 AsyncPostBack 由按钮“btnQuery”触发时,我如何告诉 .ashx(验证码处理程序)自行刷新它?

我使用会话将验证码上的图像验证为图像下方输入的 Num

这是处理程序:

<%@ 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; } }
}

我想刷新图像..不只是这样做:

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

3 回答 3

2

尝试处理程序的缓存选项,例如

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

如果上面的方法不起作用,那么我剩下的想法就是用 queryString 调用处理程序,这样每次调用时图像源都不相同
Image1.ImageUrl = "Handler.aspx?guid=" + Guid.NewGuid();

于 2009-08-13T22:44:48.910 回答
1

图像在 UpdatePanel 内吗?如果不是,我会把它放在那里。此外,每次面板更新时,请确保图像的 url 唯一,以便客户端(浏览器)或代理不会使用缓存的图像。作为查询字符串参数添加的 Guid 应该可以解决问题(例如 YourImageHandler.ashx?xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。

于 2009-08-12T16:18:57.737 回答
1

天使,

不再从服务器请求图像,因此处理程序没有机会执行。要让它再次执行,您必须让浏览器再次检索图像。

也就是说,在您的异步回发中,如果没有更新图像,浏览器将没有理由再次获取它,而不管缓存设置等如何。

您可以使用脚本解决此问题,但更简单的方法是采纳 Myra 的建议并将 GUID 附加到图像的查询字符串中,并将该 GUID作为异步回发的一部分进行更改。这将更新客户端所需图像的 URL,并强制它返回服务器以获取新图像。

如果这不能解决问题,请发布将请求映射到图像处理程序的 web.config 部分以及用于将图像添加到页面的代码。

于 2009-08-16T17:25:05.957 回答