Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the automatic completion function of asp.net+ajax+sqlserver

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to implement the asp.net+ajax+sqlserver auto-completion function. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Description: the database connection string is in the web.config file, and the official Northwind database is used for convenience.

Reference (salute to its author):

Http://www.loveweb8.com/plus/demo.php?aid=57 this example is the html source code. The automatic completion function is realized by using jquery.autocomplete plug-in and js. Because my need is to combine the sqlserver database table to achieve automatic completion function. The next step is to convert the database table to an js array, and it's natural to think of ajax.

The second part of this article on the two basic development modes of Asp.NetAjax is an example of js calling webservice.

Code parsing.

1. Add a webservice file.

Add a new item, "WCF Service with AJAX enabled", and name the new file DBService.svc.

two。 Add a function to DBService.svc, and the return value of the function is the data that is prompted to complete automatically.

The code is as follows:

[OperationContract]

Public string getSortList ()

{

List sorts = new List ()

Using (SqlConnection cn = new SqlConnection (ConfigurationManager.ConnectionStrings ["zhui.pc"] .ConnectionString))

{

Cn.Open ()

SqlCommand cmd = new SqlCommand ("select [LastName] from [dbo]. [Employees]", cn)

DataTable dt = new DataTable ()

Using (SqlDataAdapter da = new SqlDataAdapter (cmd))

{

Da.Fill (dt)

}

Foreach (DataRow row in dt.Rows)

{

Sorts.Add (row [0] .ToString ())

}

Cn.Close ()

}

Return string.Join ("," sorts.ToArray ())

}

3. Add js and css files to the default.aspx file:

The code is as follows:

4. Call webservice in the $() .ready (function ()) function in the head section of default.aspx to get the automatically completed data and associate the data to the input box.

The code is as follows:

Ready (function () {)

NewsSort.getSortList (OnComplete, OnFailed, null)

Function OnComplete (args, context) {

('# MainContent_searchBox') .AutoComplete ({

'data': args.split (",")

'itemHeight': 20

'listDirection': 'down'

'width': 280,

}). AutoComplete ('show')

}

Function OnFailed (args) {

Alert ("error!")

}

});

Thank you for reading! This is the end of the article on "how to achieve the automatic completion of asp.net+ajax+sqlserver". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report