Stop page process

I've been working on the problem for a couple of hours now. I want to create dynamic Javascripts that I load into a page when I need to. This is a project built on the Ajax technique. I first tried to do it with PageMethods, but PageMethods can only be static due to viewstate and other things. The problem was that I could not program against the page object or any other control, because the method is static.

My next idea that works is that I have an update panel and inside that panel I have a hidden field where my scripts are stored. I am running a hidden field to do a postback via javascript. In Page_Init, I check if it is asynchrounos postback and if it is a hidden field that caused it. If so, I will register my new script.

Now to the problem. When that happens, I don't want to go any further in the page lifecycle. I am everything. I don't want Page_Load to run and Pre_Render, etc. How can I achieve this? Response.End () stops the page process, but obviously my scripts are not registered.

Allright, I can check each method if it is postback asynchronous and if it is a hidden field that caused it, but I want to build it into the base page class that all pages inherit. I want it to be as general as possible.

Any ideas? I would be grateful if someone can help me with this. Thanks. / John

0


a source to share


2 answers


The methods that you don't want to run are event handlers. Until you complete your answer, these events will fire. This is a fundamental part of how an aspx page works, it fires events.

If you have code in your event handlers that you don't want to fire, you can:

- set a flag in page_init and then check that flag in event handlers -require to unhook your events on the page (this is a bad idea and will break further postbacks)



I suspect what you really want to do is create a custom handler that returns your javascript and is called from your bootstrap javascript.

So, you have a handler called something like MyJavascriptGenerator.ashx, and you have a hidden javascript call to that page, not sending back to the same aspx page.

No further information on the intended behavior that I can think of.

+1


a source


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)
    {
    }
}

      

}

Here's some sample code. How would you translate this into httphandler. How can I think that I need to reload the same page in some way in order for the javascript to be injected into the page, right? The page is already loaded and I don't want to reload it. This is the main goal. Okay, I can reload it with a partial page display like in the refresh panel, but that means the whole life cycle of the page is executed again and only parts inside the panel are updated, but all controls are initialized and all code is processed again. I want to avoid this. How can I achieve this using HttpHandler? / John

0


a source







All Articles