日期:2014-05-17  浏览次数:20496 次

请教一个ASP.NET中,AjaxControlToolkit里的AutoCompleteExtender控件的问题!在线等!
前台AutoCompleteExtender.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoCompleteExtender.aspx.cs" Inherits="AutoCompleteExtender" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
   
  <asp:ScriptManager ID="ScriptManager1" runat="server">
  </asp:ScriptManager>
  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  <cc1:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" 
  DelimiterCharacters="" Enabled="True" MinimumPrefixLength="1" 
  ServiceMethod="GetCompletionList" ServicePath="" TargetControlID="TextBox1" 
  UseContextKey="True">
  </cc1:AutoCompleteExtender>
   
  </div>
  </form>
</body>
</html>

后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class AutoCompleteExtender : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }

  [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
  public static string[] GetCompletionList(string prefixText, int count, string contextKey)
  {
  SqlConnection conn = new SqlConnection("Server=(Local);Uid=sa;pwd=15911113281;DataBase=db_Siemens");
  SqlCommand cmd;
  string cmdString = "select UserName from t_StaffInfo WHERE UserName LIKE '" + prefixText + "%'";
  // "Select CompanyName from Customers WHERE CompanyName LIKE '" +
  // prefixText + "%'";
  //conn = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
  //// Put this string on one line in your code
  cmd = new SqlCommand(cmdString, conn);
  conn.Open();

  SqlDataReader myReader;
  List<string> returnData = new List<string>();

  myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

  while (myReader.Read())
  {
  returnData.Add(myReader["UserName"].ToString());
  }

  return returnData.ToArray();
  }
}

为何运行时,在文本框输入后,无法检索出相关的呢??求助!!调了很久了!!

------解决方案--------------------
ServicePath=""
DelimiterCharacters=""
是打错还是确实是空的?
------解决方案--------------------
帮顶一下
------解决方案--------------------
引用楼主 encienqi 的帖子:
调了很久了!!