admin管理员组

文章数量:1336616

It might be seems to be FAQ. but I got Something in my mind and let me explain that. I search an test many way's for doing that and at the end I found these soultion:

  1. Jquery With Ajax
  2. Webform_Docallback
  3. Script Manager and UpdatePanel

so what I need is Something Like first solution, but also want to access to this object and even page controls. and Reason don't use the second and third solution is they call Page-Reload first and then call that method. So that's what I Need!

Is there any solution? any tip's?

Additional Info :
I need something like: call Method by button_click and go to the server side. in Server-Side call Wcf-Service and finally do something. (like binding grid datasources, change textbox values or etc).

Almost jQuery with Ajax is what I need. but I have these problem with this way!

  1. didn't have access to this Object
  2. didn't have access to Page-Controls

So How to solve these issue's?

It might be seems to be FAQ. but I got Something in my mind and let me explain that. I search an test many way's for doing that and at the end I found these soultion:

  1. Jquery With Ajax
  2. Webform_Docallback
  3. Script Manager and UpdatePanel

so what I need is Something Like first solution, but also want to access to this object and even page controls. and Reason don't use the second and third solution is they call Page-Reload first and then call that method. So that's what I Need!

Is there any solution? any tip's?

Additional Info :
I need something like: call Method by button_click and go to the server side. in Server-Side call Wcf-Service and finally do something. (like binding grid datasources, change textbox values or etc).

Almost jQuery with Ajax is what I need. but I have these problem with this way!

  1. didn't have access to this Object
  2. didn't have access to Page-Controls

So How to solve these issue's?

Share Improve this question edited Aug 23, 2011 at 3:57 Rev asked Aug 21, 2011 at 9:54 RevRev 2,2758 gold badges45 silver badges77 bronze badges 1
  • I'd go with jquery on this one. Phil Haack recently released this sweet addon to MVC for calling action methods from JS: haacked./archive/2011/08/18/… – Jamie Dixon Commented Aug 21, 2011 at 9:57
Add a ment  | 

3 Answers 3

Reset to default 3

For the first one

Create a WebMethod in the .cs file

[WebMethod]
public static string Foo() 
{ 
  //......
}

If you want to use session you should

[WebMethod(EnableSession = true)] or [WebMethod(true)]
public static string Foo() 
{
    //......
}

Then , invoke the webmothod by js

$.ajax({
type: "POST",
contentType: "application/json",
url: "WebForm1.aspx/Foo",
data: "{}",
dataType: "json",
success: function(){.......}
});

Hope it useful...

        [WebMethod()]
        [System.Web.Script.Services.ScriptMethod()] 
        public static void YourTaskNAme(Your Parameter)
        {
             // Yuor Code here
        }

Update the script manager to have the following property EnablePageMethods="true"

finally use the javascript to call this method

PageMethods.YourTaskNAme(Your Parameter, OnMethodFinished);

function OnMethodFinished() {
alert('Call to function worked.')
        }

You can bypass any logic you have inside your Page_Load method by checking the ScriptManager.IsInAsyncPostBack property with a ScriptManager and UpdatePanel.

本文标签: cHow to call page method without pagereloadStack Overflow