This project has moved and is read-only. For the latest updates, please go here.

Replace Lookup Field With SPServices

Apr 2, 2010 at 4:59 PM

Today I ran into a nasty situation with a lookup field that just plain looked horrible! As I am developing this as a .wsp solution, I have the list content type schema file where I am creating the fields. I initially created the field as a lookup to a list. This list was filled with just types of inspections let's say and these ranged from 40 to 100 characters in length and had some apostrophe's and other various characters. The data for that is pre-filled using the onet.xml file when I am declaring the list. I am also using a custom form template(.ascx) file to draw the form because two column forms just look odd to me when you have more than a few fields. When I first deployed a site from this solution I noticed that the lookup field was not drawing the 'dropdown' correctly and it was not aligned with the control on the page. It also took time to draw it and this just seems too strange. This happened in both IE6 and IE7. So, I thought it was time to use this library to fix this issue. I changed the field to a text field in the schema. In the ascx page, I put this control in a td element with css display:none so the field does not show up. Instead I used a select element and on the newform.aspx page I placed a script that gets the items from the other list and puts them in the dropdown. The change event of the dropdown then sets the value of the hidden control to the selection from the dropdown. Presto Chango the field works and looks a lot better! I also added the same script to the editform.aspx page but made sure to pre-select the option that had already been saved.

Apr 21, 2010 at 2:52 PM
So far I have now been able to use this approach several times and it is working great! I am also hopefully ensuring that the options are unique unlike a standard lookup. Just thought I would post an update!