【问题标题】:Webmethod from WebService doesn't fire for AJAX AutoCompleteExtender and UpdatePanel来自 WebService 的 Web 方法不会为 AJAX AutoCompleteExtender 和 UpdatePanel 触发
【发布时间】:2014-08-07 17:34:45
【问题描述】:

代码如下:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true" 
    AsyncPostBackTimeout="1000" ScriptMode="Release" 
    EnablePartialRendering="true" >
    <Services>
    <asp:ServiceReference Path ="~/FolderForService/Service.asmx" />
    </Services>



    <Scripts>
        <asp:ScriptReference Path="~/jscript/common.js" />
    </Scripts>       
</asp:ScriptManager>

<asp:UpdatePanel ID="upFSK" runat ="server" UpdateMode ="Conditional" >
 <ContentTemplate>

 <asp:Panel ID="Panel1" runat="server" >
 <asp:TextBox ID="TextBox1" runat="server" Visible="True" Width="128px"></asp:TextBox>
 <cc1:AutoCompleteExtender ID="ACE" 
      runat="server" 
      ServicePath="~/FolderForService/Service.asmx"
      ServiceMethod="WebMethod" 
      MinimumPrefixLength="4" 
      CompletionSetCount = "10"
      TargetControlID="TextBox1">                        
</cc1:AutoCompleteExtender>
</Panel>
 </ContentTemplate>
 </UpdatePanel>

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Linq
Imports System.Web.Caching
Imports System.Data
Imports System.Collections.Generic
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebService
   Inherits System.Web.Services.WebService

'Web method which doesn't fire
<WebMethod()> _
Public Shared Function WebMethod(ByVal prefixTekst As String, ByVal count As Integer)   As List(Of String)
    ' Code  
End Function
End Class

我做了什么:

  1. 我将文本框移到更新面板之外 - 0 分
  2. 将 WebMethod 从共享更改为实例 - 0 分
  3. 将 WebService 移至根目录 - 0 分
  4. 将 PostBack=true 和 Triggers 添加到 UpdatePanel,文本框作为触发器 - 0 分
  5. 创建了带有自动完成文本框但没有更新面板的新网站 - 10 分

我错过了什么?

【问题讨论】:

    标签: asp.net ajax vb.net updatepanel autocompleteextender


    【解决方案1】:

    我只在 c# 中完成了此操作,如果您需要,我可以给您一个 c# 示例,否则请查看此资源,它应该可以帮助您实现此操作。 http://www.aspsnippets.com/Articles/ASP.Net-AJAX-Control-Toolkit-AutoCompleteExtender-without-using-Web-Services.aspx

    来自链接:

    平均售价:

    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
    
    
    <asp:ScriptManager ID="ScriptManager1" runat="server"
    EnablePageMethods = "true">
    </asp:ScriptManager>
    
    <asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox>
    <cc1:AutoCompleteExtender ServiceMethod="SearchCustomers"
        MinimumPrefixLength="2"
        CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
        TargetControlID="txtContactsSearch"
        ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
    </cc1:AutoCompleteExtender>
    

    VB:

    <System.Web.Script.Services.ScriptMethod(), _
    System.Web.Services.WebMethod()> _
    Public Shared Function SearchCustomers(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
        Dim conn As SqlConnection = New SqlConnection
        conn.ConnectionString = ConfigurationManager _
             .ConnectionStrings("constr").ConnectionString
        Dim cmd As SqlCommand = New SqlCommand
        cmd.CommandText = "select ContactName from Customers where" & _
            " ContactName like @SearchText + '%'"
        cmd.Parameters.AddWithValue("@SearchText", prefixText)
        cmd.Connection = conn
        conn.Open()
        Dim customers As List(Of String) = New List(Of String)
        Dim sdr As SqlDataReader = cmd.ExecuteReader
        While sdr.Read
                customers.Add(sdr("ContactName").ToString)
        End While
        conn.Close()
        Return customers
    End Function
    

    【讨论】:

    • Thanx Man,但我一直在考虑一遍又一遍地使用这段代码。我用 PageMethod 做到了这一点,但每次需要时我都必须复制/粘贴。无论如何,再次感谢。
    • 您查看了链接吗?我在为您提供的答案中包含了 VB,我不确定您要完成什么,但这肯定会向您展示如何完成您想要完成的工作。
    • 是的,这行得通。正如我所说,我想在 WebService 中使用它,以便下次可以重用它。但是,如果我找不到让 WebService 工作的方法,我将不得不走这条路。
    【解决方案2】:

    设置您的页面属性 EnableEventValidation="false"。它将起作用。

    【讨论】:

      猜你喜欢
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-01
      • 1970-01-01
      相关资源
      最近更新 更多