Search code examples
javascriptjqueryasp.netajaxpagemethods

How to call page method with a parameter through in a javascript event?


I have method like this in my .cs :

[System.Web.Services.WebMethod]
public static void GetServiceInformation(IInfo x) //IInfo  is an interface
{
    x.l_power = true;
    x.lb_InboxCount = UserTrans.GetInbox(int.Parse(emp_num), 0);
}

Now i want to call this method through a javascript method as a page method but it doesn't work .

<script type ="text/javascript">

    function GetInfo() {
        PageMethods.GetServiceInformation(this);
    }
   window.onload = setTimeout("GetInfo()", 3000);
</script>

  <telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnablePageMethods="true">
  </telerik:RadScriptManager>

My .cs :

 public partial class AppMaster : Log, IInfo //My page
    {
        public string Inbox
        {
            get
            {
                return hpl_Inbox.NavigateUrl;
            }

            set
            {
                hpl_Inbox.NavigateUrl = value;
            }
        }
        public string Draft
        {
            get
            {
                return hpl_Draft.NavigateUrl;
            }

            set
            {
                hpl_Draft.NavigateUrl = value;
            }
        }

        public string New
        {
            get
            {
                return hpl_New.NavigateUrl;
            }
            set
            {
                hpl_New.NavigateUrl = value;
            }
        }
        public string Approved
        {
            get
            {
                return hpl_Approved.NavigateUrl;
            }
            set
            {
                hpl_Approved.NavigateUrl = value;
            }
        }
    //------- etc
 }

My interface :

public interface IInfo
    {
        string Inbox { get; set; }
        string Draft { get; set; }
        string New { get; set; }
        string Approved { get; set; }
        string archive { get; set; }
        string search { get; set; }
        string cand { get; set; }
        string pri { get; set; }
        string power { get; set; }
        string admin { get; set; }
        string help { get; set; }
        bool l_cand { get; set; }
        bool l_pri { get; set; }
        bool l_power { get; set; }
        bool l_admin { get; set; }

        string lb_ApprovedCount { get; set; }
        string lb_InboxCount { get; set; }
        string lb_archive { get; set; }
        string lb_DraftCount { get; set; }

    }

Solution

  • I do the following :

    Create New Page and called it : Counts.aspx

     protected void Page_Load(object sender, EventArgs e)
            {
    
                            emp_num = int.Parse(Session["empnum"].ToString());
                            Thread.Sleep(3000);
                            string res = GetCounts(emp_num);
                            Response.Write(res);
    
            }
            /***********************************************************************************************/
            protected string GetCounts(int empNum)
            {
    
                string outbox = UserTransaction.getoutboxCount(empNum, 0);
                string inbox = UserTransaction.getinboxCount(empNum, 0);
                string archive = UserTransaction.getarchivecount(empNum, 0);
                string draft = UserTransaction.getdraftcount(empNum, 0);
                return outbox + "~" + inbox + "~" + archive + "~" + draft + "~";
    
            }
    

    and in my main page :

     <script type="text/javascript">
    
            function loadXMLDoc() {
                var xmlhttp;
                if (window.XMLHttpRequest) {
                    xmlhttp = new XMLHttpRequest();
                }
                else {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function() {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        var split = xmlhttp.responseText.split('~');
    
                        var outbox = split[0];
                        var inbox = split[1];
                        var archive = split[2];
                        var draft = split[3];
                        document.getElementById("lbl_DraftCount").innerHTML = draft;
                        document.getElementById("lbl_InboxCount").innerHTML = inbox;
                        document.getElementById("lbl_ApprovedCount").innerHTML = outbox;
                        document.getElementById("lbl_archive").innerHTML = archive;
                    }
                }
                xmlhttp.open("GET", "Counts.aspx", true);
                xmlhttp.send();
            }
            loadXMLDoc();
    
    
        </script>