Friday, 13 March 2009

An Improved MsgBox for Asp.Net

Often times a simple messagebox is required at the client under ASP.Net. This is easy to do by sending back a JavaScript Alert(). However, this does not work so well if the user navigates around using the back and forward buttons, as they will see the message multiple times. Also, we would like our msgbox to be usable from AJAX enabled apps, so that an asynchronous postback can also trigger an alert.

The below vb.net 3.5 code meets these criteria:




Module PageExtensions
 
    <extension()> _
    Public Sub MsgBox(ByVal aPage As Page, ByVal aMessage As String)

        Dim timeStr As String = "msgbox" & Date.Now().Ticks.ToString

        ' Set a cookie after showing alert so as will not re-show if back button or refresh is used.

        Dim alertScript As String = "if (document.cookie.indexOf('" & timeStr & "') == -1) {"
        alertScript &= "alert(""" & aMessage & """);"
        alertScript &= "document.cookie = '" & timeStr & "=deleted';}"

        System.Web.UI.ScriptManager.RegisterStartupScript(aPage, GetType(String), aMessage, alertScript, True)

    End Sub

End Module


To use within a page, you can now simply type:
MsgBox("Hello World"), just like in a Windows App.

A temporary cookie is set for each msgbox invoked so that the msgbox will only be shown once. The method is an extension method of the Page class, so needs to be contained within a Module, or used without the attribute.

Note also you will need a reference to System.Web.Extensions in your project.

Wednesday, 1 October 2008

EazyCaptcha.com Simple Captcha Server

As a programming exercise I created a public captcha server that can be used to generate captchas without requiring any software libraries or complex programming by the developer.

You can try it out here...

www.eazycaptcha.com

Sunday, 28 September 2008

VegePlan

VegePlan is a pet .Net project I have been working on to get up to speed on AJAX and LINQ.

It is a basic drag-n-drop vegetable garden planner and designer.

I have openend it up for anyone to use here:

www.vegeplan.com

I have used the JavaScript Prototype library for the AJAX, and YUI and the Ajax Control Toolkit for the drag-n-drop and other UI functionality.

Thursday, 8 May 2008

Homebrew Google Position finder for Keywords / Domain

SEO sites like http://www.seomoz.org/rank-checker are great if you want to check out how your page ranks in Google for a given set of keywords, but you can only perform 5 searches per day using the free account :(.
There are others out there that will let you perform lots of searches, but restrict you to the first 100 results.

Apparently Google limits a given user to only so many (1000?) searches per day before having to use a CAPTCHA to perform subsequent searches, which I guess is understandable.

But what if you want to do more than 5 but less than 1000? And you want to scan the first 1000 results, because your site is really crap and you want to see if it's getting better? Luckily ,it is trivial to replicate the behaviour of SEOMOZ using simple screen scraping techniques. If you use the code below and install it on your localhost IIS you will be able to perform (practically) unlimited SEO searches. With the caveat that Google may at their whim change their HTML of course and hence break it!

Use at your own risk would be my advice.

You will need the latest .Net Framework 3.5 to run this. Create a new web app project in Visual Studio Pro 2008 or Web Dev Express and make a new .aspx page. Copy and paste the code below and voila, it should all work! (Oh yeah, you'll need a couple of images too if you want the nice AJAX effect - I suggest http://www.ajaxload.info/) If you are wondering it's Open Source so feel free to make it work better for you!


(Copy all to new .aspx page)

Sunday, 4 May 2008

Online Viewstate Viewer / Decoder for Asp.Net 2.0

I got frustrated trying to find an online viewstate decoder for ASP.Net 2.0(.aspx), so wrote my own. You can use it below. NOTE! this is very basic; if you do something wrong you will get a generic error so make sure you copy the viewstate carefully (Firebug in Firefox makes this easy!).

Also, if you have a large viewstate it will take a while for the Treeview to build as I am just using the MS TreeView control which is very verbose in its HTML output.

Breaking news : World's first (possibly) online hex file viewer written entirely in Javascript!

Tuesday, 29 April 2008

Firefox refresh viewstate updatepanel bug hell!!!

Have you ever noticed that when you click the Refresh button in Firefox, any form values you have filled in on the screen are retained after the refresh? Firefox is automatically altering these values at the end of the request, presumably as a convenience to you, the user. Hence the DOM and the page source are out of sync immediately after the refresh. There is nothing you can do to disable this behaviour within Firefox.

For the average web app, this is a desirable feature. But when combined with the ASP.Net viewstate model, it is anything but desirable, as ASP.Net uses a hidden form field to store serialized viewstate information.

The specific bug I have encountered is when using ASP.Net in conjunction with the built-in validation controls and AJAX. When using validation controls, if viewstate does not match that which last left the server, an error is produced :
The state information is invalid for this page and might be corrupted.

Because Firefox attempts to fill in the current form with current values after a refresh, a new viewstate, representing a fresh page, can be overwritten by an old viewstate (the result of 1 or many AJAX postbacks changing control states since the last "fresh" page load.)

The easiest solution I have found to this problem thus far is to change the form ID with each (non postback) page load. This will fool Firefox into thinking the form is different, and as such all values (including viewstate) will get their values from the incoming HTML rather than the browser's previous state.


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    
If Not Me.IsPostBack Then

        Me.form1.ID = "form" & Date.Now.Ticks().ToString()            

    
End If

End Sub



The only potential problem you could encounter with this solution is where the form ID has been hardwired in your application, either in client side script or server side code. This can be mitigated by always using myForm.clientID when referencing the form's ID. This is good practice in any case.

Hopefully this will help others having the same problems I was.

NOTE!!!
This solution will not work when using a Master Page. Use the solution suggested here instead (change form ID in pageLoaded AJAX event).

For those interested, the offending code is here in System.Web.UI.HtmlControls.HtmlForm.UniqueID: (from Reflector)

Public Overrides ReadOnly Property UniqueID As String
   Get
      If (Me.NamingContainer Is Me.Page) Then
         Return MyBase.UniqueID
      End If
      Return "aspnetForm"
   End Get
End Property