<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dot Net Thoughts &#187; AutoSuggest</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/autosuggest/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>thoughts about .Net, WPF, Sharepoint, Javascript and more.</description>
	<lastBuildDate>Wed, 01 Sep 2010 09:53:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Simple AutoSuggest Textbox using JQuery</title>
		<link>http://www.dotnetthoughts.net/2009/04/20/simple-autosuggest-textbox-using-jquery/</link>
		<comments>http://www.dotnetthoughts.net/2009/04/20/simple-autosuggest-textbox-using-jquery/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 08:02:54 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[AutoSuggest]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[JQuery AutoSuggest]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=262</guid>
		<description><![CDATA[Few months back, one of my colleague from UI team, introduced JQuery to me, our purpose was to show some nice dialog boxes using JQuery. After some time I moved in to some Windows projects. And now I am back in web development, few days back I got a task to implement a Auto suggest [...]]]></description>
			<content:encoded><![CDATA[<p>Few months back, one of my colleague from UI team, introduced JQuery to me, our purpose was to show some nice dialog boxes using JQuery. After some time I moved in to some Windows projects. And now I am back in web development, few days back I got a task to implement a Auto suggest textbox for US Cities. Initialy I was planning to implement it via ASP.Net AJAX, then I thought about simple<br />
implementations. After some googling I found one solution, I modified the code slightly, and implemented it in a Custom user control.</p>
<p>Here is the C# custom control (AutoSuggestTextBox) code.</p>
<pre class="brush: csharp;">

[DefaultProperty(&quot;Text&quot;)]
[ToolboxData(&quot;&lt;{0}:AutoSuggestTextBox runat=server&gt;&lt;/{0}:AutoSuggestTextBox&gt;&quot;)]
public class AutoSuggestTextBox : WebControl
{
protected override void RenderContents(HtmlTextWriter output)
output.WriteLine(&quot;&lt;script type=\&quot;text/javascript\&quot;&gt;&quot;);
output.WriteLine(&quot;$().ready(function() {&quot;);
output.WriteLine(string.Format(&quot;$(\&quot;#{0}TextControl\&quot;).autocomplete(\&quot;{0}.ashx\&quot;);&quot;, this.ID));
output.WriteLine(&quot;});&lt;/script&gt;&quot;);
output.WriteLine(string.Format(&quot;&lt;input type=\&quot;text\&quot; id=\&quot;{0}TextControl\&quot; name=\&quot;{0}TextControl\&quot; /&gt;&quot;, this.ID));
}
}
</pre>
<p>In the implementation, I need to give the output from the database as string, so I implemented the code in a asp.net handler(ASHX) file. And in the ProcessRequest method, I wrote the following code.</p>
<pre class="brush: csharp;">
if (!string.IsNullOrEmpty(context.Request.QueryString[&quot;q&quot;])) // remember, 'q' is the query the autosuggest will pass
        {
            SqlDataReader reader;
            string query = string.Format(&quot;SELECT [CityName] FROM [City] WHERE [CityName] LIKE '%{0}'&quot;, context.Request.QueryString[&quot;q&quot;].ToString());
            using (SqlConnection connection = new SqlConnection(&quot;Server=mydbserver; User Id=sa;Password=mypassword;Database=mylookups;&quot;))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                    while (reader.Read())
                    {
                        context.Response.Write(reader[&quot;CityName&quot;].ToString() + Environment.NewLine);
                    }
                    reader.Close();
                }
                connection.Close();
            }
        }
</pre>
<p>Note: I am not promising the above code is optimised. [:)]</p>
<p>And the ASPX Page you need to add reference to following JS Files and Style.</p>
<pre class="brush: xml;">
    &lt;script type=&quot;text/javascript&quot; language=&quot;javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;jquery.autocomplete.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
    &lt;style type=&quot;text/css&quot;&gt;
        .ac_odd {background:#dddddd;}
        .ac_results { padding: 0px; border: 1px solid black; background-color: white; overflow: hidden; z-index: 99999; }
        .ac_results ul { width: 100%; list-style-position: outside; list-style: none; padding: 0; margin: 0; }
        .ac_results li { margin: 0px; padding: 2px 5px; cursor: default; display: block; font: menu; font-size:12px; line-height: 16px; overflow: hidden; }
        .ac_loading { background:url(loading.jpg) right no-repeat; }
        .ac_over { background-color: #0A246A; color: white; }
    &lt;/style&gt;
</pre>
<p>We have completed almost everything, then compile the project, drag the Custom control from toolbox to ASPX page, where you refering the JS Files and CSS. The ASHX File name should be the name of the control. Like if the control&#8217;s name is AutoSuggestCity, the ASHX Filename should be AutoSuggestCity.ashx. Please modify the control code if you want to change it.</p>
<p>After running you will get a Textbox, type something there, you will get the suggestions. Here is the one I got.</p>
<p><a href="http://anuraj.files.wordpress.com/2009/04/autosuggest.jpg"><img src="http://anuraj.files.wordpress.com/2009/04/autosuggest.jpg" alt="autosuggest" title="autosuggest" width="239" height="267" class="alignnone size-full wp-image-265" /></a></p>
<p>You can download JQuery from JQuery website. &#8211; <a href="http://www.jquery.com" target="_blank">www.jquery.com</a>. And autosuggest plugin from <a href="http://plugins.jquery.com/project/autocompletex" target="_blank">AutoSuggest plugin</a> page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/04/20/simple-autosuggest-textbox-using-jquery/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
