Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Can we call C# function in javascript in ASPX page?
Posted
Updated 29-Aug-11 19:00pm
v2
Comments
Prerak Patel 30-Aug-11 1:01am    
Don't use ALL CAPS.

 
Share this answer
 
On aspx page call using JQuery

JavaScript
$.ajax({
 url: '<%=ResolveUrl("~/URL of the page where your method is kept") %>'+ '&MethodName=name of the method defined code behind',
                      dataType: "json",
                    success: function (msg) {
                        "What to do is sucess"
                    }
                });


Code behind on page load check

C#
if (Request["MethodName"] == "YourMethod")
                {
                    FillDetail();//call ur method here
                }



Ex:
XML
$.ajax({
 url: '<%=ResolveUrl("~/event.aspx") %>'+ '&MethodName=Event',
                      dataType: "json",
                    success: function (msg) {
                        "What to do is sucess"
                    }
                });


Code behind on page load
C#
protected void Page_Load(object sender, EventArgs e)
   {
    if (!Page.IsPostBack)
    {
         if (Request["MethodName"] == "Event")//Event same as in JQuery
                {
                    FillDetail();//call ur method here
                }


}
}
 
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