Sunday, December 10, 2006

Select ListControl Value

The SelectedValue property of a ListControl can be used to select an item in the list.  However, if no items in the list control contain the specified value, an ArgumentOutOfRangeException is thrown.  This exception can be avoided by using the IndexOf and FindByValue methods of the ListItemCollection.

control.SelectedIndex = control.Items.IndexOf(control.Items.FindByValue(value));

The IndexOf method returns -1 if the specified item is not found and null is returned by the FindByValue method.

Sunday, December 03, 2006

Add AutoCompleteExtender to ASP.NET AJAX Web Site

There are migration guides on the ASP.NET AJAX homepage detailing how to convert Web Sites originally coded against the Atlas CTP to ASP.NET AJAX 1.0 Beta2.  Unfortunately at this time, the AutoCompleteExtender is only included in the ASP.NET AJAX Futures November CTP but it's relatively straightforward to add the AutoCompleteExtender to an AJAX Web Site.

1. Add a reference to the Microsoft.Extensions.Preview assembly, usually located in C:\Program Files\Microsoft ASP.NET\ASP.NET 2.0 AJAX Extensions\v1.0.61025

2. Add the elements below to the configuration\system.web\pages\controls element of the web.config file.

<add tagPrefix="asp" namespace="Microsoft.Web.Preview.UI" assembly="Microsoft.Web.Preview"/>

<add tagPrefix="asp" namespace="Microsoft.Web.Preview.UI.Controls" assembly="Microsoft.Web.Preview"/>


3. Add the ScriptManager, TextBox and AutoCompleteExtender controls to the Web Form.


<form id="form1" runat="server">

  <asp:ScriptManager ID="ScriptManager1" runat="server" />


    <asp:TextBox ID="txtCity" runat="server" />

    <asp:AutoCompleteExtender ID="autoCity" runat="server" CompletionSetCount="8" TargetControlID="txtCity" MinimumPrefixLength="1" ServiceMethod="GetCities" ServicePath="AutoCompleteService.asmx"/>




4. Finally, add the Web Service that the AutoCompleteExtender will call, making sure that the Place code in seperate file checkbox is unchecked so the code is inline.  The WebMethod needs to accept the prefixText and count as parameters, returning the valid list as a string array.  Also note the inclusion of the Microsoft.Web.Script.Services.ScriptService attribute on the class definition.


<%@ WebService Language="C#" Class="AutoCompleteService" %>


using System;

using System.Collections;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;



public class AutoCompleteService  : System.Web.Services.WebService {



    public string[] GetCities(string prefixText, int count)



        string[] autoCompleteWordList = { "Canberra", "Sydney", "Darwin", "Brisbane", "Adelaide", "Hobart", "Melbourne", "Perth" };

        Array.Sort(autoCompleteWordList, new CaseInsensitiveComparer());

        int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer());

        if (index < 0)


            index = ~index;



        int matchingCount;

        for (matchingCount = 0; matchingCount < count && index + matchingCount < autoCompleteWordList.Length; matchingCount++)


            if (!autoCompleteWordList[index + matchingCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase))






        String[] returnCities = new string[matchingCount];

        if (matchingCount > 0)


            Array.Copy(autoCompleteWordList, index, returnCities, 0, matchingCount);


        return returnCities;