0

I have here a vb.net function which accepts 2 parameters and the values that will be passed here will be coming from my jquery code. Is it possible to pass the values from jquery to vb.net? I've already read some solutions but they point out that the vb.net method should be a webmethod. In my case, the vb function is not a web method so that strikes that out. Below are some snippets of my code. Hope you could help me on this.

JQuery code

$('#TextBox_PRVNo').keypress(function (event) {
    if (event.keyCode == '13') {
        //basically, pass values from textbox 1 and textbox 2 to VoucherNotClaimed() method
    }
});

VB.Net code

    Private Function VoucherNotClaimed(ByVal strVoucherType, ByVal strPRVNo) As Boolean
        ' TODO: Search in database
        Return True
    End Function
Musikero31
  • 3,115
  • 6
  • 39
  • 60
  • Why can't you use a web method (I think *page method* is the correct term)? It's the "right way" to do this. – Heinzi Dec 12 '12 at 07:58
  • Because it is not a web service but a web application. I believe that using the web method attribute is for web services only. – Musikero31 Dec 12 '12 at 08:02
  • You can use web methods for regular asp.net / vb.net web applications, it is not strictly for web services. So the answer to your question is you cannot call server side code from jquery like that. – JonH Dec 12 '12 at 08:05

1 Answers1

2

You don't need a web service to decorate your methods with the WebMethod attribute. If you do it on a regular page, you create a so-called page method. In a nutshell:

  1. Create a public static method in your page, decorated with the WebMethod attribute:

    <WebMethod()> _
    Public Shared Function VoucherNotClaimed(ByVal strVoucherType, ByVal strPRVNo) As Boolean
        ' TODO: Search in database
        Return True
    End Function
    
  2. Add a ScriptManager to your ASPX page and set its EnablePageMethods property to True. Also, make sure your web.config is set up properly.

  3. Call your method with JavaScript:

    var claimed = PageMethods.VoucherNotClaimed(voucherType, prvNo);
    

You can find a more detailed explanation in the following MSDN article:


Note that this is the classic way to do it. With JQuery, it is also possible to call the page method without a ScriptManager; a quick Google search for page method jquery yields a huge amount of blog entries going into every possible detail.

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519