如何在单个事件上调用两个函数。一个是javascript函数,另一个是服务器端函数。

时间:2022-08-24 15:00:39

I want to call two functions on same event 1 is client side and other 1 is server side. How can i do that

我想在同一个事件上调用两个函数1是客户端,1是服务器端。我怎么做呢

 <input type="text" ID="txtUserName" runat="server" maxlength="50"
                            class="DefaultTextbox" style="width:180px;" value="" 
                            onfocus="ControlOnFocus('', this, spanUserName);"
                            onblur="ControlOnBlur('',this, spanUserName); "
                            />

onblur="ControlOnBlur(); function2();

onblur = " ControlOnBlur();function2();

Is it correct ?

是正确的吗?

onblur="ControlOnBlur(); function2();

3 个解决方案

#1


3  

Thats correct. Actually it's not very good practice to define events inlined in HTML but it works. Everyhing between " and " in onblur="" is treated as one long JavaScript code block so you could write anything in it, even your whole program if you would like.

这是正确的。实际上,用HTML来定义事件并不是很好的做法,但它确实有效。“and”和“in onblur=”之间的所有内容都被视为一个长长的JavaScript代码块,因此您可以在其中编写任何内容,甚至可以编写整个程序。

onblur="sentence1; sentence2; sentence3;"

#2


1  

If using web forms, you want to post back to the server to process a server-side event, you can use __doPostBack(this.name, '') to post back to the server (it was mentioned alternatively that you can use a server side event to output (GetClientResourceUrl I believe or named similarly) this, but I like to use the __doPostBack method to perform the postback.

如果使用web表单,你想回服务器来处理服务器端事件,您可以使用__doPostBack(this.name”)发布到服务器(或者提到,您可以使用一个服务器端事件输出(GetClientResourceUrl我相信或命名同样),但我喜欢使用__doPostBack方法执行回发。

If using MVC, you can invoke an action method using $.get or $.post as in $.get("/Controller/Action", function(result) { }). For web forms, you can't invoke a method directly. You can invoke a web service or page method.

如果使用MVC,可以使用$调用操作方法。获得美元。在美元的职务。get(" /控制器/行动”、功能(结果){ })。对于web表单,不能直接调用方法。您可以调用web服务或页面方法。

HTH

HTH

#3


0  

The closest you can get is calling a local function (javascript), and then firing off a request to the server via an asynchrounous call (Ajax). You cannot directly call a method on the server from the client though.

您可以得到的最接近的方法是调用本地函数(javascript),然后通过异步调用(Ajax)向服务器发送请求。但是,您不能直接从客户端调用服务器上的方法。

Using jQuery, it would look like this:

使用jQuery,会是这样的:

$("#txtUserName").blur(function(e){
  ControlOnBlur(); // call first function
  $.post("/methods", {methodName:"refreshData", function(results){
    /* results represents that which was returned from the server */
    alert(results);
  });
});

#1


3  

Thats correct. Actually it's not very good practice to define events inlined in HTML but it works. Everyhing between " and " in onblur="" is treated as one long JavaScript code block so you could write anything in it, even your whole program if you would like.

这是正确的。实际上,用HTML来定义事件并不是很好的做法,但它确实有效。“and”和“in onblur=”之间的所有内容都被视为一个长长的JavaScript代码块,因此您可以在其中编写任何内容,甚至可以编写整个程序。

onblur="sentence1; sentence2; sentence3;"

#2


1  

If using web forms, you want to post back to the server to process a server-side event, you can use __doPostBack(this.name, '') to post back to the server (it was mentioned alternatively that you can use a server side event to output (GetClientResourceUrl I believe or named similarly) this, but I like to use the __doPostBack method to perform the postback.

如果使用web表单,你想回服务器来处理服务器端事件,您可以使用__doPostBack(this.name”)发布到服务器(或者提到,您可以使用一个服务器端事件输出(GetClientResourceUrl我相信或命名同样),但我喜欢使用__doPostBack方法执行回发。

If using MVC, you can invoke an action method using $.get or $.post as in $.get("/Controller/Action", function(result) { }). For web forms, you can't invoke a method directly. You can invoke a web service or page method.

如果使用MVC,可以使用$调用操作方法。获得美元。在美元的职务。get(" /控制器/行动”、功能(结果){ })。对于web表单,不能直接调用方法。您可以调用web服务或页面方法。

HTH

HTH

#3


0  

The closest you can get is calling a local function (javascript), and then firing off a request to the server via an asynchrounous call (Ajax). You cannot directly call a method on the server from the client though.

您可以得到的最接近的方法是调用本地函数(javascript),然后通过异步调用(Ajax)向服务器发送请求。但是,您不能直接从客户端调用服务器上的方法。

Using jQuery, it would look like this:

使用jQuery,会是这样的:

$("#txtUserName").blur(function(e){
  ControlOnBlur(); // call first function
  $.post("/methods", {methodName:"refreshData", function(results){
    /* results represents that which was returned from the server */
    alert(results);
  });
});