Sunteți pe pagina 1din 2

In one of the Ajax project while using <ajaxToolkit:AutoCompleteExtender> i was getting following error Error Creating control -autoComplete1

This control cannot be displayed because it's tag prefix is not registered in th e web form. Here is solution <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" tagpre fix="ajaxToolkit"%> Which says that the reference assembly "AjaxControlToolkit.dll" should be regist ered on the page by using above method. We should also have tagprefix="ajaxToolkit" which is simi lar to <ajaxToolkit:AutoCompleteExtender> In this article i have very simple database table called as "Tbl_Countries" with fields as CountryId CountryName int varchar(50) primary key,

I am using a web service called as "AutoComplete.asmx" whose code behind get aut omatically added to the "App_Code" folder Note: Very important part is you need to give a reference of "AjaxControlToolkit .dll" Here is complete code for "AutoComplete.asms.cs" using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using System.Data; using System.Data.SqlClient; using System.Configuration; /// <summary> /// Summary description for AutoComplete /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class AutoComplete : System.Web.Services.WebService { [WebMethod] public string[] GetCountriesList(string prefixText) { DataSet dtst = new DataSet(); SqlConnection sqlCon = new SqlConnection(ConfigurationManager.AppSetting s["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; } } Let us take another page called as "default.aspx" on which i am having a <script manager> tag and in which a <service> tag in which you can specify path of webse rvices You have already registered Assembly="AjaxControlToolkit" on this page so you ca n use that so my entire code will look something like this <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherit s="_Default" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" tagpre fix="ajaxToolkit"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/ DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> <Services> <asp:ServiceReference Path="AutoComplete.asmx" /> </Services> </asp:ScriptManager> <div> <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox> <ajaxToolkit:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtCountry" ServicePath="AutoComplete.asmx" ServiceMethod="GetC ountriesList" MinimumPrefixLength="1" EnableCaching="true" /> </div> </form> </body> </html> Have a look at attached Image1.gif which will give you clear idea as how you aut ocomplete works Enjoy very easy coding!! >>Munnamax

S-ar putea să vă placă și