问题描述
我在Visual Studio 2008中实现autocompleteextender时遇到问题,当我在文本框中输入任何文本时,它将自动捕获关键字并从数据库中建议单词.没有发现错误,但是当我输入时它没有用文本框内的任何关键字..
这是我的aspx文件:
I m having problem when implementing autocompleteextender in visual studio 2008, where when i enter my any text in my textbox,it will automaticly capture the keywords and suggest words from database..no errors found,but it didnt works when i enter any keyword inside the textbox..
This is my aspx file :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="AutoComplete.asmx" />
</Services>
</asp:ScriptManager>
<div>
<asp:TextBox runat="server" ID="myTextBox" Width="300" autocomplete="off" />
<ajaxToolkit:AutoCompleteExtender
runat="server"
ID="autoComplete1"
TargetControlID="myTextBox"
ServicePath="AutoComplete.asmx"
ServiceMethod="GetCompletionList"
MinimumPrefixLength="2"
CompletionInterval="1000"
EnableCaching="true"
CompletionSetCount="12" />
</div>
</form>
</body>
</html>
这是我的asmx.cs文件:
This is my asmx.cs file:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for AutoComplete
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{
[WebMethod]
public string[] GetCompletionList(string prefixText)
{
DataSet dtst = new DataSet();
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.AppSettings["%$ ConnectionStrings:C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL.1\MSSQL\DATA\DATABASE1.MDFConnectionString %"]);
string strSql = "SELECT Company_Name FROM Table1 WHERE CompanyName LIKE '" + prefixText + "%' ";
SqlCommand sqlComd = new SqlCommand(strSql, sqlCon);
sqlCon.Open();
SqlDataAdapter sqlAdpt = new SqlDataAdapter();
sqlAdpt.SelectCommand = sqlComd;
sqlAdpt.Fill(dtst);
string[] cntName = new string[dtst.Tables[0].Rows.Count];
int i = 0;
try
{
foreach (DataRow rdr in dtst.Tables[0].Rows)
{
cntName.SetValue(rdr["CompanyName"].ToString(), i);
i++;
}
}
catch { }
finally
{
sqlCon.Close();
}
return cntName;
}
}
我应该修改任何代码部分吗?
我可以使用使用sqldatasource的autocompleteextender来调用数据库吗?
任何想法将不胜感激.
参考: http://www.c-sharpcorner.com/UploadFile/munnamax/AutocompleteExtender08062007113113AM/AutocompleteExtender. aspx [ ^ ]
Should i modify any code part?
Can i use the autocompleteextender using sqldatasource to call database?
Any ideas will be much appreciated.
Reference:http://www.c-sharpcorner.com/UploadFile/munnamax/AutocompleteExtender08062007113854AM/AutocompleteExtender.aspx[^]
推荐答案
我应该修改任何代码部分吗?
我可以使用使用sqldatasource的autocompleteextender来调用数据库吗?
任何想法将不胜感激.
参考: http://www.c-sharpcorner.com/UploadFile/munnamax/AutocompleteExtender08062007113113AM/AutocompleteExtender. aspx [ ^ ]
Should i modify any code part?
Can i use the autocompleteextender using sqldatasource to call database?
Any ideas will be much appreciated.
Reference:http://www.c-sharpcorner.com/UploadFile/munnamax/AutocompleteExtender08062007113854AM/AutocompleteExtender.aspx[^]
<asp:TextBox ID="edCliente" runat="server" MaxLength="50" Width="250px" ValidationGroup="cabecalho"></asp:TextBox>
<cc1:AutoCompleteExtender
ID="AutoCompleteClientes" runat="server"
Enabled="True" TargetControlID="edCliente"
ServiceMethod="PesquisaClientesProposta" ServicePath="~/AutoCompleteClientes.asmx"
CompletionSetCount="12" MinimumPrefixLength="1"
CompletionInterval="0">
</cc1:AutoCompleteExtender>
这是asmx代码:
And this is the asmx code:
[WebMethod]
public string[] PesquisaClientesProposta(string prefixText)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["prodMIS"].ToString();
conn.Open();
StringBuilder ComandoSQL = new StringBuilder();
ComandoSQL.AppendLine("SELECT DISTINCT NOME_CLIENTE NOME FROM MIS..CONTRACT CTR ");
ComandoSQL.AppendLine("WHERE CTR.NOME_CLIENTE LIKE @NOME ");
ComandoSQL.AppendLine("ORDER BY NOME_CLIENTE ");
SqlParameter ParamNome = new SqlParameter("NOME", System.Data.SqlDbType.VarChar, 35, "NOME");
ParamNome.Value = String.Concat(prefixText, "%");
SqlCommand CMD = new SqlCommand(ComandoSQL.ToString(), conn);
CMD.Parameters.Add(ParamNome);
SqlDataAdapter SQLda = new SqlDataAdapter(CMD);
SqlCommandBuilder CMDbuilder = new SqlCommandBuilder(SQLda);
SqlDataReader SQLReader;
SQLReader = CMD.ExecuteReader();
List<string> listClientes = new List<string>();
//listClientes.Add("-TODOS-");
while (SQLReader.Read())
{
listClientes.Add(SQLReader["NOME"].ToString());
}
conn.Close();
return listClientes.ToArray();
}
我不知道这是否对您有帮助,但是对我来说效果很好.
问候,
Obst
I don''t know if that helps you, but it''s working fine for me.
Regards,
Obst
这篇关于从数据库自动完成.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!