开发者

ASP.NET 4.0 - Calling a server-side method from a client side method - PageMethods

开发者 https://www.devze.com 2023-01-08 18:02 出处:网络
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 (

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.

EDIT:

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?

EDIT 2:

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?????? possibl开发者_StackOverflowe????? Then, even jQuery will not help....


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


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


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

hope this will help.

0

精彩评论

暂无评论...
验证码 换一张
取 消