Calling ASP Functions from java script

Categorized Under: Development, English 2 Commented

Umm i think some may get confused that isn’t it that Ajax can call server side functions from JavaScript ?? ! well thats write but i want to talk about those people who are still using Microsoft .NET 2005 and not lucky enough to get gaya or other ajax plug-in from Microsoft, or those who are still not ready to deal with Ajax and other Ajax/JavaScript plug ins such as jquery .

Most of the old .NET developer are used to have their web applications deal with post-backs but very few of them really knows how these post-backs do work. well most of the things in .NET application are done depending on client side script (JavaScript).

For that when you have a good amount of JavaScript controlling your application you need to know how to make that JavaScript call an asp function.

Before explaining how to do this first lets see why do we need it ? Well may be you will need to do something with JavaScript before calling server side codes, like validating entered data ( well i know that is a very bad example since in .NET there is a ready validating controles) a better example lets say you have created a web page that allow users to draw for example mind maps, or charts, or mouse gestures so that when user drag his mouse in a specific way there will be a specific response from your web application.

well in all of these examples JavaScript or JavaScript frameworks like jquery will be doing most of the things in the application here the decision to make a server side function call should be given to java script code.

Calling asp.NET function by JavaScript you need to do a simple trick,

Well as i found from some of my tries to get there, that you can not do it having normal form buttons you will need at least one “link button” (sure you can hide it, but NEVER make visible = false because it will be removed from the HTML code; instead apply CSS style on it that make it hidden)

visibility:hidden;

then you need to call the post back function that usually is called if you really do click that link_button and in the asp.NET code do call your asp function in the link_button event handler function.

well thats a lot of blah blah, theoretical talks, well then lets get into action then.

here i will show a very simple example (sorry i am out of creativity by now so the example will be very simple) i created a very simple page with a text box an a link button

i added a very simple response code to the link button click event in C#

well yeah this is our server side function (i told you this week i am out of creativity).

and then i added they style to hide the link button

now even in the IDE you just cannot see the link button

now the real thing is in the java script i have done a very simple and a very boring thing witch is on each button press it makes the text in the text box uppercase and if the length of text reaches to 6 it will call the server side function

i have added it just before the end of form tag


well you can see the real point is in the function __doPostBack(”,”)
which is created by Visual Studio it self you do not have to program it and it can call server side functions by its own way (well what you do not know that there are a lot of hidden fields in your page that are getting created by visual studio and are used for these kind of stuff)

and the reason i say that we must have a link button is that i found Visual studio do not create this JavaScript function __doPostBack(”,”) if we dont have link button

well thats all for today i think hope this thing (which i had to search and try a lot of things to be able to do) will help you in your projects

Related posts:

  1. Adding a friendly splash (waiting screen) while the code is being executed in ASP
  2. JCodeBase 1.1 Beta
  3. JavaScript, the language for them all!! (Jquery with gridview in asp.net 2.0)

2 Responses to “Calling ASP Functions from java script”

  1. Arkan Hadi says:

    hey, i didnt start with asp.NET yet, am still old school (asp), so am not sure i fully understand what ur trying to do … are you showing how to call a server-side functions from a client-side script?
    i didnt know that its possible to do that, atleast not in ASP, is it? Lol?

  2. Ali Hassan Al Lawati says:

    hi Arkan, yes that is exactly what i am showing how to call server side function through a client side script :)

    well yeah you are right it is not possible in old ASP

    it is possible in asp.NET because by default asp.NET uses a lot of (javascripts and hidden fields) that are created by default

    any how while calling the server side function the page will get refreshed :)

Leave a Reply