vb.net 带有 contextKey 的 Ajax AutoComplete Extender
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18738724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax AutoComplete Extender with contextKey
提问by user2768142
Aspx code:
ASP代码:
<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 Code:
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`
Error: I am getting null value of ContextKey and even Alertbox is not appeared.
错误:我得到 ContextKey 的空值,甚至没有出现 Alertbox。
回答by Natalie Chouinard
You should be finding the AutoCompleteExtender by its BehaviorID, not ID(BehaviorIDextends functionality of the DOM element returned, i.e. providing the set_contextKeyfunction). You're also missing a $in front of the findfunction.
您应该find通过 AutoCompleteExtender 使用它BehaviorID,而不是ID(BehaviorID扩展返回的 DOM 元素的set_contextKey功能,即提供该功能)。您还缺少函数$前面的a 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>
Also, you probably meant to bind setContextKeyto changes to TxtSyllabus, not txtSem.
此外,您可能打算将setContextKey更改绑定到TxtSyllabus,而不是txtSem。

