在asp.net中使用Jquery Auto Complete

时间:2010-08-07 07:59:05

标签: asp.net jquery

我想在asp.net中使用jquery ui auto complete插件。 这是我背后的代码:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim locations As String() = New String() {"Las Vegas", "Los Angeles", "Tampa", "New York", "s", "sss"}
    Dim jsArray As String = GetJSArrayForVBArray(locations)
    Me.ClientScript.RegisterArrayDeclaration("usernames", jsArray)
End Sub
Private Function GetJSArrayForVBArray(ByVal vbArray As String()) As String
    Dim myResult As New StringBuilder()
    For Each item As String In vbArray
        With myResult
            .Append(",'" & item & "'")
        End With
    Next
    If (myResult.Length > 0) Then
        Return myResult.ToString().Substring(1)
    Else
        Return ""
    End If
End Function

这是html代码:

 <script type="text/javascript">
    $(function () {
        var availableTags = new Array();
        $("#tags").autocomplete({
            source: availableTags
        });
    });
</script>

<label for="tags">Tags: </label>
<input id="tags" />

有什么问题? 这是行不通的。 当我在aspx页面中填充数组时它可以工作,但是当我想填写代码后面没有。 感谢。

1 个答案:

答案 0 :(得分:1)

尝试将JS更改为:

<script type="text/javascript">
$(function () {
    $("#tags").autocomplete({
        source: usernames
    });
});
</script>

如果我正确猜测你的.Net代码,你将“usernames”注册为javascript数组,但是尝试使用空的“availableTags”作为自动完成源。

注意:这可能完全在这里 - 我之前没有使用过ClientScript.RegisterArrayDeclaration或jQuery自动完成插件。