jqueryasp.netasmxcustom-server-controlsasp.net-webcontrol

adding web methods to custom server control


I have created a server user control that I want it to be able to use a WebMethod. If the web method is in my main application (as an ASMX file), it works fine. The problem is that I want to include this method in the same project library as the user control, so that I can distribute the DLL as a stand alone project. Because the project is a class library, I had to make the web service a VB file, instead of .ASMX. When I try to call the web method in my .VB file, nothing seems to happen (no errors but my break point on my webmthod is never reached) Can this be accomplished? The following is an example of how I created my control:

Web Method, in 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

and my Server Controls is set up like this, in 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

and my JQuery Method, found in 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')   
    }
});

I have created the service class and included it within the same project as my user control but the service is never called. On the other hand, if i place the service in an ASMX file in my web project, it will reach the web method just fine.

Any help would be great

thanks jason


Solution

  • Sorry, but if you use the old ASMX technology, then you're stuck using ASMX files. On the other hand, you can do this with WCF.