We help IT Professionals succeed at work.

call a subroutine from a javascript function.

VBdotnet2005
VBdotnet2005 asked
on
Medium Priority
420 Views
Last Modified: 2012-05-12
I want to call a subroutine from a javascript function in aspx page?
Comment
Watch Question

Commented:
A simple variation on AJAX called a page method does what you're asking. <see code>
****ASPX PAGE****
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
    	
    	function UpdateViaPageMethod()
    	{
    		//Call our function from code behind
    		//If there are no errors it will be picked up by the onSuccess function
    		//If there is an exception it will be picked up by the onFail function
    		PageMethods.UpdateFromPageMethod(onSuccess,onFail);
    	}
    	
    	
    	//Ajax return method
    	function onSuccess(rValue, userContext, methodName)
		{
			$get('ContentFromPageMethod').innerHTML = rValue;
		}
		
		//simply fires if there is an AJAX error
		 function onFail(exValue, rParam, methodName)
		 {
			alert(exValue);
		 }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true"></asp:ScriptManager>
    <div id="ContentFromPageMethod" runat="server"></div>
    <asp:Button ID="BtnUpdate" runat="server" Text="Update" OnClientClick="UpdateViaPageMethod(); return false;" />
    </form>
</body>

Open in new window

****ASPX CODE BEHIND****

using System.Web.Services;

public partial class PageMethod : System.Web.UI.Page
{
    //Note the addition of the System.Web.Services reference
    //Must be decorated with the WebMethod attribute
    //Must be static
    //Does not have to return a value, but in this case for full demonstation value I've included one.
    [WebMethod]
    public static string UpdateFromPageMethod()
    {
        //The only trick here is you cannot access members of the page directly
        //So this will throw an error
        //ContentFromPageMethod.InnerHtml = "I've been updated via a PageMethod.";

        //You can however run any code you want like updating a database 
        //or getting some information and returning it to the page.
        
        return "I've been updated via a PageMethod.";
    }
}

Open in new window

Commented:
Im gonna guess from you username you might want that in VB :)
Imports System.Web.Services

Partial Class PageMethodVB
    Inherits System.Web.UI.Page

    '//Note the addition of the System.Web.Services reference
    '//Must be decorated with the WebMethod attribute
    '//Must be shared
    '//Does not have to return a value, but in this case for full demonstation value I've included one.
    <WebMethod()> _
    Public Shared Function UpdateFromPageMethod() As String
        '//The only trick here is you cannot access members of the page directly
        '//So this will throw an error
        '//ContentFromPageMethod.InnerHtml = "I've been updated via a PageMethod.";

        '//You can however run any code you want like updating a database 
        '//or getting some information and returning it to the page.

        Return "I've been updated via a PageMethod."
    End Function
End Class

Open in new window

Author

Commented:
I got error "PageMethods undefined" in javascript

Commented:
Show me the code you put together and I can show you what you missed.
Programmer
Commented:
also, you can also usejavascript to access XML thru a querystring to access a web page if you dont want to use script manager/page methods
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_24241186.html?sfQueryTermInfo=1+10+30+javascript+querystr+xml

Commented:
Javascript runs in the browsers, your subroutine resides on the server.

You'll have to make javascript open a url on the server, and this url should then call your subroutine. There are several ways to have javascript open a url on the server.

Author

Commented:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
        <script type="text/javascript">
        function UpdateViaPageMethod()
    	{
    		//Call our function from code behind
    		//If there are no errors it will be picked up by the onSuccess function
    		//If there is an exception it will be picked up by the onFail function
    		PageMethods.UpdateFromPageMethod(onSuccess,onFail);
    	}
    	
    	
    	//Ajax return method
    	function onSuccess(rValue, userContext, methodName)
		{
			$get('ContentFromPageMethod').innerHTML = rValue;
		}
		
		//simply fires if there is an AJAX error
		 function onFail(exValue, rParam, methodName)
		 {
			alert(exValue);
		 }


	</script>
</head>
<body>
    <form id="form1" runat="server">

        <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
       <div id="ContentFromPageMethod" runat="server"></div>
    <asp:Button ID="BtnUpdate" runat="server" Text="Update" OnClientClick="UpdateViaPageMethod(); return false;" />
  
    </form>
</body>
</html>


Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebService
     Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "Hello World"
    End Function
    <WebMethod()> _
     Public Shared Function UpdateFromPageMethod() As String
        Return "Hello World"
    End Function


End Class

Open in new window

Author

Commented:
same error  "PageMethods undefined" in javascript

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

Commented:
Also I setup a page method, a page method (as shown in my sample) would allow you to communicate with a method on the code behind of the same page.

You have setup a webmethod. There are more moving parts involved in this. One thing I can see right away is that you havent setup your class file correctly. Please take note of this line:

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _

I suggest at least initially you setup the code behind page method on your aspx page as shown in the initial model. There is no need to make an entire class for this if it is only used on one page. Even so while you learn it would be best to have some success and just use a page method.
GlobaLevelProgrammer

Commented:
here are a few things...do you have ajax toolkit  installed on your server that runs the page?
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
        <script type="text/javascript">
        function UpdateViaPageMethod()
    	{
    		//Call our function from code behind
    		//If there are no errors it will be picked up by the onSuccess function
    		//If there is an exception it will be picked up by the onFail function
    		PageMethods.UpdateFromPageMethod(onSuccess,onFail);
    	}
    	
    	
    	//Ajax return method
    	function onSuccess(rValue, userContext, methodName)
		{
			$get('ContentFromPageMethod').innerHTML = rValue;
		}
		
		//simply fires if there is an AJAX error
		 function onFail(exValue, rParam, methodName)
		 {
			alert(exValue);
		 }


	</script>
</head>
<body>
    <form id="form1" runat="server">

        <asp:ScriptManager ID="ScriptManager1" runat="server"   EnablePageMethods="true"  >
    </asp:ScriptManager>
       <div id="ContentFromPageMethod" runat="server"></div>
    <asp:Button ID="BtnUpdate" runat="server" Text="Update" OnClientClick="UpdateViaPageMethod(); return false;" />
  
    </form>
</body>
</html>


Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.SessionState.HttpSessionState

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebService
     Inherits System.Web.Services.WebService

     <System.Web.Services.WebMethod()> _
    Public Shared Function HelloWorld() As String
        Return "Hello World"
    End Function

    <System.Web.Services.WebMethod()> _
     Public Shared Function UpdateFromPageMethod() As String
        Return "Hello World"
    End Function


End Class

Open in new window

Commented:
There are quite a few differences between using a web method and a page method as I said before. Rather than going through and trying to pick out all the pieces to correct I'll just post a link to a zipped webservice example.

Use the 1st sample I posted as a page method.

Use this link for a web service.

Please follow the examples more carefully to avoid confusion.

https://docs.google.com/open?id=0B_GYu6g-VhuFYjYxZWEwZDQtMGRhZC00ZTEyLThmNTctYWRlY2JkNDYyNWFh

I would have just added the zip file here, but EE is not aware that an .asmx file is "not dangerous".

Commented:
If you are using .net 3.5 or greater on your server there are no ajaxtoolkit .dll's required to run the code I provided.
GlobaLevelProgrammer

Commented:
You can also use querystring with XML request to do round trips as well

Commented:
Tell you what:

125 pts
Doesn't follow example / direction
Takes a week to reply

I'm done. Taking thread off "watch" list. You have more than adequate information to make this happen if you put a tiny amount of focus and effort into it.

Author

Commented:
ddayx10,
I did follow your sample and got the error.
ee.JPG
GlobaLevelProgrammer

Commented:
VB...I agree this is a 500 point question.

Please in the future reward the experts appropriately. We do want serve you, but we work for free here and our time is worth the pints thank you

Author

Commented:
It is 500 now.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.