简体   繁体   中英

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. 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. 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? I'm fairly new to the .NET framework so I apologize if the question sounds elementary.

In your imageProcess.aspx.cs create a webmethod like:

[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:

$.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. I'm not entirely sure if you can do that the easy way by java script. 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)

For more information see http://www.dotnetperls.com/ashx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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