Archive for October, 2008

My first iPhone app

Despite not owning an iPhone (I want one!), I’ve built my first iPhone application.

It’s a web app for playing Suduko.  It generates a new grid every day.  Utilises jQuery of course.

Find it here http://sudoku.nerdboy.co.uk/

I used http://www.testiphone.com/ to see that it worked and fitted the screen.

If anyone reading this actually has an iPhone and the time to test it I’d appreciate it.

Enjoy

 

Firebug for Google Chrome Campaign

I’ve been a Firefox fan for ages and when I discovered Firebug my life became a whole lot easier.  So much so, that I’d hate to be without it.

But… I’m loving Google Chrome for it’s sheer speed.  I never realised Firefox was slow until I tried Chrome – maybe it’s all the extensions I have installed :)

So I now spend my time between the 2 browsers.

I’d love to see a version of Firebug for Chrome, maybe it should be called ChromeBug…

 

Required field validator for an fckeditor

Unfortunately a requiredfieldvalidator doesn’t work with the fckeditor.

The way to do this is to use a custom validator.

 

Important note: you need to set validateemptytext=”true” otherwise it doesn’t fire

Then add the following javascript (note that I’m using jQuery here to reference the validator control)

<script type="text/javascript">// <![CDATA[
function ValidateContentText(source,args)
{
var controltovalidate = jQuery(source).attr("controltovalidate");
var fckControl = FCKeditorAPI.GetInstance(controltovalidate);
args.IsValid = fckControl.GetXHTML(true) != "";
}
// ]]></script>

You might also want to handle the server-side validation too, even though if client-side isn’t working then the fckeditor won’t be working…

Protected Sub cvIntro_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvIntro.ServerValidate

Dim myContentPlaceHolder As ContentPlaceHolder

myContentPlaceHolder = CType(Master.FindControl("cphMain"), ContentPlaceHolder)

Dim fckEditorToCheck As FredCK.FCKeditorV2.FCKeditor

fckEditorToCheck = myContentPlaceHolder.FindControl(CType(source, CustomValidator).ControlToValidate)

args.IsValid = Not String.IsNullOrEmpty(fckEditorToCheck.Value)

End Sub

Update 2011

var controltovalidate = jQuery(source).attr("controltovalidate");

No longer works... just use

var controltovalidate = source.controltovalidate;

Instead - easy peasy

 

Find a control within a area

If you are using master pages, you may expect that you can find a control within an tag with the following

me.findcontrol("fckEditor")

But unfortunately that doesn’t work.
You need to make a reference to the place holder the control is in, then find the control within the place holder.
Another gotcha here is that you need to find this control based on the id it has on the master page not the id it has in the page in question.
e.g.
If the tags on the page are

<asp:Content ID="Content2" ContentPlaceHolderID="cphMain" Runat="Server">
</asp:Content>

You need to create a reference to the place holder with

Dim myContentPlaceHolder As ContentPlaceHolder
myContentPlaceHolder = CType(Master.FindControl("cphMain"), ContentPlaceHolder)

and not

Dim myContentPlaceHolder As ContentPlaceHolder
myContentPlaceHolder = CType(Master.FindControl("Content2"), ContentPlaceHolder)

Hope that saves someone some time!

 

Lo-Fi unzipping functionality for .net

I’ve looked for unzipping functionality for .net a couple of times over the years and never found a totally satifactory solution.

In a recent project I had to grab an attachment from a POP box and then unzip the attachment.

I found an unzipping product called 7zip, it’s open source and there’s a command line version.  See their downloads page for the commandline version - http://www.7-zip.org/download.html

Once you have this exe on your server you can easily unzip a file using the following code:

dim pathToZip as string = "c:\some\folder\file.zip"
Dim p As Process = Process.Start("C:\Program Files\7Zip\7za.exe", "e " & pathToZip & " -oc:\")
p.WaitForExit()

Obviously you are not limited to just unzipping, you can do anything the command line version does: zip, password protect a zip file, list the contents, etc

 

asp.net outputcache based on url

The outputcache page directive is very handy – and I tend to use this where appropriate on my usercontrols (.ascx)

You can optionally vary by querystring parameters.

Something that’s missing from the default options is to vary based on the actual url of the page.  I was building a site where a random testimonial was pulled from the database and displayed in a small panel.  I wanted this to stay constant on each particular url.

Here’s how to do it…

Add this to your global.asax

    Public Overrides Function GetVaryByCustomString(ByVal context As HttpContext, ByVal custom As String) As String

        Select Case custom.ToLower

            Case "pageurl"

                Return Request.FilePath.ToLower

            Case Else

                ' Return String.Empty

                Return MyBase.GetVaryByCustomString(context, custom)

        End Select

    End Function

Then on your ascx file add the following to the top

<%@ OutputCache Duration="60" Shared="true" VaryByParam="None" VaryByCustom="pageurl" %>

 

Microsoft adopt jQuery

I’ve been using jQuery for ages and I love it.  Finally a very easy, crossbrowser, method to target any element or group of elements on the page.

Plus it has some very straightforward and powerful ajax features built in.

I did look at the whole ajax.net thing a while back and it was way too bloated for me to give it a serious look.

I’ve just heard the Microsoft have woken up to jQuery too.  In future versions of Visual Studio jQuery intellisense wil be built it.  Thanks Microsoft!