1

我创建了一个服务器用户控件,我希望它能够使用 WebMethod。如果 web 方法在我的主应用程序中(作为 ASMX 文件),它可以正常工作。问题是我想将此方法包含在与用户控件相同的项目库中,以便我可以将 DLL 作为独立项目分发。因为该项目是一个类库,所以我不得不将 Web 服务设为 VB 文件,而不是 .ASMX。当我尝试在我的 .VB 文件中调用 web 方法时,似乎什么都没有发生(没有错误,但我的 webmthod 上的断点从未达到) 这可以完成吗?以下是我如何创建控件的示例:

Web 方法,在 myClass.VB 中:

 <System.Web.Script.Services.ScriptService()> _
 <WebService(Namespace:="http://tempuri.org/")> _
 <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _     
 Public Class myClass
     Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function TestMethod(ByVal prefixText As String) As String
    return "Hello World"
end Function

我的服务器控件是这样设置的,在 miniActiveDirectorySearch.VB 中:

Public Class miniActiveDirectorySearch
 Inherits WebControl
   Private Sub attachWebResources()  
     ScriptManager.RegisterClientScriptResource(Me, Me.GetType, "myScripts.js")
     ScriptManager.RegisterClientScriptResource(Me, Me.GetType, "jquery-1.4.1.min.js")
   End Sub
    Protected Overrides Sub CreateChildControls()
        createDynamicControls()
        MyBase.CreateChildControls()
    End Sub
    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        attachWebResources()
        MyBase.OnInit(e)
    End Sub
    Private Sub createDynamicControls()
        Controls.Clear()

        Try
            tblMainLayout = buildMaintable() 'builds out the control
            'to save space, I removed how this works.  But, it creates a textbox that
            'has a onKeyPress Event.  When the user hits return, it calls the jQuery
            'function serviceCall
            Controls.Add(tblMainLayout)
        Catch ex As Exception
            Throw New ApplicationException("Exception Occurred", ex.InnerException)
        End Try
    End Sub
 end Class

和我的 JQuery 方法,在 myScripts.js 中找到:

function serviceCall(getText, tbId, divId, bgColor) {
   $.ajax({
    type: "POST",
    url: 'myClass.asmx/TestMethod',
    data: '{"prefixText":"'some test'"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
       alert('Success')           
    },
    error: function (e) {
         alert('Error')   
    }
});

我创建了服务类并将其包含在与我的用户控件相同的项目中,但从未调用该服务。另一方面,如果我将服务放在我的 Web 项目中的 ASMX 文件中,它将很好地到达 Web 方法。

任何帮助都会很棒

谢谢杰森

4

1 回答 1

0

抱歉,如果您使用旧的 ASMX 技术,那么您将无法使用 ASMX 文件。另一方面,您可以使用 WCF 执行此操作。

于 2013-04-23T15:29:43.560 回答