简体   繁体   English

使用jQuery AJAX在控制代码隐藏而不是页面代码隐藏中调用ASP.NET函数

[英]Using jQuery AJAX to call ASP.NET function in control code-behind instead of page code-behind

I have a user control that I'm creating that is using some AJAX in jQuery. 我有一个用户控件,我正在创建在jQuery中使用一些AJAX。

I need to call a function in the code-behind of my control, but every example I find online looks like this: 我需要在我的控件的代码隐藏中调用一个函数,但我在网上找到的每个例子都是这样的:

$("input").click(function() {
    $.ajax({
        type: "POST",
        url: "Default.aspx/GetResult",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            //do something
        }
    });
});

This works fine if I have the method in the Default.aspx page. 如果我在Default.aspx页面中有方法,这可以正常工作。 But I don't want to have the function there, I need the function in the code-behind of my control. 但是我不想在那里使用该函数,我需要在我的控件的代码隐藏中使用该函数。 How can I modify the url property to call the correct function? 如何修改url属性以调用正确的函数?

I tried: 我试过了:

url: "GetResult"

but that didn't work. 但那没用。

The way to handle this is to have the webmethod in your page, then just pass the values directly to a control method with the same signature in your control - there is no other way to do this. 处理此问题的方法是在页面中使用webmethod,然后将值直接传递给控件中具有相同签名的控件方法 - 没有其他方法可以执行此操作。

In other words, ALL the page method does is call the usercontrol method so it is really small. 换句话说,所有页面方法都会调用usercontrol方法,因此它非常小。 IF you have the same signature for multiple child controls, you could pass a parameter to tell the page method which one to call/use. 如果您有多个子控件的相同签名,则可以传递一个参数来告诉页面方法调用/使用哪个。

EDIT: Per request (very very simple example). 编辑:每个请求(非常非常简单的例子)。 You can find other examples with more complex types being passed to the server side method. 您可以找到将更复杂类型传递给服务器端方法的其他示例。 for instance see my answer here: Jquery .ajax async postback on C# UserControl 例如,请参阅我的答案: C#UserControl上的Jquery .ajax异步回发

Example: Page method: note the "static" part. 示例:页面方法:记下“静态”部分。

[WebMethod]
public static string GetServerTimeString()
{
    return MyNamespace.UserControls.Menu.ucHelloWorld();
}

User Control Method: 用户控制方法:

public static string ucHelloWorld()
{
    return "howdy from myUserControl.cs at: " + DateTime.Now.ToString();
}

Client ajax via jquery: 客户端ajax通过jquery:

$(document).ready(function()
{
    /***************************************/
    function testLoadTime(jdata)
    {
        $("#timeResult").text(jdata);

    };
    $("#testTimeServerButton").click(function()
    {
        //alert("beep");
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: "{}",
            dataFilter: function(data)
            {
                var msg;
                if (typeof (JSON) !== 'undefined' &&
                typeof (JSON.parse) === 'function')
                    msg = JSON.parse(data);
                else
                    msg = eval('(' + data + ')');
                if (msg.hasOwnProperty('d'))
                    return msg.d;
                else
                    return msg;
            },
            url: "MyPage.aspx/GetServerTimeString",
            success: function(msg)
            {
                testLoadTime(msg);
            }
        });
    });
});

Note: the dataFilter: function(data)... part of the ajax is so that it works with 2.0 and 3.5 asp.net ajax without changing the client code. 注意:dataFilter:function(data)... ajax的一部分是这样的,它可以在不改变客户端代码的情况下使用2.0和3.5 asp.net ajax。

You can't... WebMethods have to be in WebServices or Pages, they cannot be inside UserControls. 你不能...... WebMethods必须在WebServices或Pages中,它们不能在UserControls中。

Think about it a different way to see the issue a bit clearer...what's the URL to a UserControl? 想想看问题更清楚一点的另一种方式...... UserControl的URL是什么? Since there's no way to access them you can't get at the method directly. 由于无法访问它们,因此无法直接获取该方法。 You could try another way perhaps, maybe a proxy method in your page? 您可以尝试另一种方式,也许在您的页面中使用代理方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM