views:

213

answers:

5

I'm trying to access a server side method from a client side one (that is just calling a server method from javascript). I'm using .NET 4.0 Visual Studio 2010 Ultimate. I'm building a web control (ascx). I have an ascx page (the control's html) and an ascx.cs page: The ascx is the following:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="BoxButton.ascx.cs" Inherits="ATB.Controls._BoxButton" %>

<asp:ScriptManager ID="SM_ScriptManager" runat="server" EnablePageMethods="true" />
<script type="text/javascript">
    function handle() {
        PageMethods.manageHandler();
    }
</script>
<span id="BoxButton_HtmlSpan" runat="server" onclick="handle();" style="background-color:#efefef;border-style:solid;border-width:1px;border-color:#cccccc;height:25px;text-align:center;padding:3px 3px 3px 3px;font-family:Arial, Helvetica, sans-serif;font-size:12px;font-style:normal;color:#333333;line-height:15px">
    <asp:Label runat="server" Text="Button" id="Text_Label"></asp:Label>
</span>

And the ascx.cs file is this one (just printing the function):

[System.Web.Services.WebMethod]
   public static string manageHandler() {
   int i = 0;
   System.Console.WriteLine("Ciao");
   return "Hello";
}

Well, as you can see I'm trying to solve my problem through pagemethods, well it does not work and I always getPageMethods is undefined b y the JScript runtime manager. Well, is it possible that PageMethods are not available in .net 4.0?

Anyway, is there another approach for solving the problem regarding server side method calling from client side scripts? I understood that pagemethods are nothing more than a web service, and this bothers me a little when thinking about security in my web app.

+1  A: 

I believe that PageMethods are only available on pages, not on user controls.

John Saunders
Well, I thought about it but found no restrictions when browsing microsoft msdn pages about this argument... anyway, do you know how it is possible to solve this problem withput pagemethods?
Andry
There's no reason I know of why you couldn't just use the WCF service of your choice - RESTful or not, returning XML or JSON, all your choice.
John Saunders
John is correct; the page method trick only works in the code behind of ASPX pages. If a page method worked how you wanted, an ASMX ScriptService will give you the same functionality without the need to hassle with WCF.
Dave Ward
@Dave: no hassle, just built-in obsolescence, stagnation, lack of bug fixes, features, or future.
John Saunders
ScriptServices work great for AJAX callbacks. For that simple scenario, there's no sense in scaring people into over-complicating something that already works well and doesn't *need* the additional bug fixes and features that WCF still does.
Dave Ward
@Dave: bug fixes and features, huh? When I mentioned bug fixes, it's because ASMX bugs are largely not being fixed. If that's ok with you and the OP, then I wish you the best.
John Saunders
What's a specific ASMX "bug" that affects these simple AJAX callbacks with ScriptServices? I don't understand the need to resort to vague scare tactics when it comes to WCF vs. ASMX. Right now, they both have their strengths.
Dave Ward
@Dave: I'm not doing "scare tactics". Why would I? Most of my career has been with companies that don't permit software to be used if bugs aren't going to be fixed in it, so I brought that up. Only the most critical security bugs are being fixed in the ASMX and XML Serialization technologies. BTW, the only strength I know of in ASMX is that it has very few features to confuse developers who get confused between what features are available and which they need to think about.
John Saunders
Simplicity *is* a feature. Added complexity must justify itself. WCF's complexity is justified by its power in a lot of scenarios, but it doesn't bring anything to the table in this one. In fact, JavaScriptSerializer also handles enums and dates more flexibly than DataContractJsonSerializer, which I've found to be very helpful on a regular basis. Ultimately, it's a bit disingenuous to suggest that Visual Studio 2010's `File` > `New File` > `Web Service` is going to disappear overnight or be unsupported any time soon.
Dave Ward
A: 

I would recommend using jQuery, it's much more elegant. Here's an example

Vedran
Changing his calling method doesn't fix his problem.
Corbin March
The last paragraph asks: "Anyway, is there another approach for solving the problem regarding server side method calling from client side scripts?" I've always found PageMethods to be a pain in the ass, whereas jQuery is easier to use. That's why I recommended it
Vedran
-1: how does jQuery in any way change the server side?
John Saunders
I never said it changed anything on the server side, all I'm trying to point out is that I've found jQuery to be more user friendly to use on the client side.
Vedran
A: 

I have found one more tutorial to do it with JQuery: Calling a server side function from JQUERY

hope this will help.

Rahul
Please link directly to articles instead of using a URL shortener. People like to see where a link is taking them.
Bill the Lizard
A: 

Well, it seems that jQuery is a valid solution... but is it able to allow me to call a server side method? I expect to be because we're talking about ajax right?

Andry
A: 

Ah... Another question... I tried to use a webmethod but it does not work getting me errors not finding PageMethods in javascript while I set scriptmanager and method's attribute WebMethod as specified....

Is it because I'm working inside an ascx?????? possible????? Then, even jQuery will not help....

Andry