Total Pageviews
Tuesday, July 7, 2009
AutoCompleteExtender Control in the ASP.NET AJAX Toolkit
The AutoCompleteExtender Control in the ASP.NET AJAX Toolkit is an awesome way to offer suggestions to users in your ASP.NET Web Applications. It is also very easy to use.
Create an ASP.NET AJAX Website and toss a TextBox and the AutoCompleteExtender Control on the page.
Fill out the details of the control to specify the appropriate ServicePath and ServiceMethod of the web service that the AutoCompleteExtender Control will call. In this case I have a web service, called MyAutocompleteService, with a web method, called GetSuggestions. The control will start offering me suggestions once the first character is typed into the TextBox and ask for up
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtCountry"
ServicePath="AutoComplete.asmx" ServiceMethod="GetCountriesList" MinimumPrefixLength="1"
EnableCaching="true" />
The GetSuggestions WebMethod on the WebService echos back what was typed in an just appends up to 12 characters ( A- L ) to the text typed in. Note the addition of the [ScriptService] Attribute to use the WebService with AJAX.
[WebMethod]
public string[] GetCountriesList(string prefixText)
{
DataSet dtst = new DataSet();
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.App Settings["ConnectionString"]);
string strSql = "SELECT CountryName FROM Tbl_Countries WHERE CountryName LIKE '"+ prefixText+"%' ";
SqlCommand sqlComd = new SqlCommand(strSql,sqlCon);
sqlCon.Open();
SqlDataAdapter sqlAdpt = new SqlDataAdapter();
sqlAdpt.SelectCommand = sqlComd;
sqlAdpt.Fill(dtst);
string[] cntName = new string[dtst.Tables[0].Rows.Count];
int i = 0;
try
{
foreach (DataRow rdr in dtst.Tables[0].Rows)
{
cntName.SetValue(rdr["CountryName"].ToString(), i);
i++;
}
}
catch { }
finally
{
sqlCon.Close();
}
return cntName;
}
Subscribe to:
Post Comments (Atom)
Blog Archive
Ideal SQL Query For Handling Error & Transcation in MS SQL
BEGIN TRY BEGIN TRAN --put queries here COMMIT; END TRY BEGIN CATCH IF @@TRANCOUNT>0 BEGIN SELECT @@ERROR,ERRO...
-
All we need to use the Custom paging in grid view without object data source. Here i am going to explain my code which is used in custom pa...
-
One of the common requests in ASP.NET is to submit a form when visitor hits an Enter key. That could be a case if, for example you want to m...
-
using System.Data; using System.Data.SqlClient; using System.Text; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; ...
No comments:
Post a Comment