views:

467

answers:

3

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();
+1  A: 

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;"
Andris
A: 

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);
  });
});
Jonathan Sampson
+1  A: 

Hey,

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

Brian