在不同的控制事件上调用js函数有什么区别吗? [英] Is there any difference calling a js function on different control events?

查看:139
本文介绍了在不同的控制事件上调用js函数有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好!

我正试图在asp:按钮控件的Onclientclick事件上调用js函数。

一切正常!



Hi Guys!
I'm trying to call a js function on the "Onclientclick" event of an asp:button control.
Everything works fine!

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





这样,如果我点击按钮,弹出窗口会显示我在文本框中写的内容。


但如果我尝试在文本框的OntextChanged事件中自动执行此操作(这种方式)...





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)...

<%@ 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编译器抛出一些错误..



错误1)e xpected C:\inetpub \wwwroot \WebPages \ Javascript.aspx 27

错误2无效的表达式术语')'C:\inetpub \wwwroot \ WebPages \ Javascript.aspx 27



这是代码隐藏方法:





... 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:

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





我猜.. asp控件的不同事件是否有不同的参数??

谢谢你的帮助! : - )



I guess.. Are there different parameters expected by the different events of the asp controls??
Thx for your help! :-)

推荐答案

OnTextChanged是服务器端事件,无法从客户端处理。

对于click事件,有一个OnClick和OnClientClick让你轻松绑定到两个...

没有内置支持来处理ASP.NET TextBox客户端上的文本更改。

你可以使用jQuery: http://api.jquery.com/change/ [ ^ ]
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/[^]


这篇关于在不同的控制事件上调用js函数有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆