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

查看:14
本文介绍了我们如何使用 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.

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

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天全站免登陆