?
Solved

Consume WebMethod  (code behind) using javascript / AJAX 1.0

Posted on 2007-12-01
2
Medium Priority
?
5,105 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

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