dotnet thoughts 

a dotnet developer's technical blog

Page methods in ASP.Net

In my previous post I mentioned about accessing server side controls in Java script using ASP.Net Ajax API. That functionality is can be combine with the Page methods feature, for simple Ajax calls without update panel control. You can create a Page method by adding a [WebMethod] attribute in the declaration. For this you need to import System.Web.Servicesnamespace. The function should be public static, otherwise it will not work.
C# Code

[System.Web.Services.WebMethod()]
public static string HelloWorld()
{
return ("Hello World");
}

And in the script manager control, you need to set the EnablePageMethodsproperty to True. Thats it you created a Page Method. You can access it in the Javascript like

Javascript code

function invokeHelloWorld()
{
PageMethods.HelloWorld(HelloWorldCallback); //Invoking the Page method, with callback function as the parameter.
}
function HelloWorldCallback(res)
{
alert(res);
}

You need to pass the Callback function as the last parameter, when you call the Page methods. You can pass the other parameters before the callback function. In the callback function you will the results from the specific operation.

Note: Page methods is Public static so we will not able to access the Page and Control objects from the function.

This is my 100th post :) Thanks for you support , cooperation, and the valuable comments.

Accessing server side controls from Javascript

With ASP.Net Ajax, you can get the server side controls from Javascript using ASP.Net Ajax javascript API. With this feature will be helpful for developers, whom wish to play around Page methods in ASP.Net Ajax.

function getValueInText()
{
var myText = $get("MyText");
alert(myText.value);
//You can also set the value of the Textbox using the same method.
myText.value = "This is updated";
}

In the above code, MyText is a server side asp.net textbox.
You can get more information about all this from ASP.Net Ajax official site.

Embedding a JavaScript File as a Resource

Last few days I was on vacation, last day I joined back in my company @ Kerala, and now I am assigned to a ASP.Net application. In the current application, I have to create a web custom control, and in that I need to write some javascript code, and I planned to write the script in the RenderContentsmethod. But when the size of the script was increased, I need to each and every line of javascript using WriteLine method, and it is visible to the user via View Sourceoption of browsers. But I don’t have any other option, but when I checked the view source option of Update Panel control from Microsoft, or Calendar control code from DayPilot, I am not able to get the actual source. So there are some option available where I can disable users to view the script. Then after some google-ing I got a solution, Embedding a JavaScript File as a Resource in the web control assembly. You can get more information about this in the ASP.Net Ajax official site.

Here is the steps to embed script file.

  1. Right click on the properties of the script file, change the Build Action to EmbeddedResource
  2. Create a new webresource attribute in AssemblyInfo file.
    [Assembly: System.Web.UI.WebResource("MonthView.DragDrop.js", "application/x-javascript")]
    

    You can put this into the same file also, where MonthView is the namespace, DragDrop.js is the script file, and second argument is the resource file type.

  3. Accessing the script file
    You can access the embed file using two ways.

    1. Using ASP:ScriptReference control
      <scripts>
      <asp:ScriptReference Assembly="SampleControl" Name="SampleControl.UpdatePanelAnimation.js" />
      </scripts>
      
    2. Using Page.RegisterScript
      Page.ClientScript.RegisterClientScriptResource(typeof(MonthView), "MonthView.DragDrop.js");
      

    Both will come in the script tags and you can call the functions and methods of the file directly from the page

And the page source you will get some WebResource.axd?some value, as the value of src attribute.