home  |  about  |  contact us
Overview  |  XWebSiteTemplate  |  XBalloon  |  AutoSuggestMenu  |  AutoSuggestBox
Customizing AutoSuggestMenu
back to documentation <<

Table of contents
  1. Overview
  2. How does it work?
  3. Classes
  4. Getting suggestions
  5. CSS


Overview

AutoSuggestMenu package consists of the following components.

AutoSuggestMenu.dll Contains AutoSuggestMenu web control
AutoSuggestMenu.css Style sheet for look and feel of the menu and menu items.
AutoSuggestMenu.js Javascript object for rendering and manipulation AutoSuggestMenu control.
AutoSuggestMenuItem.js Exposes functionality of suggestions inside AutoSuggestMenu.


How does it work?

When user starts typing text in the control javascript handles OnKeyUp, OnKeyDown and OnKeyPress events.
Then ASP.NET AJAX Extensions are used to contact the server side with every click to retrieve a list of suggested entries.

So the flow of events is as follows:
  1. User presses a key while the focus is on text box associated with AutoSuggestMenu control
  2. Javascript checks if user entered up/down/enter or any other character
    • If user entered up/down and suggest menu is visible - appropriate menu item is selected
    • If user clicked 'enter' suggest menu becomes invisible, while currently selection is filled into suggest box
  3. If user typed an alphanumeric character javascripts sends XmlHttpRequest to appropriate web service method on the server
  4. Server-side web service method takes the current value of the text box It usually generates a query to retrieve suggested values from the database. Then it uses results of the query to create JSON datastrcuture to return back to the client.
  5. Web page retrieves list of suggestions (of type 'AutoSuggestMenuItem') and displays them in place of the old one


Classes

There are two classes included in the AutoSuggestMenu.dll : AutoSuggestMenu and AutoSuggestMenuItem

AutoSuggestMenu

Summary

Web control derived from System.Web.UI.Controls.WebControl
Attaches itself to specified TextBox and displays suggested values when user types in a text box
Keeps track of last value selected int TextBox

Properties

Name Type Description Default Example
DataType string Indicates what type of values to fill into suggest box.
Must be implement in GetAutoSuggestData.aspx web page.
  Atlanta, Georgia, USA
Paris, France
SelectedValue string Unique identifier of the item selected from the list of suggested items.
Usually not visible to end user. Similar to the value field of the options in Drop Down List.
Use ASBMenuItem.Value to specify the unique value.
  <<City Code>>
like NYC, LON
ResourcesDir string Web directory where 'AutoSuggestMenu.css', 'AutoSuggestMenu.js' and 'GetAutoSuggestData' are located. /AutoSuggestMenu /Utils/AutoSuggestMenu
MinSuggestChars int Minimum number of characters in text box that trigger suggestion menu. 1  
MaxSuggestChars int Maximum number of characters in text box that trigger suggestion menu. 5  
KeyPressDelay int Milliseconds to wait for next key stroke before displaying sugesstions. Can be used to minimize number of database queries if user types quickly. 300  
UsePaging bool Indicates if paging should be used. true  
PageSize int Number of menu items that will appear per page in suggest menu. 10  
UseIFrame bool Indicates that IFrame should be used in Internet Explorer so SELECT controls overlapping suggest menu don't appear in front of it. true  
MenuCssClass string Controls menu background, border, etc... asmMenu  
MenuItemCssClass string Specifies how menu items are displayed. asmMenuItem  
SelMenuItemCssClass string Name of CSS class used to display selected menu item. asmSelMenuItem  


AutoSuggestMenuItem

Summary

Represents a single item in the auto-suggested item menu.

Properties

Name Type Description Example
Label string Label of the menu item New York, USA
Paris, France
Value string Value associated with menu item.
If user selects the menu item - AutoSuggestMenu.SelectedValue property is set to the value of the item.
<<City Code>>
like NYC, PAR
CssClass string Style sheet class for displaying the menu item.
Defaults to 'asmMenuItem' in 'AutoSuggestMenu.css'.
 
IsClickable bool Indicates if user can click on the menu item. Can be used to display item categories. For example names of the continents can be non-clickable, while city names clickable.


Getting Suggestions

AutoSuggestMenu uses Microsoft Ajax Extensions to retreive suggestions from the server. You can download the latest version of "Microsoft Ajax Extenstions" from
here.


There are two methods you could use:
  1. PageMethods - add GetSuggestions code to the code behind of the page that contains AutoSuggestMenu
  2. WebService - re-use GetSuggestions functionality for one or multiple pages
How to Use PageMethods
To use page methods - you need to have the following code on your ASPX page:

<asp:ScriptManager ID="scriptManager" EnablePageMethods="true" runat="server"/>

<asp:TextBox id="txtCity" MaxLength="100" Columns="30" runat="server"/>
<Custom:AutoSuggestMenu id="asmCity" 
                        TargetControlID="txtCity" 
                        runat="server"/>

Please make sure that EnablePageMethods is set to true in ScriptManager.

In code behind you will need to implement GetSuggestions method. Name of this method can be changed using AutoSuggestMenu.onGetSuggestions property. Here is what code-behind will look like:

using System.Collections.Generic;
using System.Web.Services;

using ENTech.WebControls;

...

[WebMethod()]
public static string GetSuggestions(string keyword, bool usePaging, int pageIndex, int pageSize)
{
     List<AutoSuggestMenuItem> menuItems;

    //Load suggestions. Take paging into account if usePageing is true.
    
    
    //Convert suggestions list into JSON format and return    
    return AutoSuggestMenu.ConvertMenuItemsToJSON(menuItems, totalResults);
}
    

For more details code-behind of Demo1 example available in download.


How to use WebService

First you need to add WebService class to your project. To do that:
  1. Right click on the web directory where you would like web service to reside
  2. Select Add New Item
  3. Select "Web Service" item type and specify web service name. Click OK
  4. You will now have "<Web Service Name>.asmx" in specified directory and "<Web Service Name>.cs" code-behind in /App_Code

Then you need to link AutoSuggestMenu to a web service method. To do that add service reference to ScriptManager declaration and then specify appropriate webservice method in AutoSuggestMenu.OnGetSuggestions.

The code in you web page (ASPX) will look like:

...
<asp:ScriptManager ID="scriptManager"  runat="server">
     <Services>
        <asp:ServiceReference Path="~/TestWebService.asmx" />
     </Services>
</asp:ScriptManager>

...
<asp:TextBox id="txtCity" MaxLength="100" Columns="30" runat="server"/>
<Custom:AutoSuggestMenu id="asmCity" 
                TargetControlID="txtCity" 
                UsePageMethods="false"
                OnGetSuggestions="TestWebService.GetCitySuggestions"
                runat="server"/>


Lastly declare WebMethod that you specifed in AutoSuggestMenu in "<Web Service Name>.cs". Make sure you add [ScriptService] to web service class declaration.

The method will look pretty much like the code behind in PageMethods approach:

using System.Collections.Generic;
using System.Web.Services;

using ENTech.WebControls;

...

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class TestWebService : System.Web.Services.WebService
{
    ...
    
    [WebMethod()]
    public static string GetCitySuggestions(string keyword, bool usePaging, int pageIndex, int pageSize)
    {
         List<AutoSuggestMenuItem> menuItems;

        //Load suggestions. Take paging into account if usePageing is true.
        
        
        //Convert suggestions list into JSON format and return    
        return AutoSuggestMenu.ConvertMenuItemsToJSON(menuItems, totalResults);
    }
    
    ...
}  


CSS

There are four CSS classes that control the look of suggest menu.
You can cusomize them by modifiying 'AutoSuggestMenu.css'.

The classes are:
Name Description
.asmMenu The look of the whole menu and how the items are positioned within it.
.asmMenuItem Describes how each menu item is displayed.
.asmSelMenuItem CSS Class for selected menu item.
.asmNavigationLink For displaying "next page"/"previous page" links when paging is used.

© 2008 ENTech Solutions    All Rights Reserved