Madhav

Madhav

  • NA
  • 196
  • 43.3k

call function from ascx.cs in javascript using ajax call

Jan 4 2017 8:08 AM
I have created one control which has one function but I want to call that function with ajax call on any .aspx page or master page same as like calling function from a code behind file,am fine with codebehind file but stucked with calling function from acsx.cs file to any .aspx page/master page on which am including/registering that control.
this is my HTML
 
<div>     
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>   
  <input type="button" id="Button2" runat="server" value="Button" />
 </div>
 my ascx.cs code
[WebMethod()] 
[ScriptMethod()] 
public static void myMethod(string text)
 { 
  Label1.text=text; 
}
my ajax call whn am calling function from code behind file is like
$(document).ready(function () { 
    $("#Button2").click(function () {  
       var text = $("#TextBox1").val();    
             $.ajax({      
             type: "POST",        
             url: "WebForm1.aspx/myMethod",   
             data: "{ text: '" + text + "'}",  
             contentType: "application/json; charset=utf-8",    
              dataType: "json",      
              async: "true",         
              cache: "false",        
              success: function (msg) {       
               alert("success");        
                                    },  
             Error: function (x, e) {   
              // On Error         
    }      
   });   
  });  
   });
n ajax call am calling function from codebehind thats why am puting url: "WebForm1.aspx/myMethod", but am confused when about calling the function from .ascx.cs, like what will be a Url value for that ,because am simply using that control on my page like
<UC:userControl id="control1" runat="server" />
 which I have included/registered on my page like
<%@ Register TagPrefix="UC" TagName="userControl " src="~/Controls/Mycontrol.ascx" %> 
how do i resolved this issue?
i tried url value(s) like <pre> url: "Mycontrol.ascx/myMethod",
also
<pre> url: "WebForm1.aspx/Mycontrol.ascx/myMethod",
but still am unable to get it 

Answers (18)