将 ASP.NET Web 表单发送到 JQuery 自动完成的最佳实践
我想要做的是从 MS SQL 客户表电子邮件列中检索所有电子邮件,并使用 JQuery 自动完成功能填充它们。当前系统使用的是VB.NET 2.0。
我所做的是获取所有电子邮件并将它们放入 DataTable 中并循环并将它们放入由“,”分隔的字符串中。将该字符串放入隐藏框中。 JQuery 从该隐藏框中检索值并使用“array = emails.split(",");”构建一个数组。这是代码。
它在开发服务器上运行得很好,因为我们只有 2,000 多条记录,但当我将其放在有 80,000 多条记录的实时服务器上时,它会永远加载。
前端
<script type="text/javascript">
$(function() {
var emails = $("#EmailList").val();
var emailList = emails.split(",");
$(".email-autocomplete").autocomplete({
source: emailList
});
});
</script>
<asp:TextBox class="email-autocomplete" ID="txtEmailAddress"
runat="server" style="width:300px"></asp:TextBox>
<asp:HiddenField ID="EmailList" runat="server" />
后端
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
FetchEmailList()
End Sub
Private Sub FetchEmailList()
Dim dt As Data.DataTable = GetCustomers()
Dim i As Integer
Dim _emails As String
For i = 0 To dt.Rows.Count()-1
If IsDBNull(dt.Rows(i).Item("Email")) = False Then
_emails &= dt.Rows(i).Item("Email") & ","
End If
Next
If _emails.length > 0 Then
EmailList.Value = _emails.substring(0,_emails.length-1)
End If
End Sub
我想出了两种解决方案 -
当我从数据库服务器检索电子邮件时,我将使用函数 TableToStr 并将所有由“,”分隔的电子邮件放在一个字段中,VB.NET 获取该值并将其放入隐藏的盒子。这里我们可以通过后端的数据表删除循环。然而,JQuery 仍然需要“拆分”该字符串来构建一个数组。
从数据库获取电子邮件,构建 JSON 并将其返回到 JQuery。(我已经在 ASP.NET MVC3 中使用 C# 完成了此操作,使用“返回 JSON”非常简单,但需要做一些研究如何在 VB 中执行此操作。 NET 2.0)。
当我们的数据源非常大时,处理自动完成的最佳实践是什么?
What I want to do is retrieve all emails from MS SQL Customer table Email column and populate them in using JQuery autocomplete feature. The current system is using VB.NET 2.0.
What I have done is get all emails and put them in DataTable and loop through and put them in the string delimited by ",". Put that string into hidden box. JQuery retrieve value from that hidden box and build an array using "array = emails.split(",");". Here is the code.
It works pretty good on development server since we have only 2,000+ records but it is loading forever when I put that on the live server where there are 80,000+ records.
Front End
<script type="text/javascript">
$(function() {
var emails = $("#EmailList").val();
var emailList = emails.split(",");
$(".email-autocomplete").autocomplete({
source: emailList
});
});
</script>
<asp:TextBox class="email-autocomplete" ID="txtEmailAddress"
runat="server" style="width:300px"></asp:TextBox>
<asp:HiddenField ID="EmailList" runat="server" />
Back End
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
FetchEmailList()
End Sub
Private Sub FetchEmailList()
Dim dt As Data.DataTable = GetCustomers()
Dim i As Integer
Dim _emails As String
For i = 0 To dt.Rows.Count()-1
If IsDBNull(dt.Rows(i).Item("Email")) = False Then
_emails &= dt.Rows(i).Item("Email") & ","
End If
Next
If _emails.length > 0 Then
EmailList.Value = _emails.substring(0,_emails.length-1)
End If
End Sub
I come up with two solutions -
When I retrieve emails from database server, I will use function TableToStr and put all emails delimited by "," in one field and VB.NET get that values and put it in hidden box. Here we can remove Looping through datatable in back end. However, JQuery still needs to "split" that string to build an array.
Get emails from DB, build JSON and return it to JQuery.(I have done that in ASP.NET MVC3 with C# which is pretty easy using "return JSON" but need to do some researches how to do it in VB.NET 2.0).
What is the best practice to deal with autocomplete when our data source is quite large.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我会将后端代码移至通用 HttpHandler (.ashx) 文件,然后通过 AJAX 从 jQuery 自动完成功能调用该文件。此外,缓存 AJAX 响应将提高性能。
*改编自 jQuery UI 演示
JSON 响应可能很简单:
{ "emails" : ["[电子邮件受保护]", "[电子邮件受保护]", "[电子邮件受保护]"]}
此外,您的后端代码还应该使用 StringBuilder 而不是 '&='。我发现字符串附加对性能造成巨大影响。
First off, I would move your back end code to a generic HttpHandler (.ashx) file, then call that file from the jQuery autocomplete via AJAX. Also, caching the AJAX response will increase the performance.
*adapted from the jQuery UI demo
The JSON response could be something as simple as:
{ "emails" : ["[email protected]", "[email protected]", "[email protected]"]}
Also, your back end code should also use StringBuilder rather than '&='. I have found string appends to be a huge performance hit.