How to call server side function from client side JavaScript?

I want to call a server-side function, like a button's click event or just another private or
public function , from a client-side JavaScript function.
How do I do that?

thanX
Anz

If this post was useful to you, please mark it as answer.

ClientSideAsp.Net | Blog
0
anzer
5/18/2005 6:13:36 AM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 6 followers.

💬 4 Replies
👁️‍🗨️ 3275 Views

Maybe alittle more that you need, but.......

1) Put a button on the page. Lets say its id is Button1. Make sure that its has causesValidation=false
2) Write the code for the button.
3) Write the following in the PAge_Load event

Button1.Attributes.Add("onClick", "myJSFunction(); return false")

4) Copy this javascript onto your page.

function
myJSFunction()
{

        //Fix to get the button to validate the page
       
if (typeof(Page_ClientValidate) == 'function')

       
{
               
Page_ClientValidate();

       
}

       
if (Page_IsValid)

       
{
               
var answer = confirm('Do you wish to continue?');

                
if (answer)

               
{
                      //This calls the Button1's server side code
                     
__doPostBack('btnButton1','');
                }
         
}

}


Intelligence is a burden.
Jagdip Singh Ajimal
Manchester (and yes, I am a Manchester United fan)
0
jagdipa
5/18/2005 8:13:12 AM
Hi jagdipa 
i didnt understand
Page_ClientValidate and Page_IsValid means. Is it a Javascript function defined by me or its an inbuilt capability like serverside Page.IsValid.
Thanks alot. and if there is any method to call a server side function instead of an event.
Anz

Gazing through the window at the world outside
Wondering if mother earth will survive
Hoping that mankind will stop abusing her, sometime






If this post was useful to you, please mark it as answer.

ClientSideAsp.Net | Blog
1
anzer
5/18/2005 9:07:11 AM
If you have validators on the page,
then ASP.NET automatically puts javascript on the page to handle client
side validation. That is where the Page_ClientValidate and Page_IsValid are coming from.
You may have problems with this if you are using anything other that IE (I havent tested this outside of IE). But, you could simply modify it to:

function
myJSFunction()
{

        //This calls the Button1's server side code
       
__doPostBack('btnButton1','');
}

The reason I included this is because people often want client side validation, and a confirmation box before running the buttons serverside code. That is what this javascript function was originally designed for.

As far as I know, there is no way to call a serverside function instead of an event. But, one way round this could be to just make Button1 invisible in the Page_Load event (Button1.visible = false). That way, there is no button on the page, but the code for the button click event is still there.

Intelligence is a burden.
Jagdip Singh Ajimal
Manchester (and yes, I am a Manchester United fan)
0
jagdipa
5/18/2005 11:05:20 AM
I tested your solution ya it works good. Thanks.
ASP.net's inbuilt validators are not working in Mozilla and all so we are using Javascript.






If this post was useful to you, please mark it as answer.

ClientSideAsp.Net | Blog
0
anzer
5/18/2005 12:47:39 PM