After much banging of the head and manual line-by-line code removal, I have figured out that a RegisterClientScriptInclude call inside an ASCX web control is causing multiple page_load events to fire. This only happens when the ASCX is included on some pages, but not others.
Removing this line of code causes the double-postback to stop:
Page.ClientScript.RegisterClientScriptInclude(
Page.GetType(),
"[SCRIPTNAME]",
"[SCRIPTFILE].js")
I also tried this without including Page.GetType(), to no avail.
That line of code shouldn't be causing multiple page loads... There's probably something else going on... I would turn on Tracing at the page level and throw some Trace.Warn statements in to see what's going on...
But I'm not a big fan of the Page.ClientScript.RegisterClientScriptInclude() method and find that it's usually easier just to include the .js file in the parent aspx page... It's a very mild performance hit if that .js file isn't needed on a particular page...
Related
I'm developing an ASP.NET website. Sometimes some modules contain so few content that I present them inside a jQUeryUI dialog and communicate with the server via AJAX. I keep those contents inside a separate file and load them to a jQueryUI dialog according to the appropriate module.I was wondering if it's a good idea to have that content as a plain HTML elements instead of asp ones. Somehow I thought maybe this would reduce the overhead the conversion from asp elements to html elements can cause.
I'd allways go with the aspx Page, because a dynamic Page is more work at the beginning but in the end it almost ever saves time.
Specially when your not sure of the content that will be shown there, it is better.
And for the one reason i do it, is to have everything the same.
One style one way to code.
I'd say this is probably premature optimization. The overhead of an aspx page is in almost all cases negligible. I believe it's more likely that you will some day need to put dynamic things in that page, in which case you would have to convert the html file to an aspx, and change the url for your ajax dialog - which will cost time/money.
If you have aspx pages, or ascs user controls that you do not actually use/run any code, you can set the AutoEventWireup the EnableViewState, and maybe the EnableSessionState to false and stop the calling of the PageLoad and the rest functions and reduce the overhead. So on top of the controls you declare:
<%# Control AutoEventWireup="false" EnableViewState="false" ...
or for page:
<%# Page AutoEventWireup="false" EnableViewState="false" EnableSessionState="false" ...
The disable of the session is let the pages loads in parallel, the disable of the EnableViewState is reduce the size, the AutoEventWireup is reduce the callback hooks and calls.
In general you can use what ever you wish - if your pages can work, but if you like to keep it robust and easy to change or update, or add new functionality in the future, then use dynamic aspx pages.
Similar question: Master page and performance
If I want to add external javascript file into an aspx page in back end code page, in what page life should I add it? Page_Load? or Page_PreInit?
As far as I know it doesn't mind where you put it. I usually put them in the the Page_PreRender event.
There are a couple of reason why I do this.
you want to conditionally add Javascript resources based on some condition in your page, so your need your whole page to be loaded. The PreRender event is the perfect time to check some conditions because everything should be loaded and ready to be renderd.
If you for example add them in the PreInit and in your PageLoad you decide to redirect to a different page you executed unnecessary code.
So even if both points do not apply to the situation I still put them in the PageRender as it's the guideline for the whole project.
I've spent at least 2 days trying anything and googling this...but for some reason I can't get RegisterClientScriptInclude to work the way everyone else has it working?
First off, I am usting .NET 3.5 Ajax,...and I am including javascript in my partial page refreshes...using this code:
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "MyClientCode", script, true);
It works perfectly, my javascript code contained in the script variable is included every partial refresh.
The javascript in script is actually quite extensive though, and I would like to store it in a .js file,..so logically I make a .js file and try to include it using RegisterClientScriptInclude ...however i can't for the life of my get this to work. here's the exact code:
ScriptManager.RegisterClientScriptInclude(this, typeof(Page), "mytestscript", "/js/testscript.js");
the testscript.js file is only included in FULL page refreshes...ie. when I load the page, or do a full postback....i can't get the file to be included in partial refreshes...have no idea why..when viewing the ajax POST in firebug I don't see a difference whether I include the file or not....
both of the ScriptManager Includes are being ran from the exact same place in "Page_Load"...so they should execute every partial refresh (but only the ScriptBlock does).
anyways,..any help or ideas,..or further ways I can trouble shoot this problem, would be appreciated.
Thanks,
Andrew
Here's the key:
partial page refreshes
You have to jump through special hoops to add javascript to a page after the initial load, because loading javascript later is considered a security risk by some (it's also bad for Google indexing).
Instead, register the scripts on the initial page load and just don't execute the scripts until later. If these scripts are created dynamically, I suggest you factor out the static portion and refactor as methods you can call with information returned dynamically from your page refresh.
i was working on a vb.net project and i couldn't find some controls in the code-behind file(*.vb). i was wondering is it because i was working in page_load function so controls are not loaded until after page_control event. however i was able to find them with findcontrol function of formview objective.
Controls inside of templates (such as in your FormView, or in a GridView) are not directly accessible in the code behind. You must use FindControl to get access to those controls.
If the controls are declared in the aspx then they're defined in partial class equivalent for your Page class.
This was introduced along with .Net v2.0 so that messing with the designer wouldn't screw up with your code behind file (which caused quite a few problems in some cases).
You can access the controls from your Page Load event. Sometimes IntelliSense plays tricks on you and doesn't suggest the control. Just type it in. It will work. You can close the aspx page and open it again. Sometimes that fixes it. Or just restart Visual Studio if you're annoyed by it.
However, there are a few considerations if you are interested in accessing control data at certain times during the life cycle of the page.
Server controls have their own life cycle that is similar to the Page life cycle, but the order in which the event is triggered for the controls is as follows:
Init and Unload event for a control occurs before the event is raised for the container (bottom-up).
Load event for a control occurs after the event is raised for the container (top-down).
You can find a more detailed explanation of the Page life cycle events on MSDN.
It's hard to tell what exactly the problem is; it would help if you could post some code here.
I do have two guesses/suggestions:
If you have the problem that brentkeller is describing, what usually fixes this completely for me is deleting the aspx.designer.cs file, then right-clicking on the .aspx file and select "Convert to Web Application". This re-creates the designer file.
The control is inside a template like Jason Berkan suggested. If it's in a LoginView, for example, you would use .FindControl("controlId") on the LoginView.
The controls would be part of a partial class in the same solution. Just find all references for your class name.
I occasionally have trouble with adding a control to a page and the Intellisense not recognizing the control. The compiler also seems to not recognize the control and prevents the project from being compiled. It can be very frustrating and I really haven't figured out why.
Sometimes it helps to close the aspx page and its code file, sometimes closing Visual Studio and re-opening it works. Sometimes none of it works and I just try another way to get things done.
I don't know if this is what you're experiencing, but if so, it can definitely make you scratch your head and wonder what is going on.
I have a parentUserControl that loads a childUserControl. Multiple instances (say 3) of parentUserControl is added to the host page.
On PageLoad of the parentUserControl, I register JS:
ScriptManager.RegisterStartupScript(this, typeof(Page), keyJSLoadBegin, "OnParentLoad();", true);
On PageLoad of hte childUserControl, I register JS:
ScriptManager.RegisterStartupScript(this, typeof(Page), keyJSLoadBegin, "OnChildLoad();", true);
I get a JS error when I navigate to the page. The line that causes this error is (in View Source):
`OnParentLoad('parentA');OnChildLoad('childA');OnParentLoad('parentB');
OnParentLoad('parentC')`;OnChildLoad('childC');OnChildLoad('childB')
Clearly, the calls are out of order.
Anyone knows why?
EDIT:
The problem seems to be related to the bug here:
http://support.microsoft.com/kb/817032
The suggestion is to register all related scripts at once. This is impossible in my situation as the scripts are in different user controls.
Has anyone come across a solution to this issue?
The Control Model in ASP.NET intends for your code to be fully encapsulated. Each control should operate independently of the others and not depend on the load order (ideally not even depend on presence). You should rethink your approach.
I don't understand why this happens but here is my opinion :
I think you should expose child control's scripts by a method. In parent control concatenate them in which order you want, then register your page at one place.