简体   繁体   中英

How do I call my Visual Basic function from my javascript in a Visual Studios 2010 ASP.net website?

So essentially, I need to write a program which will handle all the user-input client-side, then pass the final info along to my Visual Basic function to be saved in the database. I think the best/easiest way would be if I could take my JavaScript and call my Visual Basic function, that way I can include the relevant information in arguments or some other way. I just can't figure out HOW to do that. I can call my javascript function with a button click or mouseover if I want, and the same goes for the Visual Basic function, but I need a way to call the VB from the Java, not just the HTML. Does anyone here have the syntax for this, or know of a better method I should be using?

In reference to this link

There are only 2 ways to call a server-side function from client-side: AJAX or a PostBack (__doPostBack(...)).

Here is a PostBack example.

aspx file:

<script type="text/javascript">
<!--
function callServersideFunction()
{
 var someValueToPass = 'Hello server';

 __doPostBack('CustomPostBack', someValueToPass);
}
// -->
</script>

aspx.vb file:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
 ' Insure that the __doPostBack() JavaScript method is created...
 Me.ClientScript.GetPostBackEventReference(Me, String.Empty)


 If Me.IsPostBack Then
  Dim eventTarget As String
  Dim eventArgument As String

  If ( (Me.Request("__EVENTTARGET") Is Nothing)
   eventTarget = String.Empty
  Else
   eventTarget = Me.Request("__EVENTTARGET"))
  If ( (Me.Request("__EVENTARGUMENT") Is Nothing)
   eventArgument = String.Empty
  Else
   eventArgument = Me.Request("__EVENTARGUMENT"))

  If eventTarget = "CustomPostBack" Then
   Dim valuePassed As String = eventArgument
   ' Call your VB method here...
  End If
 End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM