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

2
  • 1
    Have tried using AJAX? You can call serverside components using JS code. Commented Feb 26, 2012 at 15:51
  • Can I use Ajax to call non-static functions? how? Commented Feb 26, 2012 at 16:11

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

8 Comments

Is there an async approch to this?
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
is this method considered to be better then the hidden button? And what will be the differences?
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, OK... so... what would be a better option?
|
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.

2 Comments

Problem is I dont want to use static methods (since I need to be able to communicate with objects in the page)
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.