Solved

Consume WebMethod  (code behind) using javascript / AJAX 1.0

Posted on 2007-12-01
2
5,090 Views
Last Modified: 2008-02-01
Hi,

I have a web mithod in my code behind page (simplified):

    <System.Web.Services.WebMethodAttribute()> <System.Web.Script.Services.ScriptMethodAttribute()> _
    Public Shared Function GetDynamicContent(ByVal contextKey As System.String) As System.String
        Return System.Web.HttpContext.Current.Session("MessageCount")
    End Function

I want to be able to call the web method client side and put the result into a javascript var. I found a few examples of doing this with web services, but I want to be able to access the session and application variables. and I cant seem to get that info from a service.
0
Comment
Question by:bhermer
2 Comments
 
LVL 4

Accepted Solution

by:
bhermer earned 0 total points
ID: 20401557
Ok, I found the answer here, and once you know how it is very easy. So here it is for all of you who want to consume a page event as if it was a web service, and STILL have access to the Session Vars of your clients session:

On You Page: (Take note of EnablePageMethods !!!!!)

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

</asp:ScriptManager>
       
<script language="javascript" type="text/javascript">

<!--

// Javascript function


function GetLatestMessages(){
//  CALL THIS FUNCTION TO CALL THE CODE BEHIND METHOD, IF NEEDED YOU CAN SUPPLY ARGUEMENTS
    PageMethods.GetDynamicContent(OnCallComplete,OnCallError);
}

function OnCallComplete(result,txtresult,methodName)

{
// DO SOMETHING HERE, THE RESULT IS THE RETURN FROM THE METHOD
}


function OnCallError(error,userContext,methodName)
{
    if(error !== null) {
       alert(error.get_message());
    }

}




// -->

</script>

CODE BEHIND:

<System.Web.Services.WebMethodAttribute()> <System.Web.Script.Services.ScriptMethodAttribute()> _
        Public Shared Function GetDynamicContent() As System.String
     
        Return System.Web.HttpContext.Current.Session("HelloWorldSession")
    End Function
0
 
LVL 1

Expert Comment

by:Vee_Mod
ID: 20410851
Closed, 500 points refunded.
Vee_Mod
Community Support Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Handle null when using linq in this line 1 27
Re-position the objects 7 98
Registering DLL 5 43
ASP.NET Core index.html  LOCALHOST page cannot be found 2 25
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now