web user controls have made my default page so heavy - asp.net

my web site structure is like this :
1- master page with stylesheet and scripts
2- aspx which render (number 3)
3- web user control and scripts inside them
in the number 3 I have some javascript that are call some ajax method they are about 20 lines code and I can not move them to js file in order to using <%= myAspControl.ClientID %> for some purpose
my default.aspx is so heavily now I checked it by google chrome developer tools it is equal to 2.7MB for document only document not images or style sheet.
I have about ten controls but I think it is effect on viewstate and encoding them it creates all in default.aspx dynamicly and make it so heavy.

Have you read these SO-suggestions to minimize ViewState?

That seems like poor ViewState use. I saw it in a project with some Telerik controls, which had a feature that stored the whole data source in the ViewState / which included more than was being displayed.
Keep the ViewState to what's absolutely necessary. Don't try any clever workaround, you should never store large amount of info in the view state.

Related

Is it a good idea to use plain HTML instead of ASPX

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

AjaxToolkit AsyncFileUpload needs ViewStateMode enabled, how to avoid?

I know this is rather a trivial question but anyhow I want to know if it is solvable.
I have an ASP.NET webform page with a nested masterpage and JavaScript includes. I make extensive use of jQuery and Ajax calls. Once a page is loaded, all communication with the server is done with Ajax calls, so I don't need ViewState enabled, and this is resulting in a reduced HTML output code.
I now have to implement a page where a user can upload a file. So I used the AsyncFileUpload control of AjaxToolkitFileUpload. Once a file is uploaded, I do a Ajax call to the server to request the filename.
It all works fine but I noticed that FireBug found 10 errors per file upload between the OnClientUploadStarted and the OnClientUploadComplete events. The file is uploaded properly and the events on the client side are working too. But I can't ignore the fact that there are errors thrown so I went looking for the problem.
I rebuild the page piece by piece and I found out that this control needs the Viewstate enabled.
However with viewstate=disabled in the page directive the file size is 76.6kb, and with viewstate=enabled the size is 99.2kb.
Again, I know it is trivial and will not affect performance on the site, but it is a nice to know.
- Why does this control needs the viewstate? (I suppose because it actually post the file to a iFrame or something like that?)
- How can I reduce the viewstate and let this control still work properly?
For reducing view state and having this one control work properly, you can use leave view state enabled on your page and set the ViewStateMode to Disabled for the page and then set the ViewStateMode to Enabled for the one control that needs view state. For all other controls they should inherit the setting of the parent by default.

Architectural decisions about popups in web (.NET Vision)

I've always wanted to know what is, in a general way, the opinions about popups in web (I mean, those who are implemented via divs).
I've always liked not to load the user with the entire size of this popup in his navegation (when the popup is not visible). I assume that it's better load the content by demand (when the user clicks in the corresponding button). If you have five popups in one page, I always thought that the increase in 'bytes' can make a difference downloading the page.
Following the 'on demand' option I've always liked iframes because they let me change his URL via Javascript. So, I display a popup (div) which contains an iframe in wich I can change his contents downloading the page in this moment.
In my probably limited view, this method has another advantage. The validation logic (usually Asp.NET validators) are isolated in the popup page, so they don't enter in any kind of conflict with the validators located in the parent page (if applicable).
But it seems that iframes are not so well supported by some browsers and they are not too much appreciated by the community of designers (and it's a object with strong security implications).
So Basically I was wondering what are your experiences displaying these kind of UI. I know Jquery can load dynamically HTML in one div, but probably without isolating client validation scripting.
Opinions? THANKS a lot!
Firstly, you can create validation groups (http://msdn.microsoft.com/en-us/library/ms227424.aspx). That will help you with your validation problems.
You're right, you can use jQuery to dynamically load HTML as appropriate, but I'm not sure how well that works with aspx pages. There are problem a number of gotchas. Consider, you have page1.aspx and popup.aspx. If you load popup.aspx in an iFrame, you're fine, because it's a separate page. If you load it dynamically via JQuery.load() - the output of popup.aspx will load into your page1.aspx (this includes html tags, form tags, viewstate fields etc). That will likely cause some problems. (I haven't tried just guessing).
I have used .load in the past, but I tend to load standard html pages, not aspx pages. Then when the "submit" button is pressed, it calls a webservice with the relevent fields. This adds more javascript coding on my part - coding the "submit" button, coding a webservice to handle the ajax submit, coding the "wait screen" while an action is being done or data being submitted via ajax. I also have write the js to do client side validation and any code to handle server side validation and report that back to the user.
jQuery Validation plugins work well for this - or alternatively, you can instantiate .net validators if you don't want more plugins / frameworks (http://msdn.microsoft.com/en-us/library/yb52a4x0.aspx)

ASP.NET 2.0 -how do I include files containing server-side code?

I want to be able to load a customized log in page depending on a couple of parameters passed into the querystring.
Each customized login page needs to be able to dynamically display log in errors and possibly have other variables passed in.
Let's say the dynamic login page looks like this (over-simplification here):
<form>
<% if (has_errors) { Response.Write(error_msg); } %>
<input type="text" name="email">
</form>
If the aspx page loads the file like this:
Response.writefile("path/to/custom/page");
the code shows up in the output and doesn't get processed. I have tried other ways to load the file contents (something similar to classic ASP includes) but get the same results every time.
I could have all the custom pages set up as user controls, but I need 100% control over the css, js, and html - and the documentation I read here indicates that I won't have that level of granularity.
link text
PLUS - I'm stuck in a .net 2.0 environment - so .NET MVC is not available to me
Any help/suggestions?
but I need 100% control over the css,
js, and html
You won't get 100% over the page but you will have control inside the User Control instance. Also, many times, you can override these technologies like CSS, from within your control.
In the end because all controls are solified into one big HTML page you will have the same level of control as you would in any single web page with client-side technologies.
You can build a Web UserControl to represent log/in and then include an instance of that control onto any page, in any place, across multiple pages if you wish.
(See the Topics on that MSDN help page about how to create and use it).
Other useful references (these are various angles on the same subject).
Creating a Web user Control in .NET
ASP 101 - User Controls
This should provide a good start to keep looking, if this is the kind of info you think you need.
Internals
The User Control can have its own logic, access the browser querystring, access the page Session, Application, etc. pretty much anything it needs to know for itself to work.
Object Oriented
Additionally, because a User Control is also an object, you can add your own public methods and properties to it through which you can interact to communicate with the control intance on the page (just like you interact with other web controls like Button.Text="click", TextBox.BackColor = System.Drawing.Color.Blue, etc).
Other Options - Dynamic control loading
You might want to consider loading controls dynamically at runtime using the Page.LoadControl(..) method:
Loads a Control object from a file
based on a specified virtual path.
MyControl myControl1 = (MyControl)LoadControl("TempControl_Samples1.cs.ascx");
PlaceHolder1.Controls.Add(myControl1);

Add a usercontrol as the value of a html attribute

I am working in a CMS where we use tokens ( which is turned into a user control. Is there a way to add the user control into an attribute value for our template style?
example :
<div class="<$tokenName/$>" />
this currently outputs an encoded user control, which is then not parsed by IIS.
Short answer: this is not possible.
Longer answer...
It's not IIS's job to parse the control... that happens when IIS hands off the request to the ASP.NET engine. ASP.NET does a single-pass parse through your ASPX before the Page lifecycle even starts... this is why controls you delcare in the ASPX are available during the Init event. Whenever your CMS expands "$tokenName", you are far past the point at which ASP.NET is interpreting your markup.
If you're having trouble with that, here's a thought experiment for you: What happens when $token expands into a user control that has some other $token2 control embedded in it? And that control contains some other $token3? How many times are you going to try and parse/expand/interpret your markup?

Resources