Click here to Skip to main content
15,886,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi...

i want to call function which is in .aspx.cs page from .aspx. and at the i need to reflect th text i entered in text box to .cs page.
i tried some code but it doesn't work out...


<asp:TextBox ID="TextBox1" runat="server"   onkeyup="<%=CheckCharCounter(this))%>" />  



in .cs page

C#
public void CheckCharCounter(string text)
   {

   }


how can i call function?

thanks in advance...
Posted
Updated 13-Oct-22 0:58am
v3

If this server side function is just a helper function and does not refer to any controls inside i.e. you can afford to make it static then you can call is asynchronously from javascript. Here is the article that shows how it can be done.

AJAX for Beginners (Part 3) - Calling Server Side Methods and Consuming Web Services from JavaScript in an ASP.NET website[^]
 
Share this answer
 
You can achieve it by using JQuery, following is some sample code to call code behind method using jquery script

XML
<script type="text/javascript">
        $(function () {
            debugger;
            $.ajax({
                type: 'POST',
                url: 'Default.aspx/YourMethodName;,
                data: "{'args':'test'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    //give alert if u want for success
                }
                Error:function(msg){
                    //give alert if ur functuion got some error
                }
            });
        });
    </script>



and your method is aspx.cs look like
using System.Web.Services; //add this namespace in your aspx.cs file

[WebMethod]
 public static ArrayList YourMethodName(string args)
 {
          // code here
 }</pre>
 
Share this answer
 
 
Share this answer
 
it is getting error of uncaught reference in ajax call
 
Share this answer
 
Comments
Richard Deeming 13-Oct-22 7:29am    
Your comment is not a solution to this question.
Rocky New 2-Nov-22 2:22am    
it giving an exception , that i was telling , and i waiting for someone to give me a solution

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