如何在不使用ajax的情况下制作自动完成文本框? [英] How to make an auto complete textbox without using ajax?
本文介绍了如何在不使用ajax的情况下制作自动完成文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!
我想创建一个文本框,它将自动从我的数据库中完成字段名称ContractNo而不使用ajax。任何帮助?
这是我尝试过的:
在我的.ASPX
< asp:内容 ID = Content1 ContentPlaceHolderID = head Runat = 服务器 >
< head >
< title > 使用j进行webservice的自动完成文本框查询< / title >
< link href = http://ajax.googleapis.com/ajax /libs/jqueryui/1.8.1/themes/base/jquery-ui.css\" rel = 样式表 type = text / css / >
< script type = text / javascript src = http://ajax.googleapis.com/ajax/libs /jquery/1.4.2/jquery.min.js\"> < < span class =code-leadattribute> / script >
< script type = text / javascript src = http://ajax.googleapis.com/ajax/libs/jqueryui/ 1.8.1 / jquery-ui.min.js > < / script >
< script type = text / javascript >
$(document).ready(function(){
SearchText();
});
函数SearchText(){
$(。autosuggest)。autocomplete({
source:function(request,response){
$ .ajax({
键入:POST,
contentType:application / json; charset = utf-8,
url:AutoComplete.asmx / GetAutoCompleteData,
data:{'username': '+ document.getElementById('contract_num')。value +'},
dataType:json,
success:function(data){
response(data.d) ;
},
错误:函数(结果){
alert(错误);
}
});
}
});
}
< / script >
< / head >
我添加了一个Web服务方法:
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Services;
使用 System.Data;
使用 System.Data.SqlClient;
///
/// 自动完成的摘要说明
///
[WebService(Namespace = http://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class 自动完成:System.Web.Services.WebService {
public AutoComplete(){
// 如果使用设计的组件,则取消注释以下行
// InitializeComponent();
}
[WebMethod]
public List GetAutoCompleteData( string username)
{
List result = new List();
使用(SqlConnection con = new SqlConnection( 数据源= 192.168.100.252;集成安全性= true;初始目录= AMICASSA_Checklist))
{
使用(SqlCommand cmd = new SqlCommand( 从rawdata中选择DISTINCT Contract_num,其中Contact_num LIKE'%'+ @ SearchText +'%',con))
{
con.Open();
cmd.Parameters.AddWithValue( @ SearchText,用户名);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr [ Contract_num]。ToString());
}
返回结果;
}
}
}
}
但是没有任何事情发生:(
解决方案
(document).ready(function(){
SearchText();
});
function SearchText(){
< blockquote>(。autosuggest)。autocomplete({
source:function(request,response){
.ajax({
type:POST,
contentType:application / json; charset = utf-8,
url:AutoComplete.asmx / GetAutoCompleteData,
data:{'username':'+ document.getElementById ('contract_num')。value +'},
dataType:json,
成功:函数(数据){
response(data.d);
} ,
错误:函数(结果){
alert(错误);
}
});
}
});
}
< / script >
< / head >
我添加了一个Web服务方法:
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Services;
使用 System.Data;
使用 System.Data.SqlClient;
///
/// 自动完成的摘要说明
///
[WebService(Namespace = http://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class 自动完成:System.Web.Services.WebService {
public AutoComplete(){
// 如果使用设计的组件,则取消注释以下行
// InitializeComponent();
}
[WebMethod]
public List GetAutoCompleteData( string username)
{
List result = new List();
使用(SqlConnection con = new SqlConnection( 数据源= 192.168.100.252;集成安全性= true;初始目录= AMICASSA_Checklist))
{
使用(SqlCommand cmd = new SqlCommand( 从rawdata中选择DISTINCT Contract_num,其中Contact_num LIKE'%'+ @ SearchText +'%',con))
{
con.Open();
cmd.Parameters.AddWithValue( @ SearchText,用户名);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr [ Contract_num]。ToString());
}
返回结果;
}
}
}
}
但是没有任何事情发生:(
Hi guys!
I would like to create a text box that will auto complete the field name ContractNo from my database without using ajax. Any help?
here is what i tried:
In my .ASPX
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<head>
<title>AutoComplete Textbox with webservice using jQuery</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
SearchText();
});
function SearchText() {
$(".autosuggest").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "AutoComplete.asmx/GetAutoCompleteData",
data: "{'username':'" + document.getElementById('contract_num').value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
}
});
}
</script>
</head>
I add a Web service Method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
///
/// Summary description for AutoComplete
///
[WebService(Namespace = "http://tempuri.org/")]
[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 List GetAutoCompleteData(string username)
{
List result = new List();
using (SqlConnection con = new SqlConnection("Data Source=192.168.100.252;Integrated Security=true;Initial Catalog=AMICASSA_Checklist"))
{
using (SqlCommand cmd = new SqlCommand("select DISTINCT Contract_num from rawdata where Contact_num LIKE '%'+@SearchText+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", username);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["Contract_num"].ToString());
}
return result;
}
}
}
}
But nothing Happens :(
解决方案
(document).ready(function () { SearchText(); }); function SearchText() {
(".autosuggest").autocomplete({ source: function (request, response) {
.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "AutoComplete.asmx/GetAutoCompleteData", data: "{'username':'" + document.getElementById('contract_num').value + "'}", dataType: "json", success: function (data) { response(data.d); }, error: function (result) { alert("Error"); } }); } }); } </script> </head>
I add a Web service Method:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Data; using System.Data.SqlClient; /// /// Summary description for AutoComplete /// [WebService(Namespace = "http://tempuri.org/")] [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 List GetAutoCompleteData(string username) { List result = new List(); using (SqlConnection con = new SqlConnection("Data Source=192.168.100.252;Integrated Security=true;Initial Catalog=AMICASSA_Checklist")) { using (SqlCommand cmd = new SqlCommand("select DISTINCT Contract_num from rawdata where Contact_num LIKE '%'+@SearchText+'%'", con)) { con.Open(); cmd.Parameters.AddWithValue("@SearchText", username); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { result.Add(dr["Contract_num"].ToString()); } return result; } } } }
But nothing Happens :(
这篇关于如何在不使用ajax的情况下制作自动完成文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文