Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Guys!
I'm trying to call a js function on the "Onclientclick" event of an asp:button control.
Everything works fine!

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title></title>
  <script type="text/javascript">
    function datepicker_onchange() {
      var name = document.getElementById('datepicker').value;
        PageMethods.loadStandings(name, OnSuccess, OnError);
      }
      function OnSuccess(response) {
        alert(response);
      }
      function OnError(error) {
        alert("Error!");
      }
    </script>
</head>
<body>
  <form id="form1" runat="server">
    <asp:ScriptManager ID="scriptManager" runat="server" EnablePageMethods="true" ScriptMode="Release" LoadScriptsBeforeUI="true"></asp:ScriptManager>
    <div>
      <asp:TextBox id="datepicker" runat="server"></asp:TextBox>
      <asp:Button id="btnJS" runat="server" OnClientClick="datepicker_onchange();return false;" />
      
    </div>
  </form>
</body>
</html>


This way, if I click on the button a popup shows what I wrote on the textbox.

But if i try to do it automatically on the "OntextChanged" event of the textbox (this way)...

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Javascript.aspx.cs" Inherits="WebPages_Javascript" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title></title>
  <script type="text/javascript">
    function datepicker_onchange() {
      var name = document.getElementById('datepicker').value;
        PageMethods.loadStandings(name, OnSuccess, OnError);
      }
      function OnSuccess(response) {
        alert(response);
      }
      function OnError(error) {
        alert("Error!");
      }
    </script>
</head>
<body>
  <form id="form1" runat="server">
    <asp:ScriptManager ID="scriptManager" runat="server" EnablePageMethods="true" ScriptMode="Release" LoadScriptsBeforeUI="true"></asp:ScriptManager>
    <div>
      <asp:TextBox id="datepicker" runat="server"></asp:TextBox>
      <asp:TextBox id="btnJS" runat="server" OnTextChanged="datepicker_onchange();return false;" />
      
    </div>
  </form>
</body>
</html>


... VS compiler throws a couple of errors..

Error 1 ) expected C:\inetpub\wwwroot\WebPages\Javascript.aspx 27
Error 2 Invalid expression term ')' C:\inetpub\wwwroot\WebPages\Javascript.aspx 27

this is the codebehind method:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

public partial class WebPages_Javascript : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string loadStandings(string str)
    {
      string fullName = "Hello " + str;
      return fullName;
    }
}


I guess.. Are there different parameters expected by the different events of the asp controls??
Thx for your help! :-)
Posted
Updated 20-May-14 23:47pm
v2

1 solution

OnTextChanged is a server side event and can not be handled from the client.
For click event there is a OnClick and OnClientClick to let you easily bind to both...
There is not build in support to handle text change on the client for ASP.NET TextBox.
You may use jQuery for that: http://api.jquery.com/change/[^]
 
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