By codette

2009-02-23 19:38:56 8 Comments

I know that I can call a page method with jquery using the following syntax

  type: "POST",
  url: "Default.aspx/GetDate",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Replace the div's content with the page method's return.

This works for aspx pages but is it possible with ascx pages? (web controls)

I've been trying it for about half an hour and since I can't get it to work I'm wondering if it's even possible.

Note: Just to be clear, when I try to call the ascx page I am updating the url in jquery :)


@Chưa biết 2014-05-30 16:50:47

You can write method GetDate() in Default.aspx or an other aspx file AND YOU CAN CALL THAT METHOD FROM .ASCX FILE.


In UserControl.ascx:

 type: "POST",
 url: "Default.aspx/GetDate",
 data: "{}",
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 success: function(msg) {
 // Replace the div's content with the page method's return.


In Default.aspx.cs:

Public void GetDate() //Public static void { //your code here }

@Chad Scharf 2009-11-05 03:29:50

Here is a way to get around the limitation of only having Page methods (ScriptMethod) available in ASPX pages. This example builds a proxy using a page base class and a special attribute to decorate methods in your ASCX page's code-behind and have those accessible via the client.

Creating a Page method (ScriptMethod) within an ASCX user control using AJAX, JSON, base classes and reflection

@Corey Sunwold 2011-02-17 21:04:59

It looks like your link is dead.

@David Robbins 2011-11-28 19:37:29

+1. This is article that got me started with jQuery and PageMethods.

@Justin L. 2016-09-07 13:11:27

Link marked as having malware. Chrome and Firefox block it

@hunter 2009-02-24 01:46:56

I would create a Generic Handler (.ashx) which Loads the control and writes its rendered HTML to the Response.

@RSolberg 2009-02-23 19:51:46

What about creating the method on the .aspx page that does what it needs to with the information from the control? I know people go back and fourth on what controls are supposed to contain or not contain, but if the control only contains properties and objects, I would think having the function in the .aspx page could work for you. Obviously there would be many trade-offs.

@Rex Miller 2009-02-23 19:43:20

I don't think it's possible by requesting the ascx file directly--i.e. supplying "MyControl.ascx" as the url parameter to $.ajax(..). The ascx file isn't exposed directly by the web server.

You can, I believe, supply the url of the aspx page containing the user control--i.e. if an instance of MyControl.ascx lives on MyPage.aspx you would have to supply "MyPage.aspx" as the url parameter. It sounds like that might defeat the purpose for what you're trying to accomplish though.

EDIT: What Clyde said below seems like a good idea. I'm doing something similar myself by including the ascx control on a page whose job is more or less just to host it for access from client-side script.

@Justin L. 2016-09-07 13:14:43

Does the first part here work? I have to send JSON data to a function in an ascx.cs file and would rather not have to refactor this ascx markup and code into its containing aspx page.

@Clyde 2009-02-23 19:45:21

No, because ascx controls don't represent a real URL that can be accessed from a client machine. They're purely server-side meant to embed in other pages.

What you might want to do is just have an aspx page that provides the same snippet of html you currently have in your ascx file. An aspx page doesn't necessarily need to provide a full html document (<html><body> etc.), it can just render the user control you're interested in.

We use this technique all the time with the ingrid plugin, which requires a callback url for the table contents.

@Joel Coehoorn 2009-02-23 19:48:38

I would add that the way you provide that snippet is by including the control on the page, rather than duplicating the code.

@Clyde 2009-02-23 20:01:27

If the control is also used server-side, I agree absolutely. However, if the purpose is purely to be used as a jQuery callback, and the control is never used inline another aspx page on the server side, then that's just unnecessary clutter. In that case, just put the code in the aspx page.

@eglasius 2009-02-23 19:43:13

I don't think it will be possible, as user controls aren't meant to be accessible externally (outside of a page). I suggest just using a script service (a web service).

Related Questions

Sponsored Content

39 Answered Questions

[SOLVED] How do I return the response from an asynchronous call?

38 Answered Questions

[SOLVED] Add table row in jQuery

57 Answered Questions

[SOLVED] How do I check if an element is hidden in jQuery?

18 Answered Questions

[SOLVED] Disable/enable an input with jQuery?

30 Answered Questions

[SOLVED] jQuery scroll to element

42 Answered Questions

[SOLVED] Is there an "exists" function for jQuery?

  • 2008-08-27 19:49:41
  • Jake McGraw
  • 746465 View
  • 2777 Score
  • 42 Answer
  • Tags:   javascript jquery

41 Answered Questions

[SOLVED] Setting "checked" for a checkbox with jQuery

28 Answered Questions

[SOLVED] How can I refresh a page with jQuery?

66 Answered Questions

[SOLVED] How do I check whether a checkbox is checked in jQuery?

Sponsored Content