Click here to Skip to main content
15,878,814 members
Articles / Web Development / HTML

Lookup Search - ComboBox Multicolumn, Multiselection

Rate me:
Please Sign up or sign in to vote.
4.76/5 (20 votes)
31 Mar 2014CPOL3 min read 59.2K   2.3K   45   33
Search control and ComboBox Multi-Column and Multi-Selection

Introduction

During the development of an application I often faced the necesity of a simple control to search a pick up a selection against a database without much effort.

There are many possibilities to accomplish this, but it is always tedious and often you need to jump to a different form or write a lot of controls and aspx code which results ugly and not very obvious in designer mode to what are you trying to accomplish, let alone how hard it is to maintain. I started designing a pupup control where user can complete a search, obtain the results from the pupup control to the main page, user can define the templates for the input search controls.

The same control can be used to search information about departments or employees, the only difference will be the data source and the search template.

User can define the columns to show and the columns to be availables for data selection.

In addtionl to the LookUp control I also sharing a control which I developed based on this first one, it has the basic functionality of a standar ComboBox, but with the feature of multi-column and multi-selection.

I've been seen many articles of people looking for a solution for a multi-column and multi-selection drop-down control.

These controls can be nested in other controls containers and well as nested in ajax updated panels.

Suport cross browers IE, Firefox, and Chrome.

1. Lookup Search Control

Having as TextBox which you need to populate either with a specific value being pulled from a search or with many values, this can be accomplish the as so:

Example:

Image 1

ASP.NET
<asp:TextBox ID="TextBox2" runat="server" Width="120px"></asp:TextBox>

<lu:LookUp ID="LookUp2" runat="server" 
  DataKeyNames="OuCode,OuDescription,LM" DataSourceID="ObjectDataSource2"
  ExtenderEnabled="False" HtmlTemplate="~/luTemplates/OuSearch2.htm" 
  TargetControlId="TextBox2" Width="16px" OnOnRowClick="LookUp2_OnRowClick" 
  Height="16px" TitlePopUp="Test No 2"
  FadeDelay="3" WHeight="500">
<ItemTemplate>
    <lu:LuTemplate ID="LuTemplate7" runat="server" CssClass="LU_CellLeft">
        <HeaderText>Ou Code</HeaderText>
        <Template>
            <asp:Label ID="Label100" runat="server" 
               Text='<%# DataBinder.Eval(Container.DataItem, "OuCode")%>' />
        </Template>
    </lu:LuTemplate>
    <.................................../>
    <lu:LuTemplate ID="LuTemplate12" runat="server" CssClass="LU_CellBox">
        <HeaderText>Changed on</HeaderText>
        <Template>
            <asp:Label ID="Label104" runat="server" 
              Text='<%# DataBinder.Eval(Container.DataItem, "LastChange","{0:d}")%>' />
        </Template>
    </lu:LuTemplate>
</ItemTemplate>
</lu:LookUp>

The control has an event hander returning a DataTable with results of the selected items.

C#
protected void LookUp2_OnRowClick(object sender, LookUpSearch.LookUp.RowData e)
{
    TextBox2.Text = string.Empty;

    if (e.DataTable.Rows.Count > 0)
    {
        foreach (DataRow row in e.DataTable.Rows)
        {
            TextBox2.Text = string.Format("{1},{0}", 
              ((LookUpSearch.LookUp)(sender)).TargetTextBox.Text,row["OuCode"]);
        }
        TextBox2.Text = TextBox2.Text.TrimEnd(',');
        Oudescripion2.Text = e.DataTable.Rows[0]["OuDescription"].ToString();
        Lm2.Text = e.DataTable.Rows[0]["LM"].ToString();
    }
}

User can define html templates with the input contros to define the search criteria, the name of the input controls must match with the names of the parameters in the ObjectDataSource.


Image 2

Image 3

Get_OuSearch1 Method

C#
public List<Department> Get_OuSearch1(string search1, string search2, string search3, bool OnlyActive)
{
    List<Department> list = GenData();
    return list.Select(s => new Department
    {
    OuCode = s.OuCode,
    OuDescription = s.OuDescription,
    LM = s.LM,
    LMName = s.LMName,
    LastChange = s.LastChange,
    Active = s.Active
    }).OrderBy(o => o.OuCode).ToList<Department>();
}

Image 4

Result:

Image 5

2. JmComboBox Control

The JmComboBox can be single or multi selection, it is very similar to the Lookup control, but this control does not use different template, user can define the presentation of the culumns directly in the control template as well as define the Keys or Fields desired to be available when user complete the selection.

A combo box, which is represented by the JmComboBox control, is an editor that combines the functionality of a single-line text editor and dropdown list editor. The editor's dropdown displays a list of items which can be selected by end-users.

ASP.NET
<jmcb:MultiComboBox ID="MultiComboBox3" runat="server" CssClass="" 
     DataSourceID="ObjectDataSource1" DataTextField="ContactName" 
     DataValueField="Id" Height="18px" TextFormatString="{0} - {1} - {2}" 
     Width="250px" LayoutWidth="500" LayoutHeight="300" 
     DataKeyNames="Country" MaximumEntries="50">
 <columns>
    <jmcb:ColumnField ID="LuTemplate3" runat="server" 
            CssClass="CB_CellLeft" Width="100px">
        <HeaderText>Contact Name</HeaderText>
        <FieldTemplate>
            <table width="100%">
                <tr>
                    <td style="width: 18px; padding-right: 6px;"><img 
                      src="twitter-icon.png" style="width: 18px; height: 18px" 
                      alt=""/></td><td style=" 
                      text-align: left;"><%# DataBinder.Eval(
                         Container.DataItem, "ContactName")%></td>
                </tr>
            </table>
        </FieldTemplate>
    </jmcb:ColumnField>   
    <jmcb:ColumnField CssClass="CB_CellLeft" Width="100px">
    <HeaderText>Company Name</HeaderText>
    <FieldTemplate>
        <%# DataBinder.Eval(Container.DataItem, "CompanyName")%>
    </FieldTemplate>
</jmcb:ColumnField>
    <jmcb:ColumnField CssClass="CB_CellBox" Width="100px">
    <HeaderText>Country</HeaderText>
    <FieldTemplate>
        <asp:Label ID="Label4" runat="server" 
          Text='<%# DataBinder.Eval(Container.DataItem, "Country")%> '>
        </asp:Label>
    </FieldTemplate>
</jmcb:ColumnField>
</columns>
</jmcb:MultiComboBox>
Data-Bound and Unbound Modes Support

The editor's contents can be generated dynamically by binding the editor to a data source, as well manually populating the control's item collection. When retrieving items from the data source, item characteristics such as the text, value, and image are obtained from specific data fields that can be defined manually or are obtained automatically providing the data fields named in a specific way.

Incremental Filtering

The JmcComboBox editor enables end-users to filter list items dynamically, based upon the text typed into the editor's input box on the client side (find-as-you-type filtering). The following filter modes are available.

  • StartsWith: The editor is filtered for list items that begin with the search string.
  • Contains: The editor is filtered for list items that contain the search string. The found search string is highlighted within items for clarity.

Image 6

Display Formatting

Editor display values can be formatted using the standard formatting mechanism.

TextFormatString="{0} : {1} of {2}"

Image 7

Multi-Column Mode

It's possible to represent the JmcComboBox's list data in several columns. This functionality is in effect if the editor's items collection is obtained from a data source. You can also define a column's header caption, width, visibility state and other settings for columns.

Image 8

Image 9

Multi-Selection Mode

It's possible to select multiple values.

Image 10
Validation

The JmcComboBox control supports data validation server side, allowing you to implement any custom validation logic. If an editor fails validation logic, an error message can be displayed.

License

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


Written By
Web Developer UBS AG
Switzerland Switzerland
More than 12 years of design/development data driven applications, with 8 years in the banking industry working at a high standard with both sensitive and a high volume of data.

Comments and Discussions

 
Suggestioncode Pin
shbnq422vb6-Dec-13 4:05
shbnq422vb6-Dec-13 4:05 
GeneralRe: code Pin
Josue Medrano6-Dec-13 4:45
Josue Medrano6-Dec-13 4:45 
GeneralRe: code Pin
Josue Medrano6-Dec-13 6:48
Josue Medrano6-Dec-13 6:48 
QuestionWhere's source code?? Pin
Sunil Wise5-Dec-13 22:49
professionalSunil Wise5-Dec-13 22:49 
AnswerRe: Where's source code?? Pin
Josue Medrano6-Dec-13 2:35
Josue Medrano6-Dec-13 2:35 
GeneralRe: Where's source code?? Pin
Josue Medrano6-Dec-13 6:48
Josue Medrano6-Dec-13 6:48 
AnswerRe: Where's source code?? Pin
Josue Medrano6-Dec-13 6:50
Josue Medrano6-Dec-13 6:50 
QuestionWhere is the source code of the control? Pin
Tridip Bhattacharjee5-Dec-13 19:10
professionalTridip Bhattacharjee5-Dec-13 19:10 
u talk about the control but did not post the source code....what does it mean. how people can run your code and see how controls works.
tbhattacharjee

AnswerRe: Where is the source code of the control? Pin
Josue Medrano6-Dec-13 2:35
Josue Medrano6-Dec-13 2:35 
AnswerRe: Where is the source code of the control? Pin
Josue Medrano6-Dec-13 6:48
Josue Medrano6-Dec-13 6:48 
QuestionControl Pin
Muhammad_Shoaib5-Dec-13 17:50
professionalMuhammad_Shoaib5-Dec-13 17:50 
AnswerRe: Control Pin
Josue Medrano6-Dec-13 2:36
Josue Medrano6-Dec-13 2:36 
AnswerRe: Control Pin
Josue Medrano6-Dec-13 6:49
Josue Medrano6-Dec-13 6:49 
GeneralThanks Pin
Muhammad_Shoaib6-Dec-13 18:58
professionalMuhammad_Shoaib6-Dec-13 18:58 

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.