Solved

Consume WebMethod  (code behind) using javascript / AJAX 1.0

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

20 Experts available now in Live!

Get 1:1 Help Now