Archives

Archives / 2008 / January
  • Stop IE Flickering Between Pages

    I dont know if anyone has noticed but in IE when you click between pages in your site that have common elements i.e. a header. Your page will flicker for a brief moment. But in FireFox the page will not flicker and it will look smooth. I came across a fix for IE that will make this smooth just like in firefox.


    To use simply add the following 2 tags into your <head> section on your page:

  • Gotcha with linq and paging

    Hey All,

    Had a query which I was paging on the front end. I knew that a certain product was meant to be in my display but could not see it. But on page 2 a product would repeat itself. Odd, got into profiler and looked at the queries. The first page would get a select top 9 which would not do any orderby, the next page would have a query like so: SELECT ROW_NUMBER() OVER (ORDER BY [t15].[test], [t15].[ID], [t15].[CreatedDate] which was ordering by all my columns.

    So I added an orderby to my LINQ query which ordered my results by ProductName, then the paging was working as expected. Had another look in profiler and now I had what I expected.


    So a warning to all, make SQL Profiler your best friend because if you are not careful you could get spanked by LINQ.


    Thanks

    Stefan

  • Issues using HyperLink control with an image in a URL Rewriting scenario

    Hey All,

    I was using the HyperLink control in my page and added URL Rewriting to my page so that for example:

    http://site/test/test/default.aspx would be rewritten to http://site/default.aspx. I had a HyperLink control on the default page like so:

    <asp:HyperLink ID="hl1" runat="server" NavigateUrl="~/test.aspx" ImageUrl="~/test.jpg" Text="A"></asp:HyperLink>

    And I was getting the dreaded error: Cannot use a leading .. to exit above the top directory...

    Looking into this a little more if you remove the ImageURL property or even set it to be /test.jpg it will work fine, so conluded it must be something to be with the ImageUrl property in the control. Digging into reflector it looks like that when the HyperLink control renders it creates a new Image object and sets the ImageUrl of that to be ResolveClientUrl(url), internally the Image control also does a ResolveClientUrl again on that URL and I think this is where the issue is.

    My solution was to create a FixUrl method in my utility class which would convert replace ~/ in a url with the current application base path hence giving me an absolute URL. This is also handy to use in your pages to set the urls of your CSS, JS etc.


    Public Shared Function FixUrl(ByVal inURL As String) As String
            Return If(inURL.StartsWith("~"), _
                      HttpContext.Current.Request.ApplicationPath & inURL.Substring(1), _
                      inURL).Replace("//", "/")
    End Function


    I would now assign my ImageUrl property in code i.e. img1.ImageUrl = FixURL("~/test.jpg") and this now works well.


    If anyone has come past this before suggestions or ideas they would be appreciated.


    Thanks

    Stefan

  • EventValidation issues with asp.net ajax and FireFox caching causing issues...

    Was having some wierd issues with asp.net ajax in firefox. If for example you put a textbox and a button in an updatepanel and put in a value and click the button. Then you hit F5, in IE the value will be gone, but in FireFox the value is still there. This is due to firefox caching input values. The only way to clear that textbox in FF is to do a ctrl + F5. I had an issue in a paged datagrid where if you paged to say page 5, hit F5 it would be at page 1, hit nextpage and it would be back at 5. This had me stumped until I realised that firefox is caching the values and most likely the __VIEWSTATE hidden field is being cached and this is causing the issues. So I added this to my page load:

    Response.Cache.SetNoStore()


    This made firefox work but this turns off client caching which might not be a good idea. A developer (Steve) at work had a form with a button and textbox which in FF would not remember the value, this was a real WTF moment. I got his code and double checked the differences between them. The only thing different was that in his form he had autocomplete="off" set on the form due to using an autocomplete extender. I added this to my form and presto it was working perfectly. This was ok but you might not want to turn autocomplete off for your whole form so what can we do:

    Solution:


    Thinking about this I thought of what fields could affect this. Looking at a page you have the following hidden fields:

    __VIEWSTATE, __EVENTTARGET, __EVENTARGUMENT, __EVENTVALIDATION.


    I decided to try and turn of autocomplete for only these fields using some script at the bottom of my page:

    <script>
           function setAutoCompleteOff(id) {
                var elem = document.getElementById(id);
                if(elem) {
                    elem.setAttribute('autocomplete', 'off');
                }           
            }
           
            setAutoCompleteOff('__VIEWSTATE');
            setAutoCompleteOff('__EVENTTARGET');
            setAutoCompleteOff('__EVENTARGUMENT');
            setAutoCompleteOff('__EVENTVALIDATION');
    </script>


    Then I turned autocomplete back on on the form, and gave it a go, and to my supprise it worked perfectly. Now FF was not using sending over the cached values on hitting F5 and was using the original values. This meant my page works fine. One more thing is you can now turn on EnableEventValidation on your page and it seems to work fine now.

  • PNG Fix Component

    Needing a solution to fix transparent PNG in IE6 I came to start using one of the many javascript based solutions to do this. But when it came time to use ajax and populating new images in my postbacks this obviously broke. So I created a custom ASP.NET Component to help me do this...