<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Sohail Sayed : Ajax.net</title><link>http://weblogs.asp.net/sohailsayed/archive/tags/Ajax.net/default.aspx</link><description>Tags: Ajax.net</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Calling methods in a codebehind function (PageMethods) from client side using ajax.net</title><link>http://weblogs.asp.net/sohailsayed/archive/2008/02/23/calling-methods-in-a-codebehind-function-pagemethods-from-client-side-using-ajax-net.aspx</link><pubDate>Sat, 23 Feb 2008 18:29:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5845803</guid><dc:creator>ssayed@iotap.com</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/sohailsayed/rsscomments.aspx?PostID=5845803</wfw:commentRss><comments>http://weblogs.asp.net/sohailsayed/archive/2008/02/23/calling-methods-in-a-codebehind-function-pagemethods-from-client-side-using-ajax-net.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;Hi,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Everyone using ajax.net will be familiar with the updatepanels. In addition another well known way of utilizing the rich ajax.net library is calling web service methods or methods from the code behind file from&amp;nbsp;the client side. Recently i came across a situation in my project where i wanted to call methods from code behind (i have been using updatepanels but in some situation all i was doing was getting few values from the db and i felt not using the updatepanel would be a better option rather calling the my code behind function using pagemethods feature would be more appropriate). Though&amp;nbsp; there is a good deal of information on the internet on update panels and calling web services i found it difficult to&amp;nbsp;reach a good enough article showing me how to call a function from the codebehind file.&amp;nbsp; Finally though :-) i was able to call the code behind function. i am going to demonstrate the same in this blog.&lt;br&gt;&amp;nbsp;&amp;nbsp; Before starting let me indicate there could be&amp;nbsp;many reasons for calling code behind methods from client side using ajax.net .....in my case i wanted to use existing functionality in my code behind and we were not using any webservices .&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; Here i am going to demonstrate a simple&amp;nbsp;&amp;nbsp; application that calls a code behind method&lt;br&gt;&lt;br&gt;to start first we create an ajax enabled website. Then in our page we add the heart of ajax.net -- the ScriptManager control and set the EnablePageMethods property as true&lt;br&gt;&lt;br&gt;&lt;font color="#0000ff" size="2"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#800000" size="2"&gt;ajax&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;:&lt;/font&gt;&lt;font color="#800000" size="2"&gt;ScriptManager&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;ID&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;="ScriptManager1"&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;runat&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;="server"&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;EnablePageMethods&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;="true"&amp;gt;&lt;/font&gt;&lt;br&gt;&lt;br&gt;We also place a div and input buttons on our form&lt;br&gt;&lt;br&gt;&lt;font color="#0000ff" size="2"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#800000" size="2"&gt;div&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;id&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;="div1"&amp;gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="2"&gt;
&lt;/font&gt;&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;&amp;lt;/&lt;/font&gt;&lt;font color="#800000" size="2"&gt;div&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="2"&gt;
&lt;/font&gt;&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#800000" size="2"&gt;input&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;type&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;="button"&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;value&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;="Call Page Method With No Parameters"&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;onclick&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;="CallPageMethod()"&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;/&amp;gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="2"&gt;
&lt;/font&gt;&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#800000" size="2"&gt;input&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;type&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;="button"&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;value&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;="Call Page Method With Parameters"&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;onclick&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;="CallParametersPageMethod()"&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;/&amp;gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="2"&gt;
&lt;/font&gt;&lt;p mce_keep="true"&gt;&lt;font size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/p&gt;
&lt;p mce_keep="true"&gt;As the text indicates on call a&amp;nbsp;function without parameters and the other calls a function having parameters&lt;/p&gt;The code behind function are as below&lt;br&gt;&lt;br&gt;&lt;font size="2"&gt;[System.Web.Services.&lt;/font&gt;&lt;font color="#008080" size="2"&gt;WebMethod&lt;/font&gt;&lt;font size="2"&gt;()]&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;public&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;static&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;string&lt;/font&gt;&lt;font size="2"&gt; MyFirstPageMethod()&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;return&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#800000" size="2"&gt;"Welcome to the world of AJAX.NET "&lt;/font&gt;&lt;font size="2"&gt; ;&lt;br&gt;}&lt;/font&gt;&lt;font size="2"&gt; &lt;br&gt;&lt;br&gt;[System.Web.Services.&lt;/font&gt;&lt;font color="#008080" size="2"&gt;WebMethod&lt;/font&gt;&lt;font size="2"&gt;()]&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;public&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;static&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;string&lt;/font&gt;&lt;font size="2"&gt; MyFirstParameterPageMethod(&lt;/font&gt;&lt;font color="#008080" size="2"&gt;String&lt;/font&gt;&lt;font size="2"&gt; strVal)&lt;br&gt;{&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp; return&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#800000" size="2"&gt;"Welcome to the world of AJAX.NET , the value you passed is : "&lt;/font&gt;&lt;font size="2"&gt; + strVal ;&lt;br&gt;}&lt;/font&gt;&lt;font size="2"&gt; 
&lt;/font&gt;&lt;p mce_keep="true"&gt;To call a page method it must be static and you have to mark it with the WebMethod attribute. The first function doesnot takes parameter while the second function takes a string value as parameter. both return a string value.&lt;br&gt;&lt;br&gt;&amp;nbsp;The javascript function are as below&lt;br&gt;&lt;br&gt;&amp;lt;&lt;font color="#800000" size="2"&gt;script&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;language&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;="javascript"&amp;gt;&lt;/font&gt;&lt;/p&gt;&lt;font color="#0000ff" size="2"&gt;function&lt;/font&gt;&lt;font size="2"&gt; CallPageMethod()&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; PageMethods.MyFirstPageMethod(onSucceeded,onFailed);&lt;br&gt;}&lt;/font&gt;&lt;font size="2"&gt; 
&lt;/font&gt;&lt;p mce_keep="true"&gt;&lt;font color="#0000ff" size="2"&gt;function&lt;/font&gt;&lt;font size="2"&gt; CallParametersPageMethod()&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp; PageMethods.MyFirstParameterPageMethod(&lt;/font&gt;&lt;font color="#800000" size="2"&gt;"This is a Demo"&lt;/font&gt;&lt;font size="2"&gt;,onSucceeded,onFailed);&lt;br&gt;}&lt;/font&gt;&lt;/p&gt;&lt;font color="#0000ff" size="2"&gt;function&lt;/font&gt;&lt;font size="2"&gt; onSucceeded(result,userContext,methodName)&lt;br&gt;{&lt;br&gt;&amp;nbsp; $get(&lt;/font&gt;&lt;font color="#800000" size="2"&gt;'div1'&lt;/font&gt;&lt;font size="2"&gt;).innerHTML=result;&lt;br&gt;&lt;/font&gt;&lt;font size="2"&gt;}&lt;/font&gt;&lt;font size="2"&gt; 
&lt;/font&gt;&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;function&lt;/font&gt;&lt;font size="2"&gt; onFailed(error,userContext,methodName)&lt;br&gt;{&lt;br&gt;&amp;nbsp; alert(&lt;/font&gt;&lt;font color="#800000" size="2"&gt;"An error occurred"&lt;/font&gt;&lt;font size="2"&gt;)&lt;br&gt;}&lt;/font&gt;&lt;/p&gt;&lt;font color="#0000ff" size="2"&gt;&amp;lt;/&lt;/font&gt;&lt;font color="#800000" size="2"&gt;script&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; 
&lt;/font&gt;&lt;p mce_keep="true"&gt;The&amp;nbsp;CallPageMethod method calls the parameterless function MyFirstPageMethod&amp;nbsp;from codebehind and the CallParametersPageMethod&amp;nbsp;calls the code behind function MyFirstParameterPageMethod&amp;nbsp;and passes a string as parameter&amp;nbsp;&amp;nbsp;&lt;br&gt;&lt;br&gt;We call the codebehind method as PageMethods.MethodName();&lt;br&gt;&lt;br&gt;The OnSucceeded parameter indicates&amp;nbsp;the javascript function to be called when the call has succeeded and the result parameter has the result stored in it in our case the&amp;nbsp;string returned from the code behind file.&lt;br&gt;&lt;br&gt;The OnFailed&amp;nbsp;parameter indicates the&amp;nbsp;javascript function to be called when&amp;nbsp; error occurs.&lt;br&gt;&lt;br&gt;In case the code behind function has no parameters then&amp;nbsp;we pass the onSucceeded and OnFailed parameters only.&lt;br&gt;&lt;br&gt;To pass a parameter to code behind we pass them in the same order as the code behind function and at the end we add the&amp;nbsp;onsucceeded and onfailed&amp;nbsp;parameters as in the CallParametersPageMethod&amp;nbsp;javascript function.&lt;br&gt;&lt;br&gt;The return value is displayed in the div.&lt;br&gt;&lt;br&gt;I have attached the complete code&amp;nbsp;of this.&lt;br&gt;&lt;br&gt;Hope&amp;nbsp;this is helpful to you all.&lt;br&gt;&lt;br&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5845803" width="1" height="1"&gt;</description><enclosure url="http://weblogs.asp.net/sohailsayed/attachment/5845803.ashx" length="4544" type="application/x-zip-compressed" /><category domain="http://weblogs.asp.net/sohailsayed/archive/tags/asp.net/default.aspx">asp.net</category><category domain="http://weblogs.asp.net/sohailsayed/archive/tags/code+behind+functions/default.aspx">code behind functions</category><category domain="http://weblogs.asp.net/sohailsayed/archive/tags/Ajax.net/default.aspx">Ajax.net</category><category domain="http://weblogs.asp.net/sohailsayed/archive/tags/codebehind+functions+ajax.net/default.aspx">codebehind functions ajax.net</category><category domain="http://weblogs.asp.net/sohailsayed/archive/tags/PageMethods/default.aspx">PageMethods</category><category domain="http://weblogs.asp.net/sohailsayed/archive/tags/.NET/default.aspx">.NET</category></item></channel></rss>