在ASP.VB.Net中自动完成TextBox [英] Autocomplete TextBox in ASP.VB.Net

查看:83
本文介绍了在ASP.VB.Net中自动完成TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人可以帮助解决如何在asp.vb.net中编写自动完成TextBox的代码,我会很高兴。

我能够在VB.Net Windows应用程序中执行此操作,但无法在Web应用程序中执行相同操作。以下是我在Page_load下尝试的内容:







I'd be glad if someone could help with how to write a code for autocomplete TextBox in asp.vb.net.
I'm able to do that in VB.Net Windows application, but unable to do same in the Web Application. The following is what I've tried under Page_load:



Try
            cmd = New SqlCommand("Select SubjectName from ProgramDetails.Subjects", cn)
            dr1 = cmd.ExecuteReader
            While dr1.Read
                txtSearch.AutoCompleteType.GetType.Name.Trim(dr1(0))
            End While
            dr1.Close()
            dr1 = Nothing
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
        End Try

推荐答案

http://jqueryui.com/autocomplete/ [ ^ ]


看起来您的目标是实现服务器端方法来实现自动完成功能。在ASP.NET Web应用程序中, Page_Load 事件不是实现它的地方。您通常会使用 TextBox TextChanged 事件。但是当你走上这条路线时你应该小心,因为 TextChange 事件导致服务器触发并触发回发并点击每次更改/键入 TextBox 中的内容时,都会使用数据库。您可以立即缓存数据以减少数据库调用,但仍然需要使用搜索结果重新绑定数据控件 - 这有点贵。



您最接近的选项如果你想避免做一些客户端脚本使用:自动完成示例 [ ^ ]



这是一个显示如何使用它的文章: ASP.Net中的AutoCompleteExtender

[ ^ ]



实现自动完成功能的理想方法是使用客户端方法 AJAX 。您可以使用其他人已经建议的 jQuery ,并将其与AJAX结合使用,以便与数据库中的数据进行通信。以下是关于以下内容的精彩文章:使用jQuery AJAX和ASP.NET与数据库通信的多种方式 [ ^ ]



一旦掌握了网络如何将数据从服务器传送到客户端,您就可以开始使用自动完成功能了。网上有很多可用的例子。以下是其中一些:



TextBox AutoComplete with ASP.NET和jQuery UI | DotNetCurry [ ^ ]

填充jQuery自动完成使用ASP.Net中的Web服务从数据库中获取TextBox

JQuery - ASP.Net中的自动完整文本框
It seems like you are aiming for a server-side approach to implement an autocomplete feature. In ASP.NET web app, Page_Load event isn't the place to implement that. You would typically use the TextChanged event of the TextBox. But you should be careful when taking to this route as TextChange event causes the server to fire and trigger a postback and hits your database each time you change/type something in the TextBox. You could surely cache the data at once to reduce database calls but still you need to rebind your data control with the search results - which is kinda expensive.

Your closest option if you want to avoid doing some client-side scripting is using : AutoComplete Sample[^]

Here's an article that shows how to use it: AutoCompleteExtender in ASP.Net
[^]

The ideal way to implement an autocomplete feature is using a client-side approach with AJAX. You could use jQuery as already suggested by others and couple that with AJAX to communicate with your data from your database. Here's a great writeup about: Many ways to communicate with your database using jQuery AJAX and ASP.NET[^]

Once you get a good grasp on how the web works regarding communicating your data from the server to the client, then you can start working on your autocomplete feature. There are tons of examples in the net that are available. Here are a few of them:

TextBox AutoComplete with ASP.NET and jQuery UI | DotNetCurry[^]
Populate jQuery AutoComplete TextBox from Database using Web Service in ASP.Net
JQuery - Auto Complete Text Box In ASP.Net


这篇关于在ASP.VB.Net中自动完成TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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