Search code examples
javascriptc#textboxdom-events

Trigger a page change based on a textbox edit


My problem is that, I want to call a code behind function from a JS. So that I can implement TextBox on Click event. I am NEW in JS so bear with me on this one...What I heard is that from the client side scripting you cannot call anything to the server (code behind). Here is my code:

 <script type="text/javascript">
       function callCodeBehind() {
       <% txtAgentName_TextChanged(); %>
       }

 </script>

This is my Textbox:

 <asp:TextBox ID="txtAgentName" runat="server" Onclick = "callCodeBehind()"></asp:TextBox>

This is my code behind:

 protected void txtAgentName_TextChanged()
    {

    }

This function is getting fired on Page_Load(), which I don't want it to. All I want is that to call this function txtAgentName_TextChanged() when user clicks on txtAgentName textbox.

Help!


Solution

  • For simplicity, you can use page methods. Check below articles for exact instructions.

    Using Page Methods in ASP.NET AJAX

    Using jQuery to directly call ASP.NET AJAX page methods