using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using System.IO;

public partial class _Default : System.Web.UI.Page 
{
    DataTable dictionaryWords = new DataTable();

    private void LoadData()
    {
        string tdfPath = Server.MapPath("App_Data/en-US.tdf");

        if (Cache["dictionaryWords"] == null)
        {
            DataTable words = new DataTable("words");
            DataColumn column = words.Columns.Add("word");

            string word = String.Empty;
            StreamReader sr = File.OpenText(tdfPath);

            word = sr.ReadLine();
            while (word != null)
            {
                string realWord = word.Split(':')[0];
                words.Rows.Add(new string[] { realWord });
                word = sr.ReadLine();
            }

            sr.Close();
            Cache["dictionaryWords"] = words;
        }

        dictionaryWords = (DataTable)Cache["dictionaryWords"];
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
        }
    }

    protected void RadComboBox1_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        LoadData();
        RadComboBox1.Items.Clear();

        string text = e.Text;

        try
        {
            DataRow[] rows = dictionaryWords.Select("word LIKE '" + text + "*'");

            int itemsPerRequest = 20;
            int itemOffset;

            itemOffset = Int32.Parse((string)e.Context["ItemOffset"]);

            int endOffset = itemOffset + itemsPerRequest;


            if (endOffset > rows.Length)
            {
                endOffset = rows.Length;
            }

            for (int i = itemOffset; i < endOffset; i++)
            {
                RadComboBox1.Items.Add(new RadComboBoxItem(rows[i]["word"].ToString(), rows[i]["word"].ToString() + "'"));
            }

            if (rows.Length > 0)
            {
                e.Message = String.Format(
                                "Items <b>{0}</b>-<b>{1}</b> out of <b>{2}</b>", 
                                (itemOffset + 1).ToString(), 
                                endOffset.ToString(), 
                                rows.Length.ToString());
            }
            else
            {
                e.Message = "No matches";
            }
        }
        catch (Exception ex)
        {
            e.Message = "No matches";
        }
    }
}