Reputation: 2202
I have worked on a problem for a couple of hours now. I want to create dynamic Javascripts which I load into the page when I need. This is a project built on Ajax technique. First I tried to do it by PageMethods but PageMethods can only be static due to viewstate and other things. The problem was that I wasn't able to program against the page object or any other controlbecause the method is static.
My next idea which works is that I have an update panel and within this panel I have a hidden field where I store my scripts. I trigger the hiddenfield to do a postback via javascript. In Page_Init I check if it is an asynchrounos postback and if it is the hiddenfield that caused it. If so I register my new script.
Now to the problem. When this happens I don't want to go further in page lifecycle. I am done. I don't want Page_Load to run and Pre_Render etc.. How can I achieve this? Response.End() stops the process of the page but obviously my scripts are not registered.
Allright I can check in every method if it is an asynchrounos postback and if it is the hiddenfield that caused it but I want to build this into a base page class which all pages will inherit from. I want it to be as generic as possible.
Any ideas? I would appreciate if someone could help me on this. Thanks. /John
Upvotes: 0
Views: 583
Reputation: 2202
ASPXpage:
function Generate() {
__doPostBack('hdJS', 'function SayHello(){alert(\'Hello to you\')}');
}
function Execute() {
SayHello();
}
</script>
Codebehind:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
namespace WebApp { public partial class _Default : System.Web.UI.Page { protected void Page_Init(object sender, EventArgs e) { if (ScriptManager1.IsInAsyncPostBack) { if (Request["__EVENTTARGET"] == "hdJS") ScriptManager.RegisterClientScriptBlock(hdJS, hdJS.GetType(), "key", Request["__EVENTARGUMENT"], true); } }
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
Thats is a code example. How would you translate this into a httphandler. As what I can think of I need to postback the same page in some way to have the javascript injected in the page, right? The page is loaded already and I don't want to reload it. This is the main goal. Okay I can reload it with a partial page render as with the update panel but this means the whole page lifecycle is executed again and only the parts inside the panel is updated, but still all controls are inialized and all the code are processed again. I want to avoid this. How can I achieve that with a HttpHandler? /John
Upvotes: 0
Reputation: 12396
The methods you don't want to run are event handlers. As long as you don't end the response, those events are going to fire. That is a fundamental piece of how an aspx page works, it fires events.
If you have code in the event handlers that you don't want to run, then you can:
-set a flag in page_init, and then check for that flag in your event handlers -try to unhook your page events (this is a bad idea and would break further postbacks)
I suspect what you really want to do is create a custom handler that returns your javascript and call that from your loading javascript.
So you have a handler called something like MyJavascriptGenerator.ashx and you have the hidden javascript call that page instead of posting back to the same aspx page.
Without more details about the pages intended behaviour, that's all I can think of.
Upvotes: 1