Click here to Skip to main content
13,446,005 members (43,969 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 24 Jan 2008

PopupLookup control, an alternative for AJAX?

, 24 Jan 2008
Rate this:
Please Sign up or sign in to vote.
Getting that 1 record out of 1000000+ records with these convenient controls.



In a lot of websites I've built and seen, the flow of the program needed lookup information. Some used AJAX to retrieve records out of those 100000+ records. This solution, however, uses a classic but workable popup-form to search and select that single record.

I also wanted to have this control be used with different business objects. These business-objects have to be created dynamically. This is done in a factory using a little Reflection.

This demo application consists of a webform, a user control and a popup form. The webform contains the user control. Actually, this demo contains three of these user controls which are filled with different types of data. The user control is a bag for handling the popup form. It contains a readonly textbox in which the selection will be placed, and a Search button which is supposed to open the popup form and give it focus. The user then enters a search-string and clicks on the Search button. The results are presented in a DataGrid. Selecting a record closes the popup form, and the selected record will be found back in the original webform. The user control has been built using a specifically defined set of properties, so it can be used with all kinds of meta-data. So, for example, organizations can be 'looked-up' with the same control as, for instance, social-security numbers.


This solution was part of an ASP website migration to .NET. Users where used to getting a new webform on screen and selecting certain records within that form. This interface had to be rebuilt in .NET

Using the code

The control uses JavaScript to show a new window. In this window, a textbox with a button will be shown. Entering a text and pressing the button will fill the GridView. Clicking a record in the GridView will close the window (JavaScript) and will fill in the original field with the selected record.

Because the control is made for use with different business objects, as long as it uses the predefined dataset, it has to create different business objects, and therefore, it uses a simple factory. The dataset holds a table with text, value, and description. Usually, this is enough. The value can be disabled in the popup form by using a property. So, for example, for software, you only see the text and the description, but for organizations, there is also a value added. The factory uses Reflection, and finds, in the namespace business, the classes which have the same name given in the ASPX-page. So, if a person-popup-lookup-form is needed, the type is person, and the factory finds, in the namespace business, the string person and creates it. Below is the factory code:

public static IBusiness Create(string businessName) 
    Assembly a = Assembly.GetExecutingAssembly();
    Type[] types = a.GetTypes();
    foreach (Type t in types)
        if (t.Namespace == "Business")
            if (t.FullName.EndsWith(businessName))
                return Activator.CreateInstance(t) as IBusiness;

    throw new ApplicationException(string.Format("Business object" + 
                                   " [{0}] not known", businessName));

The interface IBusiness has all the methods the business objects need for the popup form.

Points of interest

Setting the readonly property of a textbox disables the viewstate functionality. I use this to remember the value during postback. To workaround the absence of viewstate, I had to place the value of the textbox in another special hidden value control, unfortunately. That control is called, hiddenText, in case you are looking for it in the code.

Another point of interest is in the JavaScript. Firefox demands first to close the popup form and then insert the values by calling the window.opener.InsertValue. Peculiar... IE does not demand this.

function LocalSelect(str_Text, str_Value)
    var objForm = window.document.forms[0];
    var objHiddenText = objForm["hiddenTextId"];
    var objShownText = objForm["shownTextId"];    
    var objHiddenValue = objForm["hiddenValueId"];

    if (!window.opener) {
        alert('No more valid reference in calling form');

    if (!window.opener.InsertValue) {
        alert('The calling form does not contain ' + 
              'an implementation of InsertValue(params)');
    //alert('First CloseWindow! After that insertvalue ' + 
    //      'or else this window will not work in firefox');
    window.opener.InsertValue(objHiddenText.value, objShownText.value, 
                              str_Text, objHiddenValue.value, str_Value);

I found the following line to disable auto-complete by the web browser. It is useful for disabling auto-complete for specific controls.

this.txtSearch.Attributes.Add("autocomplete", "off");

That's about it. I hope you liked it, and will use the control. Please give me some reactions and teach me tricks I don't know. Bye.


  • Version 1.0, January 2008.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Dries de Groot
Software Developer
Netherlands Netherlands
Hi my name is Dries de Groot. I have a Masters degree in applied physics and am graduated in solid-state matter. Besides that I am a .NET Professional with the following titles: MCAD, MCSD

You may also be interested in...


Comments and Discussions

QuestionAJAX? Pin
kianoosh soorani25-Jan-08 4:09
memberkianoosh soorani25-Jan-08 4:09 
AnswerRe: AJAX? Pin
Dries de Groot26-Jan-08 6:45
memberDries de Groot26-Jan-08 6:45 
GeneralRe: AJAX? Pin
DimitrisGr31-Jan-08 3:42
memberDimitrisGr31-Jan-08 3:42 
It is not. Actually that functionality (using javascript) existed long before ajax.
GeneralRe: AJAX? Pin
Dries de Groot25-May-10 23:33
memberDries de Groot25-May-10 23:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180314.2 | Last Updated 24 Jan 2008
Article Copyright 2008 by Dries de Groot
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid