admin管理员组

文章数量:1402041

I am trying to call a method in my server-side vb code from jquery.

    import System.Web.Services
    ...
    'my VB Code
    <WebMethod()> _
    Public Shared Function SubmitReport_Click()
        'this is where my code will go
        Return Nothing
    End Function

In my javascript code the alert is being called but the SubmitReport_Click is not being called.

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>

<script type="text/javascript">
    $("#<%= FileInput.ClientID%>").on('filebatchselected', function (event) {
        alert("file input");
        pagemethods.SubmitReport_Click();
    })
</script>

I am trying to call a method in my server-side vb code from jquery.

    import System.Web.Services
    ...
    'my VB Code
    <WebMethod()> _
    Public Shared Function SubmitReport_Click()
        'this is where my code will go
        Return Nothing
    End Function

In my javascript code the alert is being called but the SubmitReport_Click is not being called.

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>

<script type="text/javascript">
    $("#<%= FileInput.ClientID%>").on('filebatchselected', function (event) {
        alert("file input");
        pagemethods.SubmitReport_Click();
    })
</script>
Share Improve this question asked Jul 16, 2015 at 20:24 Jake HunterJake Hunter 1411 gold badge2 silver badges8 bronze badges 5
  • I am pretty sure that isn't possible but maybe someone smarter than me will chime in. – Gary Storey Commented Jul 16, 2015 at 20:27
  • I am not getting any errors and the alert("file input") is being called on this particular event – Jake Hunter Commented Jul 16, 2015 at 20:31
  • I don't know will this help <WebMethod(EnableSession:=True)> this I use, but for Public Shared Sub ... and every time work. btw. is this important PageMethods... ? upper/lower case – nelek Commented Jul 16, 2015 at 20:59
  • btw. what is Your code under that function? – nelek Commented Jul 16, 2015 at 21:11
  • Currently i just am just assigning a variable with a breakpoint to make sure it is being called – Jake Hunter Commented Jul 17, 2015 at 12:13
Add a ment  | 

2 Answers 2

Reset to default 2

I'd make a function that fires on the click event and calls over to your web method using AJAX, and use JSON to pass any relevant data.

$(".clickMe").click(doWebMethod);

function doWebMethod () {    
    var data = { 'name': 'jessikwa' , 'location': 'ny' }
    var params = "{'dataPackage': '" + JSON.stringify(data) + "'}";
    $.ajax({
        type: "POST",
        url: webMethodUrl,
        async: true,
        data: params,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            alert(msg.d);
        },
        error: function () {
            alert("fail");
        }
    });
}

//VB HERE

<WebMethod()> _
Public Shared Function SubmitReport_Click(ByVal dataPackage as String) as String
    Dim rtnStr As String = "OK"
    //deserialize data package here
    Return rtnStr
End Function

You can use an Ajax request. I just typed this up, so you may need to Google the syntax in case I messed it up.

$.ajax({
    type: "GET",
    url: "FileName.aspx/SubmitReport_Click",
    success: function(){
        alert('It Worked!');
    },
    failure: function() {
        alert('It Failed!');
    }
});

本文标签: javascriptHow do I call a server side VBnet function from jquery in an aspnet formStack Overflow