[SalesForce] Adding an ad hoc lookup to Visualforce page

I have a totally custom visualforce page, with a number of elements on it, backed by controller instance variables, and I'm using apex:inputField to successfully render them all perfectly.

I now wish to allow the user to "select" a contact, and then I have an Apex function to do some stuff to the chosen contact.

I have created a Contact instance variable (as per all the others) like so:

public Contact contactLookup { get; set; }

but then, on the VisualForce page, if I try to render the input via:

<apex:inputField value="{!contactLookup}" />

I get an error message:

    Error: Could not resolve the entity from <apex:inputField> value binding '{!contactLookup}'. <apex:inputField> can only be used with SObjects, or objects that are Visualforce field component resolvable.  

Which is fair enough "Contact" is not associated with an input type. So my question is, how do I get the good old "Look up" box to appear on the page?

As seen here, from another standard page, for Account:
Account lookup

I'm fully aware of how to do this when using a standard controller/custom field – but not when the lookup is merely backed by an instance variable like this?

Best Answer

You should not reference a Sobject, reference its field instead. Correct way is:

public Account contactLookup { get; set; }

<apex:inputField value="{!contactLookup.contact}" />

<apex:inputField> binds with sObject's field not the whole sObject. If it is a lookup then it will have lookup style.