Solved

Consume WebMethod  (code behind) using javascript / AJAX 1.0

Posted on 2007-12-01
2
5,094 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 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