简体   繁体   English

使用jQuery load()调用.aspx.cs代码后面的函数

[英]Calling a function in the .aspx.cs code behind file with jQuery load()

So I have something like this situation: 所以我有类似这样的情况:

    $(document).on('click', 'a[data-link]', function () {
        var $this = $(this);
        url = $this.data('link');
        $("#imagePreview").load("imageProcess.aspx?" + url);

where url holds GET parameters. 其中url包含GET参数。 But imageProcess.aspx is different than the file I'm in ( dashboard.aspx ) and I need to reference some panels within my dashboard.aspx file. 但是imageProcess.aspx与我所在的文件( dashboard.aspx )不同,我需要在dashboard.aspx文件中引用一些面板。 So my question is, using the .load() function, or even any function that could get the job done, how do I call a function, with GET parameters, in the dashboard.aspx code behind file? 所以我的问题是,使用.load()函数,甚至任何可以完成工作的函数,如何在dashboard.aspx代码后面的文件中使用GET参数调用函数? I'm fairly new to the .NET framework so I apologize if the question sounds elementary. 我是.NET框架的新手,所以如果问题听起来很简单,我会道歉。

In your imageProcess.aspx.cs create a webmethod like: 在您的imageProcess.aspx.cs创建一个WebMethod,如:

[WebMethod]
public static string YourMethod(your parameters)
{
//Do Your Work
}

and in your dashboard page, in javascript use jquery to send request your webmethod like: 并在您的仪表板页面中,在javascript中使用jquery发送请求您的webmethod如:

$.ajax({
type: "POST",
    url: "imageProcess.aspx/YourMethod",
    data: "{parameter1Name:'" + JSON.stringify(parameter1value) + "', Parameter2Name:'" + JSON.stringify(parmeter2Value) + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data) {
// do your success work, keep in mind that your returned data will be in data.d
    },
error: function(XMLHttpRequest, textStatus, errorThrown) {
// do your failuer work
}
});

I hope it will give you a guidance to achieve your task. 我希望它能为您提供指导,帮助您完成任务。

Just to make sure: You are trying to access functionality from a different aspx page than the one you are currently on. 只是为了确保:您正尝试从不同于当前所在页面的aspx页面访问功能。 I'm not entirely sure if you can do that the easy way by java script. 我不完全确定你是否可以通过java脚本轻松实现这一点。 Maybe someone else knows a better way, but the way I would do it is creating ashx service page which will handle your request so you can provide the data you need (in your case an image) 也许其他人知道更好的方法,但我这样做的方法是创建ashx服务页面,它将处理您的请求,以便您可以提供所需的数据(在您的情况下是图像)

For more information see http://www.dotnetperls.com/ashx 有关更多信息,请访问http://www.dotnetperls.com/ashx

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

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