Solved

Consume WebMethod  (code behind) using javascript / AJAX 1.0

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

Industry Leaders: 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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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