ImageURL在更新面板中不起作用 [英] ImageURL doesn't work inside Update Panel

查看:129
本文介绍了ImageURL在更新面板中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用ASP.NET C#编写验证码身份验证程序。我面临的问题是回发期间输入错误的值会刷新图像;但是当我将它们保留在更新面板中时,同一图像不会在部分回发期间得到刷新。

I am writing a captcha authentication program in ASP.NET C#. The problem i face is that the image gets refreshed on entering wrong value during a postback; but same image doesn't get refreshed during a partial postback, when i keep them inside an update panel.

aspx源

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>            
            <asp:Image ID="ImageCaptcha" runat="server" ImageUrl="~/BringImg.aspx" /><br />
            <asp:TextBox ID="txtCaptcha" runat="server" ></asp:TextBox><br />                        
            <asp:Button ID="btnSubmit" runat="server" Text="Submit Project"  OnClick="btnSubmit_Click"/>
            </ContentTemplate>
</asp:UpdatePanel>

隐藏代码:

private System.Random rand = new System.Random();
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.Session["Captcha"] = GenerateRandomCode();
        }
    }
protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string temp = this.Session["Captcha"].ToString();
        if (string.Compare(temp, this.txtCaptcha.Text.Trim()) == 0)
        {            
            // success logic
        }
        else
        {                     
            this.lblResult.Text = "Validation Text was not correct.";
            this.Session["Captcha"] = GenerateRandomCode();
            ImageCaptcha.ImageUrl = "~/BringImg.aspx";
            ImageCaptcha.DataBind();
        }
    }


推荐答案

I我猜你的〜/ BringImg.aspx页面正在将其内容类型设置为图像,并根据该会话值生成验证码图像。在部分回发期间,图片可能没有更新,因为浏览器没有意识到图片内容已更改。有几种方法可以让浏览器知道图像已更改,但是最容易测试的方法之一是对验证码的ImageUrl应用无意义的查询字符串(每个图像不同)。

I'm guessing your ~/BringImg.aspx page is setting it's content type to an image and generating your captcha image based off that session value. The image likely isn't updating during partial postback because the browser doesn't realize that the image content has changed. There are several ways to let the browser know the image has changed but one of the easiest to test is to apply a meaningless querystring (different for each image) to the ImageUrl of the captcha.

protected void btnSubmit_Click(object sender, EventArgs e)
{
    string temp = this.Session["Captcha"].ToString();
    if (string.Compare(temp, this.txtCaptcha.Text.Trim()) == 0)
    {            
        // success logic
    }
    else
    {                     
        this.lblResult.Text = "Validation Text was not correct.";
        this.Session["Captcha"] = GenerateRandomCode();
        ImageCaptcha.ImageUrl = string.Format("~/BringImg.aspx?refresh={0}", Guid.NewGuid());
        ImageCaptcha.DataBind(); //This isn't necessary
    }
}

这篇关于ImageURL在更新面板中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆