自动完成扩展程序问题?

时间:2010-07-06 05:35:14

标签: asp.net ajax

我正在使用autocomplete extender,我写了一个webservice,当我运行webservice时,webservice工作正常。但是当我运行我的aspx页面时,它没有显示任何自动完成没有显示只有文本框的东西。这是我的代码......

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService 
{

    SqlConnection con;SqlDataAdapter da;
    public WebService () {

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

[WebMethod]
public string[] GetTitleInfo(string prefixText)
{
    int count = 10;
    string sqry = "select * from news_upload where newstitle like @prefixText";
    da = new SqlDataAdapter(sqry, "server=localhost;database=tfcnew;user id=sa;password=sql123");
    da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 100).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["newstitle"].ToString (), i);
        i++;
    }
    return items;
}

这是(上述)服务。

        

    <asp:TextBox ID="txtcomplete" runat ="Server" ></asp:TextBox>
        <asp:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server" MinimumPrefixLength ="1" ServiceMethod ="GetSuggestions" ServicePath="~/WebService2.asmx" TargetControlID ="txtcomplete" >
        </asp:AutoCompleteExtender> 

这是aspx代码..

你能帮我吗?谢谢。

1 个答案:

答案 0 :(得分:0)

在类声明

之前添加“[System.Web.Script.Services.ScriptMethod]”
[System.Web.WebService(Namespace = "http://tempuri.org/")] 
[System.Web.WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.Web.Script.Services.ScriptMethod]
public class WebService : System.Web.Services.WebService {
...
...
...

如果它已经在您的代码中,您可以检查服务路径。使用Fiddler或Firebug查看是否有实际的服务调用。如果不工作,那么从aspx页面的paga_Load事件设置服务路径

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
   If Not Page.IsPostBack() Then
      AutoCompleteExtender2.ServicePath = ResolveUrl("~/mywebservice.asmx")                    
   End If
End Sub