我在aspx页面上有一个文本框,一个脚本管理器和一个自动完成扩展器。我试图简单地在文本框中自动完成,目前我什么都没得到。我错过了什么?当我尝试调试代码时,断点永远不会超出GetComplete方法中的第一个大括号。我在VS2010上
标记:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="AjaxAutocomplete._Default" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
DelimiterCharacters="" Enabled="True" ServiceMethod="GetComplete" MinimumPrefixLength="3" TargetControlID="TextBox1">
</asp:AutoCompleteExtender>
</asp:Content>
代码背后的代码:
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;
using System.Configuration;
namespace AjaxAutocomplete
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Script.Services.ScriptMethod]
[System.Web.Services.WebMethod]
public static List<string> GetComplete(string Prefixtetxt)
{
List<string> returnList = new List<string>();
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
using (SqlConnection conn = new SqlConnection(cs))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p = new SqlParameter("@drug_name", Prefixtetxt);
//cmd.Parameters.AddWithValue("@drug_name", p);
cmd.Parameters.Add("@drug_name", SqlDbType.VarChar);
using (SqlDataReader reader = cmd.ExecuteReader())
{
returnList.Add(Convert.ToString(reader["drug_name"]));
}
}
}
return returnList;
}
}
}
来自Sql Server的sproc
create proc spFindAllDrugs
@drug_name varchar(100)
as
begin
select distinct drug_name
from rx
where drug_name like '%' + @drug_name + '%'
end
答案 0 :(得分:1)
看起来您没有将SqlCommand
与连接相关联,也没有告诉命令存储过程的名称。尝试修改您的代码,如图所示。
using (SqlCommand cmd = new SqlCommand("spFindAllDrugs"))
{
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;