Getting started

  • Add reference to AjaxAvailableUserControls.Library.dll assembly in your ASP.NET Application (or SharePoint Webpart project or Sitecore solution)
  • Create ASP.NET UserControl, for example, it should look like as markup below:

 

  1. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DemoUserControl.ascx.cs" Inherits="AjaxAvailableUserControls.Application.AjaxUserControls.DemoUserControl" %>
  2.  
  3. <input type="button" value="click me to call server side method 1" id="btnDemo1"/><br />
  4. <input type="button" value="click me to call server side method 2" id="btnDemo2"/><br />
  5. <input type="button" value="click me to call server side method 3" id="btnDemo3"/><br />

 

  • Inherit code-behind class from  AjaxAvailableUserControl and add methods you want to use on the client side;
For example, it should look like as following code (this code is taken from demo web application available to download from the source code tab here)

 

  1.     /// <summary>
  2.     /// User control to demonstate some benefits of usage AjaxAvailableUserControl
  3.     /// </summary>
  4.     public partial class DemoUserControl : AjaxAvailableUserControl
  5.     {
  6.         #region demo methods here
  7.         public AjaxResponse MyDemoMethod1(string arg1, string arg2, int arg3, bool arg4)
  8.         {
  9.             AjaxResponse result = new AjaxResponse
  10.             {
  11.                 AdditionalSettings = String.Format("This is response to callback with parameters {0}/{1}/{2}/{3} from server side", arg1, arg2, arg3, arg4),
  12.                 CurrentPage = 0,
  13.                 Records = new List<object>()
  14.                 {
  15.                     new
  16.                     {
  17.                         Date = DateTime.Now,
  18.                         Data = int.MinValue
  19.                     }
  20.                 },
  21.                 TotalRecords = 1,
  22.                 TotalPages = 1
  23.             };
  24.  
  25.             return result;
  26.         }

 

  • After that we can use the server side method in the JavaScript  as follows:

 

  1. <script language="javascript" type="text/javascript">
  2.     
  3.     $(document).ready(function()
  4.     {
  5.             $("#btnDemo1").click(function()
  6.             {
  7.                 jQuery.execute("MyDemoMethod1",
  8.                 { "arg1": "test string 1", "arg2": "test strign 2", "arg3": 2, "arg4": false },
  9.                 {
  10.                     onSuccess: function (data) {
  11.                         debugger;
  12.                         alert("settings:" + data.AdditionalSettings + ";currentPage:" + data.CurrentPage.toString());
  13.                     },
  14.                     onError: function (exception) { alert(exception); }
  15.                 });
  16.             });

 

  • To test click by first button, and there will be following result:

 

It's all you need to do to be able to use server side logic on the client side.

Many thanks for your attention and time.

 

 

  1. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DemoUserControl.ascx.cs" Inherits="AjaxAvailableUserControls.Application.AjaxUserControls.DemoUserControl" %>
  2.  
  3. <input type="button" value="click me to call server side method 1" id="btnDemo1"/><br />
  4. <input type="button" value="click me to call server side method 2" id="btnDemo2"/><br />
  5. <input type="button" value="click me to call server side method 3" id="btnDemo3"/><br />

 

 

 

 

Last edited Feb 10, 2012 at 10:14 AM by andreysmirnov, version 4

Comments

No comments yet.