Click here to Skip to main content
15,886,732 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
How to call a class file function in java script..I have a class file let class1.cs in which i define a function let log_record()....i wanna call this function in my java script function how can i do this please help .....

My complete Class file class1.cs is.....

C#
public class age_calculator
{
    public age_calculator()
    {
        //
        // TODO: Add constructor logic here
        //
    }


C#
public void log_info(out string browser, out string pagepath, out string pagename, out string ip, out string dt)
    {
        string pgepath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        pagepath = System.Web.HttpContext.Current.Request.Url.ToString();
        System.IO.FileInfo oinfo = new System.IO.FileInfo(pgepath);
        pagename = oinfo.Name;
        string visitorIP = HttpContext.Current.Request.UserHostAddress.ToString();
        ip = GetIP();
        dt = Convert.ToString(DateTime.Now);
        //string browser;
        browser = HttpContext.Current.Request.Browser.Browser.ToString();
        browser = browser + "-" + HttpContext.Current.Request.Browser.Version.ToString();
    }

}


Actually i want to call this function on my classic asp page but its not possible directly so i want to call it in java script and then call javascript function in my code...If you have any other way then please suggest me..


Thanks & Regards
Srishti
Posted
Comments
Er Daljeet Singh 8-Oct-13 1:35am    
Srishti you cannot call class function or server side function directly from javascript.For doing so you need to register your function to the DOM with the help of Ajax.I am posting sample code for this scenario.

Call your class function in an aspx page method then call this method via javascript.
See here ASP.NET – How to call a server-side method from client-side JavaScript[^]
 
Share this answer
 
 
Share this answer
 
For calling a function from server side decorate your function with a Attribute which is as follow.
C#
[WebMethod]
    public static bool IsUserAvailable(string val)
    {
        if (val == "daljeet")
        {
            return true;
        }
        else
        {
            return false;
        }
    }


Now for registering your function add a script manager control to your page with EnablePageMethods property set to true.

XML
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>

      <asp:Button ID="btnForPass" runat="server" Text="Click Here"  OnClientClick="CallJavascript();"/>

      </form>


now on the button click i am calling a javascript function which is calling the function from server side.

XML
<script language="javascript" type="text/javascript">

       function CallJavascript() {
           PageMethods.IsUserAvailable(username, OnSucceeded);
       }
       function OnSucceeded(result, userContext, methodName) {
           if (methodName == "IsUserAvailable") {
               if (result == true) {
                   alert("Success");

               }
               else {
                   alert("Failed");

               }
           }
       }
     </script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900