本文介绍了自动完成在阿贾克斯不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阿贾克斯,我在我的asp.net应用程序中使用autocompleteExender,我写为服务,当我运行该服务它工作正常,当我把autocompleteextender在asp.net页面和AJAX autocompleteextender其分配properity不管用。这是我的code服务:

  [的WebMethod]
公共字符串[] GetCompletionList(字符串prefixText)
{
    SqlConnection的CON =新的SqlConnection
     (服务器= ******;数据库= MYDB;用户ID = ***;密码= ****;);
    字符串SQL =从F_Product选择产品名称
      凡产品名称像'+ prefixText +%';
    SqlDataAdapter的大=新SqlDataAdapter的(SQL,CON);
     尝试
    {
        DataTable的DT =新的DataTable();
        da.Fill(DT);
        字符串[] =项新的字符串[dt.Rows.Count]
        INT I = 0;
        的foreach(在dt.Rows的DataRow博士)
        {
            items.SetValue(DR [0]的ToString(),I);
            我++;
        }
        返回的物品;
    }
    抓住
    {
        返回null;
    }
    最后
    {
        con.Close();    }

这是我的ajax autocompleteextender code。

 < ASP:AutoCompleteExtender ID =AutoCompleteExtender1最低prefixLength =2
     的TargetControlID =TextBox1的ServiceMethod =GetCompletionList
       ServicePath =〜/ Autocomplete.asmx
         =服务器>
    < / ASP:AutoCompleteExtender>
    < ASP:文本框ID =TextBox1的=服务器
     WIDTH =213px>< / ASP:文本框>


解决方案

尝试了这一点。

  [的WebMethod]
    公共字符串[] GetCompletionList(字符串prefixText)
    {        字符串SQL =选择从F_Product产品名称产品名称在哪里像@ prefixText
        SqlDataAdapter的大=新SqlDataAdapter的(SQL,System.Configuration.ConfigurationManager.ConnectionStrings [dbConnectionString]的ConnectionString);
        da.SelectCommand.Parameters.Add(@ prefixText,SqlDbType.VarChar,50).value的= prefixText +%;
        DataTable的DT =新的DataTable();
        da.Fill(DT);
        字符串[] =项新的字符串[dt.Rows.Count]
        INT I = 0;
        的foreach(在dt.Rows的DataRow博士)
        {
            items.SetValue(DR [产品名]的ToString(),我。);
            我++;
        }
        返回的物品;
    }

如果你觉得它有用,请把它标记为其他人你的答案让我知道...

In Ajax, i am using autocompleteExender in my asp.net application, i write the service for that, when i run that service it is working fine, when i place autocompleteextender in asp.net page and assign properity for ajax autocompleteextender it is not working. this is my code service:

[WebMethod]
public string[] GetCompletionList(string prefixText)
{
    SqlConnection con=new SqlConnection
     ("server=******;database=Mydb;user id=***;password=****;");
    string sql = "Select productname from F_Product
      Where productname like '" + prefixText + "%'";
    SqlDataAdapter da = new SqlDataAdapter(sql, con); 
     try
    {
        DataTable dt = new DataTable();
        da.Fill(dt);
        string[] items = new string[dt.Rows.Count];
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            items.SetValue(dr[0].ToString(), i);
            i++;
        }
        return items; 
    }
    catch
    {
        return null;
    }
    finally
    {
        con.Close();

    }

and this is my ajax autocompleteextender code.

<asp:AutoCompleteExtender ID="AutoCompleteExtender1" MinimumPrefixLength="2"
     TargetControlID ="TextBox1" ServiceMethod="GetCompletionList"
       ServicePath="~/Autocomplete.asmx"
         runat="server">
    </asp:AutoCompleteExtender> 
    <asp:TextBox ID="TextBox1" runat="server"
     Width="213px"></asp:TextBox>
解决方案

Try this out.

[WebMethod]
    public string[] GetCompletionList(string prefixText)
    {

        string sql = "Select productname from F_Product Where productname like @prefixText ";
        SqlDataAdapter da = new SqlDataAdapter(sql, System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString);
        da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText + "%";
        DataTable dt = new DataTable();
        da.Fill(dt);
        string[] items = new string[dt.Rows.Count];
        int i = 0;
        foreach (DataRow dr in dt.Rows)
        {
            items.SetValue(dr["productname"].ToString(), i);
            i++;
        }
        return items;
    }

If you find it useful, please mark it as your answer else let me know...

这篇关于自动完成在阿贾克斯不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 05:52