I’m in the code behind of a generic http handler (.ashx) and I’d like to get a reference to the instance of the calling .aspx page, so I can call some methods/get some properties of it. I can easily call static methods of the page, but I’m not able to get the actual object instance.
Is there a way, without resorting to services/webmethods/whatnot? Thanks!
EDIT:
I call the ashx from the JS inside the aspx page
$.ajax({
url: "handler.ashx",
context: "my content"
}).done(function() {
alert("Done");
});
Then I update an asp:Label
with the result of it.
I’ve found a way to do it anyway (you can do it via JQuery from JS for instance), but now I’m curious if you can do it from the code behind simply calling some pageInstance.setMyLabel(ashxResult)
or something like this.
2
Answers
There is no direct way to modify the contents of your calling .aspx-page via server-side code.
You should (like you mention yourself) process the results of the call to your .ashx-handler with javascript.
If you would like to use some results ‘serverside’ I think the only option is that you write some data to the session-object during the processing of the .ashx-handler.
On the next postback of the .aspx-page you could use that data to accomplish some change. If you would like to do that, please refer to this question also:
How to access Session in .ashx file?
The instance of the page class only exists for as long as it takes to process the request and send the response back to the client. By the time the Javascript code executes and makes a request to the
ashx
file, the page instance has been destroyed.ASP.NET Page Life Cycle Overview | Microsoft Docs