Results 1 to 4 of 4

Thread: Calling a VB.NET function (code behind) from a JavaScript function.

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Calling a VB.NET function (code behind) from a JavaScript function.

    I have a popup window, which returns some values to a javascript function in the parent calling web page.
    My JScript function is:
    Code:
    function SelectedVisitors(VisitorKeys)
    {
        alert(VisitorKeys)
    }
    Where visitorkeys is like "34,6,234,1"

    Basically I need to call a vb.net function is code behind to save these values to the DB, and then refresh the grid on that page too.

    Woka

  2. #2
    Member EricDalnas's Avatar
    Join Date
    Sep 2004
    Location
    Rhode Island
    Posts
    51

    Re: Calling a VB.NET function (code behind) from a JavaScript function.

    perhaps copying the data to a hidden control and raising a postback would work
    http://weblogs.asp.net/mnolton/archi...6/04/8260.aspx
    Maybe if you'd stop breathing and die for a change I wouldn't be so pissed off all the time.

    www.mredkj.com

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Calling a VB.NET function (code behind) from a JavaScript function.

    Quote Originally Posted by EricDalnas
    perhaps copying the data to a hidden control and raising a postback would work
    http://weblogs.asp.net/mnolton/archi...6/04/8260.aspx
    That's just what I was about to suggest.

  4. #4

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Calling a VB.NET function (code behind) from a JavaScript function.

    Cheers for the replies.
    Right...OK. Bit of a cheap hack/frig don't you think?
    How about...
    Code to create JS:
    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.                  doPostBack(Me)
    3.              End Sub
    4.  
    5.     Private Sub doPostBack(ByVal oPage As Page)
    6.  
    7.         If oPage.IsClientScriptBlockRegistered("__doPostBack") Then Return
    8.  
    9.         oPage.RegisterClientScriptBlock("__doPostBack", GetPostBackScript)
    10.  
    11.         oPage.RegisterHiddenField("__EVENTTARGET", "")
    12.         oPage.RegisterHiddenField("__EVENTARGUMENT", "")
    13.  
    14.     End Sub
    15.  
    16.     Private Function GetPostBackScript() As String
    17.  
    18.         Dim Java As New StringBuilder(1024)
    19.  
    20.         With Java
    21.             .Append("function __doPostBack(eventTarget, eventArgument)          " + nl)
    22.             .Append("{                                                          " + nl)
    23.             .Append("   var theform;                                            " + nl)
    24.             .Append("                                                           " + nl)
    25.             .Append("   if (window.navigator.appName.toLowerCase().indexOf('microsoft') > -1)   " + nl)
    26.             .Append("   {                                                       " + nl)
    27.             .Append("       theform = document.Form1;                           " + nl)
    28.             .Append("   }                                                       " + nl)
    29.             .Append("   else                                                    " + nl)
    30.             .Append("   {                                                       " + nl)
    31.             .Append("       theform = document.forms['Form1'];                  " + nl)
    32.             .Append("   }                                                       " + nl)
    33.             .Append("                                                           " + nl)
    34.             .Append("   theform.__EVENTTARGET.value = eventTarget.split('$').join(':');         " + nl)
    35.             .Append("   theform.__EVENTARGUMENT.value = eventArgument;theform.submit();         " + nl)
    36.         End With
    37.  
    38.         Return Java.ToString
    39.  
    40.     End Function
    Now you need a hidden control in your page to handle the postback and store some data:
    Code:
    <input type="hidden" id="VisitorKeys" value="" runat=server NAME="VisitorKeys">
    And now the JS function that is called from a button click event or something:
    Code:
    function SelectedVisitors(VisitorKeyValues)
    {
    	document.forms[0].VisitorKeys.value=VisitorKeyValues
    	__doPostBack(document.forms[0].VisitorKeys.value, '')
    }
    We now need a function in the code behind to handle this postback.
    because we are using a hidden input control we can use an event of this:
    VB Code:
    1. Private Sub VisitorKeys_ServerChange(ByVal sender As Object, ByVal e As System.EventArgs) Handles VisitorKeys.ServerChange
    2.         Dim Keys As String = VisitorKeys.Value
    3.         'Handle code here
    4.     End Sub

    make sense?

    Woka

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width