Ajax自动完成功能不适用于搜索框asp.net c#

时间:2012-09-16 05:47:40

标签: c# asp.net webforms asp.net-ajax

我正在尝试创建一个谷歌搜索框。我没有得到任何结果。我只是试图制作自动完成功能,现在还没有完成搜索.Below就是我尝试过的。
< ====== Directory.aspx文件=======>

<ajaxToolkit:ToolkitScriptManager  ID="ScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>

       <div class="search-box">
           <span class="strong">Search Members: </span> 
<ajaxToolkit:AutoCompleteExtender ID="autoComplete1" runat="server"
  EnableCaching="true"
  BehaviorID="AutoCompleteEx"
  MinimumPrefixLength="2"
  TargetControlID="myTextBox"
  ServicePath="AutoComplete.asmx"
  ServiceMethod="GetCompletionList" 
  CompletionInterval="100"  
  CompletionSetCount="20"
  CompletionListCssClass="autocomplete_completionListElement"
  CompletionListItemCssClass="autocomplete_listItem"
  CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"
  DelimiterCharacters=";, :"
  ShowOnlyCurrentWordInCompletionListItem="true">
  <Animations>
  <OnShow>
  <Sequence>
  <%-- Make the completion list transparent and then show it --%>
  <OpacityAction Opacity="0" />
  <HideAction Visible="true" />

  <%--Cache the original size of the completion list the first time
    the animation is played and then set it to zero --%>
  <ScriptAction Script="// Cache the size and setup the initial size
                                var behavior = $find('AutoCompleteEx');
                                if (!behavior._height) {
                                    var target = behavior.get_completionList();
                                    behavior._height = target.offsetHeight - 2;
                                    target.style.height = '0px';
                                }" />
  <%-- Expand from 0px to the appropriate size while fading in --%>
  <Parallel Duration=".4">
  <FadeIn />
  <Length PropertyKey="height" StartValue="0" 
    EndValueScript="$find('AutoCompleteEx')._height" />
  </Parallel>
  </Sequence>
  </OnShow>
  <OnHide>
  <%-- Collapse down to 0px and fade out --%>
  <Parallel Duration=".4">
  <FadeOut />
  <Length PropertyKey="height" StartValueScript=
    "$find('AutoCompleteEx')._height" EndValue="0" />
  </Parallel>
  </OnHide>
  </Animations>
  </ajaxToolkit:AutoCompleteExtender>
<asp:TextBox ID="myTextBox" AutoCompleteType="FirstName" placeholder="Type First Name Here" runat="server"></asp:TextBox>
</div>

AutoComplete.asmx.cs文件

// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;

///<summary>
/// Summary description for AutoComplete
///</summary>

[WebService(Namespace = "localhost")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, 
// uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{

    public AutoComplete()
    {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public string[] GetCompletionList(string prefixText, int count)
    {
        //ADO.Net
        MySqlConnection cn = new MySqlConnection("Database=bvshree;Data Source=localhost;User Id=root;Password=axcva4@@@3");

        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandType = CommandType.Text;
        //Compare String From Textbox(prefixText) AND String From 
        //Column in DataBase(CompanyName)
        //If String from DataBase is equal to String from TextBox(prefixText) 
        //then add it to return ItemList
        //-----I defined a parameter instead of passing value directly to 
        //prevent SQL injection--------//
        cmd.CommandText = "select * from family_header Where Self_Name like @myParameter";
        cmd.Parameters.AddWithValue("@myParameter", "%" + prefixText + "%");

            cn.Open();
            cmd.ExecuteNonQuery();
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            da.Fill(ds);


            cn.Close();

        dt = ds.Tables[0];

        //Then return List of string(txtItems) as result
        List<string> txtItems = new List<string>();
        String dbValues;

        foreach (DataRow row in dt.Rows)
        {
            //String From DataBase(dbValues)
            dbValues = row["Self_Name"].ToString();
            dbValues = dbValues.ToLower();
            txtItems.Add(dbValues);
        }

        return txtItems.ToArray();
    }
}

1 个答案:

答案 0 :(得分:0)

我在很长一段时间后再次看到这个问题,因为没有发布任何回复,我认为我应该自己回复。 问题在于代码的样式,生成的列表的不透明度设置为零,并且未按预期更改。所以我只是从它移除动画来获得解决方案。 :)