0

On Clicking the LinkButton, I refresh the image in image control without refreshing the page. For this I have used the UpdatePanel and AsyncPostBackTrigger.

It is working perfectly in chrome. But not in IE and Mozilla. On both IE and Mozilla when I click the link button, nothing happens. Look very weird. Have any clue on this ?

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:TextBox ID="txtCaptchaInput" BorderStyle="Solid" Style="vertical-align: top" runat="server" Width="106px" BorderWidth="1px"></asp:TextBox>
        <asp:Image ID="img_captcha" runat="server" Height="32px" ImageUrl="~/captchaJPEG.aspx" Width="108px" /> 
        <asp:LinkButton ID="captcha_refresh" runat="server">Refresh Image</asp:LinkButton>
    </ContentTemplate> 
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="captcha_refresh"  />
    </Triggers>
</asp:UpdatePanel>

CaptchaJPEG.aspx : Page Load

    Dim captcha As New Captcha.CaptchaImage()
    captcha.width = 150 
    captcha.height = 40 
    captcha.text = Me.Session("CaptchaText").ToString()
    captcha.GenerateImage() 
    captcha.image.Save(Me.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
Majid
  • 13,853
  • 15
  • 77
  • 113
Anuya
  • 8,082
  • 49
  • 137
  • 222
  • Check what javascript errors you get ! Also the link button is not call any function ! Maybe this is the issue ? – Aristos May 13 '13 at 13:36
  • @Aristos There are no javascript errors in Mozilla. And the Link Button has a click event which fires correctly when clicking the link button in both Mozilla and Chrome. The problem is the image does not change in browsers other than mozilla – Anuya May 13 '13 at 13:47
  • Then maybe the image stay on cache, and need to make some trick to force the update. And now that I see it better, you use a page to send an image. Better use a handler. – Aristos May 13 '13 at 13:50
  • @Aristos Okay, I have created a .Ashx file and I could see "Public Sub ProcessRequest(ByVal context As HttpContext)" But my code to generate image in .Aspx page was in pageload. Where do i put that code in this file ? – Anuya May 13 '13 at 13:55
  • You place it there on the ProcessRequest, also set the cache headers – Aristos May 13 '13 at 13:57
  • @Aristos I am not able to use the sessions and Response.OutputStream in .ASHX file. And please let me know how to set Cache headers. Code Updated above – Anuya May 13 '13 at 14:03

1 Answers1

0

Your issue have to do with the caching of the captchaJPEG.aspx that return an image. As image the browser can keep it and not change it.

To avoid that you have two ways, you can set some cache headers to say to the browser to not keep it on the cache as:

Response.Cache.SetExpires(DateTime.Now.AddDays(-10));
Response.Cache.SetMaxAge(new TimeSpan(-10, 0, 0));

Or -that I think is better- add a random number on the image tag as you set it on code behind, as:

img_captcha.ImageUrl = "~/captchaJPEG.aspx?_rnd=" + RandomNumber;

Here you can make something even better, to use the hash() of your code captcha, as:

img_captcha.ImageUrl = "~/captchaJPEG.aspx?_rnd=" + CaptachHiddenNumbers.hash();

Thats way you keep it on cache if the captcha is the same from load to load.

Now, the "correct way" is to use a handler and not a page for many reasons, like you do not want all the overhead of the page just to send an image. Now the handler come with the minimum modules call, to add session you need to use the IRequiresSessionState.

To answer to the question, why is acting different on the browsers, is because in some small details browser have different behavior, is depend how and what they check to make a decision to use the cached images and how aggressive are the browser with the cache.

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • @majidgeek I do not know about that - I need to make a test and see if it is. On the comments on the question is claim that it is. (see the first comment on the OP) And because working on one browser, then maybe is not this is the issue, the cache if for sure. – Aristos May 13 '13 at 16:33
  • @Aristos I have added the Cache expiry, as you have mentioned in your first point in page load of "captchajpeg.aspx" but even that is not working. Am i doing wrong ? – Anuya May 14 '13 at 09:15
  • @Anuya Maybe the captcha have the issue and keep the same code ? – Aristos May 14 '13 at 09:29