我们如何使用asp.net,webservice和sql数据库集成jQuery自动完成?

时间:2021-12-06 09:44:01

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.

我正在尝试实现为“jQuery Autocomplete和ASP.NET”提供的代码,但无法集成它,因为您使用subsonic来查询数据库。

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

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

2 个解决方案

#1


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.

这是一个非常简单的任务,问题是jQuery自动完成扩展器需要一组值。下面是我如何从ASMX Web服务解析标准XML结果以与jQuery自动完成扩展程序一起使用的示例。

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

由于ASP.NET喜欢重写您的ID,因此您可以传入ClientID以获取动态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"
    });

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

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

[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();
    }

}

#2


I am not fluent in asp.net but fundamentally like most web coding questions this involves breaking your problem into smaller ones.

我不熟悉asp.net,但从根本上说,大多数网络编码问题都涉及到将问题分解为较小的问题。

From an architectural perspective your components might include the following...

从架构的角度来看,您的组件可能包括以下内容......

  • a service layer that potentially uses your db etc to answer or produce a result for your query.
  • 可能使用您的数据库等来回答或生成查询结果的服务层。

  • a web component or service entry point that uses the completed service mentioned above to return the data in a format the browesrr can easily understand - eg JSON.
  • 一个Web组件或服务入口点,它使用上面提到的已完成的服务以browesrr可以轻松理解的格式返回数据 - 例如JSON。

  • some javascript using jquery which invokes the end point define in the immediate point above.
  • 一些javascript使用jquery调用结束点定义在上面的直接点。

  • write unit tests for all the above components - don't forget to test failure cases because as we all know software sometimes fails ...
  • 为所有上述组件编写单元测试 - 不要忘记测试失败案例,因为我们都知道软件有时会失败...

#1


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.

这是一个非常简单的任务,问题是jQuery自动完成扩展器需要一组值。下面是我如何从ASMX Web服务解析标准XML结果以与jQuery自动完成扩展程序一起使用的示例。

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

由于ASP.NET喜欢重写您的ID,因此您可以传入ClientID以获取动态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"
    });

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

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

[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();
    }

}

#2


I am not fluent in asp.net but fundamentally like most web coding questions this involves breaking your problem into smaller ones.

我不熟悉asp.net,但从根本上说,大多数网络编码问题都涉及到将问题分解为较小的问题。

From an architectural perspective your components might include the following...

从架构的角度来看,您的组件可能包括以下内容......

  • a service layer that potentially uses your db etc to answer or produce a result for your query.
  • 可能使用您的数据库等来回答或生成查询结果的服务层。

  • a web component or service entry point that uses the completed service mentioned above to return the data in a format the browesrr can easily understand - eg JSON.
  • 一个Web组件或服务入口点,它使用上面提到的已完成的服务以browesrr可以轻松理解的格式返回数据 - 例如JSON。

  • some javascript using jquery which invokes the end point define in the immediate point above.
  • 一些javascript使用jquery调用结束点定义在上面的直接点。

  • write unit tests for all the above components - don't forget to test failure cases because as we all know software sometimes fails ...
  • 为所有上述组件编写单元测试 - 不要忘记测试失败案例,因为我们都知道软件有时会失败...