Why is my ASP.NET Web forms rendering slow? - c#

I have some complex ASP.NET Web Forms website. Tracing showed this:
So basically, all my database calls and other complex, time-consuming things often take less time then ASP.NET Web Form render method. How to make it faster?
Rendering often takes around 200-300ms or even more. That is already above Google's reccomendation for time for first byte
Is it because it is not small? But it is around 40kb (size of rendered website).
I if was database query that is slow I would know what to do it? But here I don't know how to make faster, apparanetly, internal ASP.NET Web forms method?

First, if you have codes in the Page_Load event, u might have to look into the codes, not remove it, but look for another way to write them.
Second, check all controls on the form, remove idle controls if any, or look for alternate controls to replace some of the ones you used

Related

Dynamic Web Controls when using Pure Razor

I want to make a dynamic control on a web page that does an action and generates new HTML code without needing to refresh the page (what would traditionally be handled by JavaScript, I presume). However, my project is currently comfortably and neatly nestled in as a pure ASP.NET Web Pages project strictly using Razor, no MVC at all.
I've hunted everywhere and haven't gotten a clear answer; what answers I have found always concern deep MVC or Web Forms components. As I understand, it should be fairly easy to make use of bits and pieces of those, but I'm still fairly inexperienced with ASP.NET.
What I want to make in short: A button that
Is an image
When clicked, increments a record value (already stored in the Razor code, simple to push to the database), and replaces itself with another image button
This button has a different image, will decrement the record value and replace itself with the original button (they are effectively the inverse of each other)
This is the most complex control I need at the moment for my project, and learning about this will hopefully provide a basis from which to engineer different controls.
The underlying technology of the server is irrelevant, whether it's PHP, ASP.NET, Ruby, et cetera - if you want a web page to "do something", you need to send a request to the server. If you want it to do something without the user directly submitting a form or clicking on a link and changing their location, you have to send the request to your server with javascript. Javascript is how you get the client to do things. The razor templating system operates on the server, not on the client.
Specifically, you should look in to Javascript AJAX requests; I'd particularly recommend learning about jQuery, because it simplifies a lot of aspects of Javascript.

Variables in ASP.NET

I recently switched to ASP.NET Web Forms from Windows Forms and I have an issue with something I would not expect. In good Web Forms I could create a field inside my Form class and assign to it. So if I wanted one control to raise a flag on one event (say user button click) I could declare Boolean and assign to it from my form methods. Then I could check the state of the flag from different methods on different events.
It looks like it's not so much in Web Forms. The value of my fields (or global variables if you will) remains unchanged from the moment I initialized them.
This is probably simple thing but for me it's quite a frustrating problem. I could store my vaues in session but I don't think that it would be a right way to do it. The problem is I need to store a lot of variables since I write code for SQL interface applications. Never had a single problem in Windows Forms and in Web Forms I keep pulling my hair.
Ultimately I need to store objects to reuse them like LINQ to SQL classes objects so I would really appreciate some guidance.
I suggest you review PostBacks, Page life-cycle and how ASP.NET processes client requests . In order to better understand ASP.NET forms, you should take time to read up on the page life-cycle, events and the order in which they are fired. That way you understand why it seems your variables are not being updated.
you should also read about State management like viewstate , session ect...
put your page_load contents that you don't want to execute after button click to the following condition:
if (!IsPostBack)
{
// do something
return;
}

Silverlight - Dynamically creating controls without any postbacks

Can I dynamically create controls in Silverlight without a postback to the server (even an asynchronous one). Does silverlight drag-n-drop requires postback?
I'm asking this because I've an asp.net application where I dynamically create/delete lots of controls. So after the postback I'm getting error with view state stating that the control tree doesn't match the view state tree.
Can I avoid such problems in Silverlight?
everything done in a silverlight control/application happen on the client. web service calls if any happen asynchronously. thats on of the advantages of using silverlight
Yes you can add controls dynamically to pages, without a round-trip to the server.
Drag-drop is also executed client-side.
Think of Silverlight as more like a desktop app, that talks to the server only to get/save data.
Adding controls dynamically in Silverlight is as simple as newing the appropriate control class and inserting it in the render tree (e.g. by adding it to a parent control).
Here is an example: http://asd.murven.com/blog/post/2009/10/16/Silverlight-Adding-controls-dynamically.aspx.
However, I would not suggest switching to Silverlight just to kill this bug. Only if you have a real need for a client-like application instead of a real web application. ASP.NET is suitable for dynamically creating controls as well. Please remember to initialize the control on the server during each postback. If this doesn't help, I would suggest you to submit a description of your problem with some code to help us solve it with you.
Br. Morten
The vast majority of what goes on in Silverlight involves no postback. In fact, I'd say Silverlight represents a completely different mindset. Whenever there IS a postback from Silverlight, it will almost always be asynchronous, and there is no "view state" that the server needs to worry about. In my opinion, it makes ASP.NET look like a joke when it comes to writing web applications.

Asychnronous Message Send Asp.net

I'm trying to learn about how AJAX stuff works under the hood. Say I have an asp.net form with a button on it. I then have a database with a single column DateTime. When I click the button on the asp.net form I want to insert a column in the database with the current time.
I'll have a C# event handler:
protected void btnButton_OnClick(Eventargs e, object sender)
{
//insert DateTime.now into DB
//This part is easy
}
What I would like to happen on the form itself is to have the button click NOT reload the form. It would just send a message to the server; there is no reason for the page to reload. What are some ways I could accomplish this?
I've looked into the AJAX framework a little bit and it seems like this could be done within an update panel, but the page would still reload, it would just be less noticable to the user.
Use the __doPostBack call in javascript. I have no idea how this works.
It sounds like you're partial to Microsoft's provided javascript libraries, which is fine. The UpdatePanel and the like have their place, but I've found that when you need to do something very simple and straightforward like what you're describing it is easier and cleaner to get it done with a direct call to a server resource from Javascript.
There are many ways to do this, but I like using jquery's $.ajax() method to invoke webservice methods (or MVC actions more recently). If you're willing to examine some slightly different technologies here's a blog post that will give you a taste of what I'm talking about.
ASP.NET Ajax has 2 major parts:
server side controls based
client side framework (which is called Microsoft Ajax)
With the first you would opt to make use of an UpdatePanel control. This however still sends a lot of unneeded data over the wire like the full viewstate, your pages follow almost the full page life cycle like with a normal postback. Mostly I recommend against using that in production code.
The second part is an ajax library based on pure javascript with a touch of Microsoft sauce over it. It has similarities with the page life cycle like a pageLoad function and such and is quite easy to learn.
__doPostBack
That function gets inserted when you place certain server controls on your page like a LinkButton control. You can call that from javascript directly but know that it'll cause a full page postback to the server so if you want to avoid that don't go that path.
During the last 2 years however I've become a big fan of jQuery which works quite well together with ASP.NET and ASP.NET MVC.
I suggest that you read this fine article to get more information about it: Using jQuery with ASP.NET Part 2: Making an AJAX Callback to ASP.NET.
jQuery itself has also been adopted and favored by Microsoft. I strongly suggest you take a look at it and its power.

How can I load .ascx user control from console application or windows service?

We use ascx user controls as templates for documents (e.g. invoices).
Now I need to load,render to HTML, then transform to PDF these controls from windows service.
So, what's the correct way/workaround? TemplateControl.LoadControl(path) doesn't work.
To be perfectly honest, I don't know how to do this. However, if I was stuck with this problem, I think I'd download the ASP.NET MVC source, and see what they do to render a partial view (a partial view in MVC is essentially an .ascx control). The Render method on System.Web.Mvc.WebFormView might be a good starting place.
You're probably going about it the wrong way, to be honest. You should probably have your logic embedded in a library, and then have the user control interface with that library. Then, you could use that same logic to populate values on a form, or similar.
You're probably going to run into a lot of trouble trying to get the control to load, and deal with things that don't exist outside of ASP.Net, like session variables, and viewstate.
Generally, what you're asking for is a report, which is handled by something like Crystal Reports, or SQL Server Reporting Services.

Categories

Resources