带有contextKey的Ajax AutoComplete Extender

时间:2013-09-11 10:28:05

标签: javascript asp.net vb.net autocompleteextender

Aspx代码:

<script type ="text/javascript">
    function setContextKey() {
        find('AutoCompExt2').set_contextKey($get("%=TxtSyllabus.ClientID%>").value)
        alert(("<%=TxtSyllabus.ClientID %>").value)
        }
</script>
<asp:TextBox ID = "TxtSem" runat = "server" Width = "200px" onkeyup="setContextKey()"></asp:TextBox>
<asp:AutoCompleteExtender ID = "AutoCompExt2" runat = "server" MinimumPrefixLength="2" CompletionInterval="100" FirstRowSelected = "false"
        TargetControlID= "TxtSem" EnableCaching = "false" CompletionSetCount = "10" ServiceMethod = "SearchSem" UseContextKey= "true" ></asp:AutoCompleteExtender>`

VB代码:

<System.Web.Script.Services.ScriptMethod(), System.Web.Services.WebMethod()> _
Public Shared Function SearchSem(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As List(Of String)
    Try
        Dim cnn As New SqlConnection
        Dim cmd As New SqlCommand
        Dim ds As New Data.DataSet
        Dim SyllabusName = Mid(contextKey, 1, Len(contextKey) - 4)
        Dim Year = Mid(contextKey, Len(contextKey) - 4, Len(contextKey))


        cnn.ConnectionString = ConfigurationManager.ConnectionStrings("excelconn").ToString()
        cmd.CommandText = "Select Semester From MastLookup where SyllabusName='" & SyllabusName & "' And SyllabusYear='" & Year & "' And Semester=@SearchText + '%'"
        cmd.Parameters.AddWithValue("SearchText", prefixText)
        cmd.CommandType = Data.CommandType.Text
        cmd.Connection = cnn
        cnn.Open()

        Dim Syllabus As List(Of String) = New List(Of String)
        Dim sdr As SqlDataReader = cmd.ExecuteReader
        While sdr.Read
            Syllabus.Add(sdr("Semester").ToString)
        End While
        cnn.Close()
        Return Syllabus
        cnn.Close()
    Catch ex As Exception

    End Try
End Function`

错误: 我得到了ContextKey的空值,甚至没有出现Alertbox。

1 个答案:

答案 0 :(得分:0)

您应find BehaviorID ID使用自动完成扩展程序,而不是BehaviorIDset_contextKey扩展返回的DOM元素的功能,即提供$功能)。您在find函数前面还缺少<script type="text/javascript"> function setContextKey() { $find('AutoCompBehavior2').set_contextKey($get("%=TxtSyllabus.ClientID%>").value) alert(("<%=TxtSyllabus.ClientID %>").value) } </script> <asp:TextBox ID="TxtSem" runat="server" Width="200px" onkeyup="setContextKey()"></asp:TextBox> <asp:AutoCompleteExtender ID="AutoCompExt2" BehaviorID="AutoCompBehavior2" runat="server" MinimumPrefixLength="2" CompletionInterval="100" FirstRowSelected="false" TargetControlID="TxtSem" EnableCaching="false" CompletionSetCount="10" ServiceMethod="SearchSem" UseContextKey="true"></asp:AutoCompleteExtender>

setContextKey

此外,您可能希望将TxtSyllabus绑定到txtSem的更改,而不是{{1}}。