0

I've came into a need of calling a server-side function from a javscript function.

As much as I know, this isn't really possible since JS is running on the client, thus it is only possible to call a static function (which is not in the same context).

I've read somewhere about the hidden button method, of hiding an asp server-side button, and then clicking it programmatically from JS, hence making postback and then calling the eventhandler.

Few questions about this method -

  1. How good is this method? I mean, is this method is widely used or it considered to be primitve and old?

  2. What are the down sides of this method?

  3. Another thing I read about this method is that if you came to use this method it means that something is wrong with the overall page logic design. Is that on some level true? Is there perhaps a better way to deal with this problem?

Thanks ahead

Skipper Geffen
  • 75
  • 2
  • 11

2 Answers2

1

You can do the postback manually by calling the __doPostBack javascript function manually. Take a look at this other post.

Community
  • 1
  • 1
Dante
  • 3,833
  • 4
  • 38
  • 55
  • Is there an async approch to this? – Skipper Geffen Feb 26 '12 at 16:15
  • you will call __doPostBack javascript function (that is the function of your linkbutton) on onload event, and that as you pressed the button and do the postback normally and will not effect the layout as you asked – Mhmd Feb 26 '12 at 16:22
  • is this method considered to be better then the hidden button? And what will be the differences? – Skipper Geffen Feb 26 '12 at 16:36
  • If you have a button with runat=server, then it will call the __dopostback function itself. If you can avoid the middle man and do the postback yourself, I don't see a reason why you have to create the button. Either way, all solutions seem a bit cumbersome to me... – Dante Feb 26 '12 at 16:38
  • @Dante, OK... so... what would be a better option? – Skipper Geffen Feb 26 '12 at 17:37
  • I would implement the __dopostback call. As far the better option, moving to ASP.NET MVC :) – Dante Feb 26 '12 at 17:44
  • How about using an AjaxManager? I know that there is a way to use it for asyc postbacks – Skipper Geffen Feb 26 '12 at 17:50
  • http://www.simple-talk.com/dotnet/asp.net/asynchronous-client-script-callbacks/ You have to decide if you want async or not, if you need a return value from the server, and based on that decide on the implementation. The functionality should guide the implementation, not the other way around... – Dante Feb 26 '12 at 17:58
0

One decent way of doing it is using PageMethods. You can declare a static method in the code behind that has the attribute [WebMethod]. This means you can access it with an ajax request from the client side. Like this:

[WebMethod]
public static string GetDate()
{
    return DateTime.Now.ToShortDateString();
}

You'll have to use System.Web.Services to get the attribute.

From the client side, you could do something like this, with jQuery:

$.ajax({
    type: "POST",
    url: "YourPage.aspx/GetDate()",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data) {
        alert(data.d);
    }
});

Take note that ASP.net will add a .d object to whatever you're sending back.

AFAIK this is the preferred way to communicate between client side and codebehind, without triggering a postback. It's also pretty clear what's going on.

Using a hidden button with a postback that's triggered by calling click() on the button, is much more "magical" and not at all clear what's going on. The same with __doPostback.

Joakim Johansson
  • 3,196
  • 1
  • 27
  • 43
  • Problem is I dont want to use static methods (since I need to be able to communicate with objects in the page) – Skipper Geffen Feb 26 '12 at 16:15
  • 1
    @SkipperGeffen then no, there are no good ways. ASP.Net is trying to keep state in a stateless protocol. This works best if you're using ASP controls only. If you're set on using javascript then you're best off communicating via ajax and webmethods. The solution in ASP.Net for doing async postbacks is using UpdatePanels. They send way more information than often is necessary though. – Joakim Johansson Feb 26 '12 at 16:20