使用jquery,ajax和C#自动完成的Facebook类型搜索框 [英] Facebook type search box with auto complete using jquery, ajax and C#

查看:72
本文介绍了使用jquery,ajax和C#自动完成的Facebook类型搜索框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

i需要自动完成文本框,如facebook搜索但不需要照片,我使用此代码工作,但我将此代码包含在我的项目中,我得到错误没有方法自动完成

 search.cs 

使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Web.Services;
使用 System.Data;

public partial class _Default:System.Web.UI.Page
{
protected void 提交( object sender,EventArgs e)
{
lblParticular.Text = Request.Form [txtSearch.UniqueID] .ToString();
lblStockId.Text = Request.Form [hfStockId.UniqueID] .ToString();
}

[WebMethod]
public static string getItem( string 搜索,字符串 StockId )
{
string Item = ;
bl_M_StockCreation obj = new bl_M_StockCreation();
string [] itemArr = StockId.Split(' _ );
DataSet dsStock = obj.getItem(Search,itemArr [ 0 ]);
if (dsStock.Tables [ 0 ]。Rows.Count!= 0
{
dsStock.Tables [ 0 ]。TableName = StockDetails;
Item = dsStock.GetXml();
}

return 项目;
}
}



 Service.asmx 

使用系统;
使用 System.Collections.Generic;
使用 System.Web;
使用 System.Web.Services;
使用 System.Data.SqlClient;
使用 System.Configuration;
使用 System.Web.Script.Services;
使用 System.Data;

/// < 摘要 >
/// Service_CS的摘要说明
/// < / summary >
[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 Service(){
}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string [] GetParticular( string 前缀)
{
List< string>详情= new List< string>();
使用(SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings [ constr]。ConnectionString;

使用(SqlCommand cmd = new SqlCommand( [USP_M_Test]))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ Search,前缀);
cmd.Parameters.AddWithValue( @ intMode 1 );
cmd.Connection = conn;
conn.Open();
使用(SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
Particulars.Add( string .Format( {0} - {1},sdr [ 详情],sdr [ StockId])) ;
}
}
conn.Close();
}
return Particulars.ToArray();
}
}




}



请帮助我

解决方案

你已经在aspx中添加了这个函数

 


(document).ready(function(){


.ajax({
type: POST
url: / Service / Service.asmx / getItem
dataType: json
data: {}
contentType: application / json; charset = utf-8
成功:函数(数据){
var datafromServer = data.d.split( );

hello,
i need autocomplete textbox like facebook search but doesn't need photo,am using this code its working but i am include this code to my project and i got the error has no method auto complete

search.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Submit(object sender, EventArgs e)
    {      
        lblParticular.Text = Request.Form[txtSearch.UniqueID].ToString();
        lblStockId.Text = Request.Form[hfStockId.UniqueID].ToString();
    }

    [WebMethod]
    public static string getItem(string Search, string StockId)
    {
        string Item="";
        bl_M_StockCreation obj = new bl_M_StockCreation();
        string[] itemArr = StockId.Split('_');
        DataSet dsStock = obj.getItem(Search, itemArr[0]);
        if (dsStock.Tables[0].Rows.Count != 0)
        {
            dsStock.Tables[0].TableName = "StockDetails";
            Item = dsStock.GetXml();
        }
       
        return Item;
    }
}


Service.asmx

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Script.Services;
using System.Data;

/// <summary>
/// Summary description for Service_CS
/// </summary>
[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 Service : System.Web.Services.WebService {

    public Service () {
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string[] GetParticular(string prefix)
    {
        List<string> Particulars = new List<string>();
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

            using (SqlCommand cmd = new SqlCommand("[USP_M_Test]"))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Search", prefix);
                cmd.Parameters.AddWithValue("@intMode", 1);
                cmd.Connection = conn;
                conn.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        Particulars.Add(string.Format("{0}-{1}", sdr["Particulars"], sdr["StockId"]));
                    }
                }
                conn.Close();
            }
            return Particulars.ToArray();
        }
    }




}


pls help me

解决方案

u have add this function in aspx


(document).ready(function() {


.ajax({ type: "POST", url: "/Service/Service.asmx/getItem", dataType: "json", data: "{}", contentType: "application/json; charset=utf-8", success: function(data) { var datafromServer = data.d.split(":");


这篇关于使用jquery,ajax和C#自动完成的Facebook类型搜索框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆