使用ASP.NET AJAX的UpdatePanel与处理程序
-
11-09-2019 - |
题
即时通讯使用的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);
}
解决方案
尝试用于处理高速缓存选项如
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();
其他提示
是对UpdatePanel内的图像?我把它放在那里,如果它不是。此外,每次面板更新,确保了独特的图像的URL,以便客户端(浏览器),或者代理不能使用缓存的图像。一个GUID上涨了作为查询字符串参数应达到目的(例如YourImageHandler.ashx?XXXXXXXXXXXX-XXXXXXXX-XXXXXXXXXXXX)。
天使,
在图像没有被再次请求从服务器,所以没有该处理程序来执行机会。要获得再次执行时,必须使浏览器再次检索图像。
这是说,在你的异步回发,如果没有对图像进行更新,浏览器将没有理由不管缓存设置再次读取它,等等。
您可以用脚本解决这个问题,而是一个更简单的方法是采取迈拉的意见,并追加一个GUID到图像的查询字符串和变化是GUID 的为您的异步回发的一部分。这将需要更新客户端上的图像的URL,并迫使它返回到服务器,以获得新的形象。
如果这不能解决问题,请发表你的web.config那也请求映射到图像处理程序,您使用的图像添加到页面的代码部分。