UpdatePanel working only on Chrome and not in IE / Mozilla. Why? - asp.net

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)

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.

Related

Is there a way to force page position on a website that has an update panel that refreshes every 15 seconds?

I have a web control that is a long form that users fill out. There are many checks along the way, lots of scrolling and lots of postbacks.
For every button click or postback action (radio select, dropdown changes) I use this in my code behind:
Page.MaintainScrollPositionOnPostBack = True
Which works as expected. It scrolls back to that button or event that called it.
However, on the web control, there is an Update panel:
<asp:PlaceHolder runat="server" ID="plFingerprint" Visible="false">
<tr>
<td colspan="2" class="l">
<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<uc1:Listener id="tidListener" runat="server" />
<a href="FingerBio:match,--=7392=--,<%= tidListener.SessionID.ToString() %>,<%= ReplyIndex.BaseSiteUrl %>">
<img id="Img1" runat="server" src="~/images/finger.png" class="scan"
alt="Scan Finger" /></a>
<asp:Label ID="matchlabel" runat="server"></asp:Label>
<div style="display: none">
<asp:Label ID="matchIDLabel" runat="server"></asp:Label></div>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</asp:PlaceHolder>
This is essentially a fingerprint scanner that refreshes every 15 seconds to get any new fingerprints captured. What basically happens is that the user will be capturing data well down the form, when the interval kicks in, the panel is updated and the screen is positioned back to the top (which is where the update panel sits BTW). It reads data from a web service linked to the site.
Now this is frustrating to say the least. Users complain that the screen jumps all the time.
I have the
Page.MaintainScrollPositionOnPostBack = True
everywhere it needs to be, but that doesn't stop the update panel from taking over.
I would like to use anchors, but that will not work with our website structure.
Is there anything else I can attempt? I would like the update panel to continue to update as normal, every 15 seconds, without repositioning the screen each time.
Could this be done in Javascript? Code behind? Something as simple as a change on the form itself?
EDIT
This is on that control itself:
<asp:Timer ID="timerTid" runat="server" Interval="1500" OnTick="timerTid_Tick">
Could I set the interval in code, after the user has or has not used this facility?
The best thing would be to update only what is needed. But I think there's an other option. You could try and use Sys.WebForms.PageRequestManager.getInstance. Which are js functions that are called when there's an update to the UpdatePanel. I haven't used it a lot, I think it would look like this.
<script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function BeginRequestHandler(sender, args)
{
// Get current scroll position
}
function EndRequestHandler(sender, args)
{
// Set scroll position back to where it was
}
</script>
You might have a problem if new record are added to the top of your grid.
Managed to solve my issue by adding this to my ScriptManager
LoadScriptsBeforeUI="true"
That makes all scripts load up, so the control gets it data and refreshes, but without all the scrolling.

Listbox not causing post back in Chrome and Safari

In chrome and safari I am having problems with a list box I have. The data is loaded on the backend initially which works fine. But then on the event SelectedIndexChange I want to figure out what they selected and update my panel with that info. Problem is chrome and safari is not calling the event. Ive checked for script erros and get none. IE and FF work just fine, so I am just wondering what is wrong with those browsers and what I can do to fix this. Basically this is a control that sits inside a sharepoint 2007 page.
<asp:UpdatePanel ID="ISudpl" runat="server">
<ContentTemplate>
<strong>Product</strong><br /><asp:ListBox runat="server" ID="Product"
OnSelectedIndexChanged="Filter_SelectedIndexChanged" EnableViewState="true" AutoPostBack="true"
SelectionMode="Multiple" Rows="15" CssClass="designIntent"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Product" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
I think this is a documented issue with Microsoft AJAX and webkit based browsers check here http://blog.turlov.com/2009/01/aspnet-ajax-compatibility-patch-for.html and here Asp.Net, DropDownList, AutoPostBack and Google Chrome for a possible solution
You do not need Trigger because your control(ListBox) is inside same UpdatePanel. Please remove the complete <Triggers></Triggers> block from your code and check if it works in Chrome?

Weird behavior of linkbutton within updatepanel

This problem is relevent apperantly to IE6 and maybe to newer versions im not sure. when you put a linkbutton inside an updatepanel like so:
<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:LinkButton runat="server">Test</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
You run it and then play around with ctrl+mouse wheel and change the size of the site. Then click the linkbutton. It changes the size of the text of the website again to somekind of deffault size... Im wondering if someone knows how to fix this. And what is causing this problem
Edit: the problem was with a few users clicking the link and suddenly all the text on the page became very small and unreadable
You have nothing to fix here, this is a browser behavior.
But if you have a custom behavior that you change the text size, then you need to take care to save the last setting somewhere and then get the last setting and use it.

ASP.Net: asynchronous image upload displayed using handler

I have a really specific question for which I don't think I will have an answer but... let's try!
I have an aspx web page that works perfectly with Firefox and Chrome but not on IE9 (don't feed the troll ^^).
My objective is to allow an asynchronous image upload using ASP AjaxControlToolkit (see http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/asyncfileupload/asyncfileupload.aspx for a demo). When the image asynchronous upload is finished, the UploadedComplete function is called and I put the image in a session variable :
if (fileSizeOk && fileTypeOk)
Session["image"] = this.AsyncFileUploadLogo.FileBytes;
In parallel I have a handler responsible for returning an image from the session variable :
byte[] buffer = (byte[])context.Session["image"];
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.OutputStream.Flush();
Then, in my aspx page, I have an asp:Image field in a UpdatePanel that asks to the handler to get the image every five second :
<asp:UpdatePanel ID="UpdatePanelLogo" runat="server">
<ContentTemplate>
<asp:Image ID="ImageFileUploaded" runat="server" ImageUrl="AsyncImageHandler.ashx" />
<asp:Timer ID="TimerFileUploaded" Interval="5000" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
When the user validates the form, I just have to get the variable from the session and I save the image in database with the other values.
Here is my problem: with Firefox and Chrome, the timer provokes the reloading of the image but with IE (even IE9) the image is only displayed after hitting F5. When I put a breakpoint in the handler I can see that it's never called with IE (maybe a caching mechanism?).
Do you have an idea of ​​how to solve this problem?
Thanks for your answers!
Found the solution using a classic Web dirty trick:
this.ImageFileUploaded.ImageUrl = "AsyncImageHandler.ashx?p=" + Environment.TickCount.ToString();
I added that line in the PageLoad(), this last is called on each post back triggered by the timer :)

Firefox and Updatepanel

I have a problem with FireFox and ASP.NET UpdatePanel.
I have in a form a checkbox and an UpdatePanel. When I check the checkbox, an asp:panel which is into the UpdatePanel should become visible.
<asp:CheckBox ID="cbMoreOptions" runat="server" Text="plus d'options" AutoPostBack="True" OnCheckedChanged="cbOptions_CheckedChanged" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" Visible="false">
sssssssss
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="cbMoreOptions" EventName="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>
Everything is working fine but not after I refresh the page while the checkbox is checked. If this is happening, the checkbox rest checked the page doesn’t make postback more to the server. The firebug shows that the page gets a response and when I verify the its content I have en error 500 which tell that the information of the page is corrupt. All this is happening only in Firefox. In IE8 and Google Chrome everything is ok.
Does anybody have an idea how can I avoid this? It’s a bug of Firefox?
All the weird comportment continues until I make enter into the URL textbox. Even if I make F5 nothing happens. What is the difference between F5 and enter into the URL? They shouldn’t have the same result?
Thanks a lot.
Have you tried placing the checkbox "cbMoreOptions" inside the UpdatePanel?
Place in inside the UpdatePanel ContentTemplate section and set an AsyncPostBackTrigger for the CheckBox.
We had this problem a long time ago and it was the way firefox uses in-memory cache to store the state of a page which messes up the way ASP.NET handles VIEWSTATE hidden field. Check if caching turned on for your aspx pages and disable it.
Edit: Here's a link that explains it in detail.
What is the difference between F5 and enter into the URL? They
shouldn’t have the same result?
If you hit enter in the address bar, it does a GET request, i.e. you enter your page's lifecycle with !IsPostBack.
When you hit F5, I observed that firefox will repeat the last request, so if that last request was a POST, it will repeat it. That's why your page stay in a buggy state. If the first post back caused an error (the checkbox' checkedChanged), hitting F5 will just repeat that error.
So, no, the don't have the same result.
I don't have any documentation to backup this statement, but this is what I observed. If anyone can point a source I'll be glad to see it.

Resources