How to call server side(C#) function from Client side(Javascript) function?



Hi i have used below functions, 
function
HandleOnClose() {// this function will be called after clicking browsers close button in Internet explorer
if (event.clientY < 0) {
event.returnValue = 'Are you sure you want to leave the page?';
}
// if he clicks ok then i need to call one server side function in this instance.. else not.
}
<body onbeforeunload="HandleOnClose()" leftmargin="0" rightmargin="0">other controls in page..</div>
 How to achieve this?
Thanks
0
vishwanatha
2/20/2007 6:04:03 AM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 6 followers.

💬 14 Replies
👁️‍🗨️ 2704 Views



MS AJAX Extensions would be the option of choice here. Combined with webservices you can call on serverside functions from your clientside code.
This article will get you headstarted http://ajax.asp.net/docs/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx

"What good is money if it can't inspire terror in your fellow man?"
0
ViktorBergmanSwe
2/20/2007 7:29:10 AM

To execute Server Side code you must have a postback, but that can be conditional.
 
Eg.
 
<head runat="server">
    <title>Untitled Pge</title>
    <script type="text/javascript" language="javascript">
   
    function doIt(){
    
     var condition = false; // would really be a condition (true causes postback)
    
     return condition;
           
    }
   
   
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:return doIt();"/>
    </div>
    </form>
</body>
</html>
  <br class="MsoNormal" style="MARGIN:0cm 0cm 0pt;mso-layout-grid-align:none;">Server side test
<br class="MsoNormal" style="MARGIN:0cm 0cm 0pt;mso-layout-grid-align:none;">
 
 
 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Write("Server side postback/ button click handler")
    End Sub
   

 
Regards,
Martin.

Rgds,
Martin.

For the benefit of all users please mark any post answers as appropriate.
0
mokeefe
2/20/2007 7:36:51 AM

That will not solve that fact that he wants to trigger serverside code from a clientside event.
Webmethods/Pagemethods is the way to go here.

"What good is money if it can't inspire terror in your fellow man?"
0
ViktorBergmanSwe
2/20/2007 8:44:56 AM

What is most appropriate will depend on what his real objectives are.
If it is simply to audit the close of the window a Form submit may be ample and is certainly simpler and involves miniscule (lower) overhead and end user skills.
rgds,
Martin.

Rgds,
Martin.

For the benefit of all users please mark any post answers as appropriate.
0
mokeefe
2/20/2007 8:49:54 AM

That is true. Now he has two alternatives, thats always something right? ;-).
Have nice day :-) 

"What good is money if it can't inspire terror in your fellow man?"
0
ViktorBergmanSwe
2/20/2007 9:10:55 AM

HI,
 second reply solution doesnt match my requirement.
Thanks
 
0
vishwanatha
2/20/2007 9:21:38 AM

Hi,
   Is there any way to call server side function from client side?
Thanks
Vishwanath
0
vishwanatha
2/20/2007 11:34:23 AM

            
vishwanatha:

Hi,

   Is there any way to call server side function from client side?

Thanks

Vishwanath


Dude...
http://ajax.asp.net/docs/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx.
 

"What good is money if it can't inspire terror in your fellow man?"
0
ViktorBergmanSwe
2/20/2007 6:57:45 PM

            
vishwanatha:

Hi,

   Is there any way to call server side function from client side?

Thanks

Vishwanath


Seriously dude...

http://ajax.asp.net/docs/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx.
 

"What good is money if it can't inspire terror in your fellow man?"
0
ViktorBergmanSwe
2/20/2007 6:58:30 PM

            
ViktorBergmanSwe:

That is true. Now he has two alternatives, thats always something right?


Absolutely.

Rgds,
Martin.

For the benefit of all users please mark any post answers as appropriate.
0
mokeefe
2/22/2007 10:41:50 AM

Hi,
  Can i not code it without web service?
Thanks
 
0
vishwanatha
2/22/2007 11:31:50 AM

To call serverside code from clientside events you will need to use AJAX, and the easiest way for that is to use the Microsoft AJAX Extensions.
These are the options you have:
1. Create a webservice in a .asmx file, register it with your page scriptmanager.
2. Create some serverside code and place it in your Page, mark it as WebMethods and use it the same way you utilize webservices. Check out http://ajax.asp.net/docs/ViewSample.aspx?sref=Sys.Net.PageMethod%23PageMethod.aspx.
Sure, it will take you some time to learn this stuff... but so does all learning. But creating your own server/clientside hack will just result in even MORE time and most likely a bad design. Instead, read a bit about AJAX and let it leverage your implementation.
 

"What good is money if it can't inspire terror in your fellow man?"
0
ViktorBergmanSwe
2/22/2007 12:01:23 PM

hi,
maybe you wanna try to send the page with server side code through xmlHttp object.
Just remember this run asynchronously.
hopefully that suits you,
 Happy programming,
 recruit.
0
recruit
2/22/2007 1:39:16 PM
Hi,

To summarize, options above are all implemented with creating a additional request to the web server and that's the only way to invoke methods on the server.
No matter which way you take, the basic idea is the same.
If none of them works, I'd like to hear what is your requirement specifically.
0
Raymond
2/25/2007 9:54:37 AM
Reply: