我们如何使用asp.net,webservice和sql数据库集成jQuery自动完成功能? [英] How can we integrate jQuery autocomplete using asp.net, webservice and sql database?

查看:89
本文介绍了我们如何使用asp.net,webservice和sql数据库集成jQuery自动完成功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现为"jQuery Autocomplete and ASP.NET"提供的代码, 但由于要使用亚音速查询数据库而无法集成它.

I am trying to implement the code given for "jQuery Autocomplete and ASP.NET", but unable to integrate it because you are using subsonic to query database.

那么您能告诉我如何使用C#从asp.net中的webservice查询sqldatabase并将查询结果绑定到插件吗?

So can you tell me how to query sqldatabase and bind the query result to the plugin from webservice in asp.net using C#?

推荐答案

这是一个非常简单的任务,要注意的是jQuery自动完成扩展器需要一个值数组.这是我如何解析来自ASMX网络服务的标准XML结果以与jQuery自动完成扩展器一起使用的示例.

This is a pretty easy task, the catch is that the jQuery autocomplete extender expects an array of values. Here is example of how I parse the standard XML results from a ASMX web serivce to use with the jQuery autocomplete extender.

由于ASP.NET喜欢重写您的ID,因此您可以传入ClientID以获取动态ID.

Since ASP.NET likes to rewrite your ID's, you can pass in the ClientID to get the dynamic ID.

    $("#<%= TextBox1.ClientID %>").autocomplete("/Demo/WebSvc.asmx/SuggestCustomers", {
        parse: function(data) {
            var parsed = [];

            $(data).find("string").each(function() {
                parsed[parsed.length] = {
                    data: [$(this).text()],
                    value: $(this).text(),
                    result: [$(this).text()]
                };
            });
            return parsed;
        },
        dataType: "xml"
    });

这是关联的Web服务的外观,请记住取消注释Web服务上的[ScriptService]属性:

Here is what the associated web service would look like, remember to uncomment the [ScriptService] attribute on the web service:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebSvc: WebService
{
    [WebMethod]
    public string[] SuggestedCustomers(string q)
    {
        // Do Query

        // Add items into string array
        List<string> items = new List<string>();
        while (dr.Read())
        {
            items.Add(dr[0].ToString());
        }

        // Return array
        return items.ToArray();
    }

}

这篇关于我们如何使用asp.net,webservice和sql数据库集成jQuery自动完成功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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