Shantanu Gupta
Shantanu Gupta

Reputation: 21188

How to call two functions on single event. one as a javascript function other a server side function

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

 <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();

Is it correct ?

onblur="ControlOnBlur(); function2();

Upvotes: 3

Views: 6458

Answers (3)

Brian Mains
Brian Mains

Reputation: 50728

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.

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.

HTH

Upvotes: 1

Sampson
Sampson

Reputation: 268326

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.

Using jQuery, it would look like this:

$("#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);
  });
});

Upvotes: 0

Andris
Andris

Reputation: 27865

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.

onblur="sentence1; sentence2; sentence3;"

Upvotes: 3

Related Questions