Search code examples
asp.netupdatepanelcaptcha

Refresh Captcha Code with Updatepanel control in ASP>NET


I have a captcha code in my site. sometimes needs to chenge that picture , but i dont want the whole page refreh. i used updatepanel in this way :

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Label ID="lblmsg" runat="server" Font-Bold="True"
                    ForeColor="Red" Text=""></asp:Label>
                <asp:TextBox ID="txtimgcode" runat="server"></asp:TextBox>
                <asp:Image ID="Image1" runat="server" ImageUrl="~/CImage.aspx" />
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Check" />


                <asp:Button ID="Button3" runat="server" Text="refresh" OnClick="Button3_Click" />
            </ContentTemplate>  
        </asp:UpdatePanel>

Here is Button3 code behind :

protected void Button3_Click(object sender, EventArgs e)
    {
        Image1.ImageUrl = "~/CImage.aspx";
    }

Button1 Works correctly But Button3 which should change the captcha picture doesn't work.

Am I missing something?

In addition if Iwant to use a refresh image instead of Button3,What controls should I use?


Solution

  • The image likely isn't updating during partial postback because the browser doesn't realize that the image content has changed since it been targeted to same page over and over. Adding querystring might help in this case one need to pass random id as querystring to the ImageUrl of the captcha.

    protected void Button3_Click(object sender, EventArgs e)
    {
        Image1.ImageUrl = string.Format("~/CImage.aspx?img={0}", Guid.NewGuid());
    }