21. Only save the Server Control view status when necessary
Automatic view status management is a function of the server control, which enables the Server Control to re-fill their attribute values during the round-trip process (you do not need to write any Code ). However, because the view status of the server control is redirected to and from the server in the hidden form field, this function does have an impact on performance. You should know under which circumstances the view status will be helpful and under which circumstances it will affect the page performance. For example, if you bind a server control to data during each round-trip process, the new value obtained from the data binding operation replaces the saved view status. In this case, disabling the view status can save processing time.
By default, view status is enabled for all server controls. To disable view status, set the enableviewstate attribute of the control to false, as shown in the following DataGrid Server Control example.
The following is a reference clip:
You can also use the @ page command to disable the view status of the entire page. This is useful when you do not send the page back to the server:
The following is a reference clip:
Note that the @ control command also supports the enableviewstate attribute, which allows you to control whether the view State is enabled for the user control.
To analyze the number of view States used by the server control on the page, please (include the trace = "true" attribute in the @ page command) enable tracing on this page and view the viewstate column of the control hierarchy table. For more information about tracing and how to enable it, see ASP. NET tracing.
22. Avoid unnecessary round-trip to the server
Although you may want to use the time-saving and code-saving functions of the web forms page framework as much as possible, ASP. NET Server controls and sending back events cannot be used in some cases.
Generally, you only need to start the round-trip process to the server when retrieving or storing data. Most data operations can be performed on clients during the round-trip process. For example, it is often possible to verify user input from an HTML form on the client before the data is submitted to the server. Generally, if you do not need to pass the information to the server to store it in the database, you should not write the code that leads to the round-trip process.
If you develop custom server controls, consider making them present client code for browsers that support ecmascript. By using the server control in this way, you can significantly reduce the number of times information is not necessary to be sent to the web server.
Use Page. ispostback to avoid unnecessary operations on the round-trip Process
If you write the code for processing the Server Control sending and processing, you may sometimes need to execute other code on the first request page, instead of the Code executed when the user sends an HTML form contained in the page. Use the page. ispostback attribute to execute code conditionally based on whether the page is generated in response to the server control event. For example, the following code demonstrates how to create a database connection and command. This command binds data to the DataGrid server control when you request this page for the first time.
The following is a reference clip:
Void page_load (Object sender, eventargs E)
{
// Set up a connection and command here.
If (! Page. ispostback)
{
String query = "select * from authors where firstname like '% Justin % '";
Mycommand. Fill (DS, "Authors ");
Mydatagrid. databind ();
}
}
Because the page_load event is executed during each request, the code above checks whether the ispostback attribute is set to false. If yes, the code is executed. If this attribute is set to true, no code is executed.
Note that if you do not run this check, the page sending behavior will not be changed. The code of the page_load event is executed before the Server Control event is executed, but only the results of the server control event can be displayed on the output page. If you do not run this check, the page_load event and any server control event on the page will still be processed.
23. Disable it when the session status is not used
Not all applicationsProgram Or the page must be specific to the user's session status. You should disable the session status for any application or page that does not need the session status.
To disable the page session Status, set the enablesessionstate attribute in the @ page command to false. For example:
The following is a reference clip:
Note: If the page needs to access session variables without creating or modifying them, set the enablesessionstate attribute in the @ page command to readonly.
You can also disable the session Status of the XML Web Services method. For more information, see XML Web Services created using ASP. NET and XML Web Services clients.
To disable the session Status of an application, set the mode attribute to off in the sessionstate configuration section of the Application Web. config file. For example:
The following is a reference clip:
24. Carefully select the session Status provider
ASP. NET provides three different methods for storing application session data: in-process session Status, out-of-process session status as a Windows service, and out-of-process session Status in the SQL Server database. Each method has its own advantages, but the in-process session status is the fastest solution so far. If you only store a small amount of data that is prone to loss in the session state, we recommend that you use in-process providers. Out-of-process solutions are mainly used to scale applications across multiple processors or computers, or to prevent data loss during server or process restart. For more information, see ASP. NET status management.
25. Unnecessary Server Control
In Asp.net, a large number of server-side controls facilitate program development, but may also lead to performance loss, because each time a user operates a server-side control, a round-trip process with the server is generated. Therefore, server control should be used less unless necessary.
26. ASP. NET application performance test
Before performing a performance test on ASP. NET applications, ensure that the application is correctly functioning and has no errors. The following tools can be used for specific performance tests:
Web application strees tool (was) is a free test tool released by Microsoft. It can be downloaded from http://webtool.rte.microsoft.com. It can simulate hundreds of thousands of users simultaneously to access web applications, form a traffic load on the server, so as to achieve the purpose of testing, can generate average ttfb, average TTLB and other performance summary reports.
Application Center Test (ACT) is a testing tool that is attached to the Enterprise Edition of Visual Studio. NET and is a Web application testing tool officially supported by Microsoft. It can intuitively generate chart results with more functions than was, but does not have the ability to be tested simultaneously by multiple clients.
The "performance" counter in the "Administrative Tools" of the server operating system can monitor the server to understand the application performance. [End]
For website developers, compile ASP. net Applications, develop good habits, improve application performance, at least can delay the necessary hardware upgrade, reduce the cost of the website.