views:

583

answers:

2

Hi all,

I have a cascading dropdown (3 of them) Type, Categories and Sub Categories. Type loads first and upon selection of Type, Category load and selection of Category, Sub Category loads. Also i have 2 buttons, "Add Category" and "Add Sub Category" Upon clicking on these buttons, i call a JQuery Modal Form to add them. I use Webmethod in code behind to add them to database

This works perfectly in ASPX page.

Since I need use this in 3-4 pages, i thought of making the above as User control (ASCX). When i try to use this in a webpage, the webmethods in ASCX don't get called.

Is my approach correct? what should be done for my scenario

lOoking forward for your suggestions.

Thanks in advance Karthik

A: 

Was your web method was a static method in the code behind that was marked with WebMethod attribute as described in the "Calling Static Methods in an ASP.NET Web Page" section here before you moved it? If so this type of webmethod only works at the page level and cannot be used in a usercontrol. For an alternative read the first two sections of this page.

drs9222
Yes the web methods are static. The same works if i have it as norml aSPX pages and not as web user controls. BTW i am using ASP.NET 2.0 with JQuery
Karthik K
@KarthikK, Then I stand by my answer that you can't move them into a user control and have them work.
drs9222
+3  A: 

Hi, i dont think you can have a WebMethod within a ASCX Control. I solved it for my Problem like this:

AJAXBridge:

namespace Demo{
public partial class AjaxBridge : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    [WebMethod(EnableSession = true)]
    public static string Control_GetTest()
    {
        return Control.GetTest();
    }
}}

Control.ascx.cs

namespace Demo{
public partial class Control : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    HttpContext.Current.Session["test"] = DateTime.Now.ToString();
    }

    // ALMOST A WEB METHOD
    public static string GetTest()
    {
        return " is " + HttpContext.Current.Session["test"];
    }
}}

Control.ascx

<script type="text/javascript">    
var dataSend = {};

$.ajax({
    type: "POST",
    url: "AjaxBridge.aspx/Control_GetTest",
    data: dataSend,
    cache: false,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    beforeSend: function(data) {
        alert("before");
    },
    success: function(data) {
        alert("Page Load Time from Session " + data.d);
    },
    fail: function() {
        alert("fail");
    }
});    </script>

So you have one ASPX which acts basically like a Interface for all AJAX Methods in all your Web Controls. There also some advantages like having overview and control of all exposed WebMethods, which makes it a lot easier to handle security matters (eg. with Annotations).

csharpnoob