Asp. NET application Design 10 tips (ii)

Source: Internet
Author: User
Tags command line comments contains insert session id net command net command line visual studio
5. Use server-side controls only when necessary

Asp. NET, a control called Web server controls that runs on the server side is introduced, and in code, they are often illustrated by the following syntax:

They are also sometimes referred to as ASP controls. The server-side control is indicated by the Runat property, and its value is always "server".
By adding the Runat property, a generic HTML control can be easily converted to a server-side run, and here's a simple example:

By using the name specified in the id attribute, we can reference the controls in the program and programmatically set the properties and get the values, so the server-side approach has greater flexibility.

There is a certain cost to this flexibility. Each server-side control consumes resources on the server. In addition, unless the control, Web page, or application explicitly prohibits view state, the control's status is contained in the hidden field of view, and is passed in each loopback, causing severe performance degradation.

A good example of this is the application of a control table on a Web page that uses HTML tables that do not require server-side processing if you do not need to refer to elements in the table in your code. We can still place the server control in the HTML table cell and reference the server control in code. If you need to refer to any table element, such as a specified cell, the entire table must be a server control.

6. Differences between hyperlink controls, LinkButton controls

HyperLink, LinkButton controls are the same for web visitors, but they still differ significantly in functionality.
When a user clicks on a control, the hyperlink control immediately "navigates" the user to the destination URL, and the table is not echoed to the server. The LinkButton control first sends the table back to the server and then navigates the user to the target URL. Use the LinkButton control if server-side processing is required before reaching the target URL, or you can use the hyperlink control if you do not need server-side processing.

7. Annotation Code

This technique is not aimed at asp.net, but it is a good programming habit.

Comments should not only explain what the code does, but should also indicate why. For example, instead of just stating in a comment that you are traversing an array, you should explain that the traversal array calculates a value based on an algorithm, unless the algorithm is fairly simple, otherwise the algorithm should be briefly described.

. NET project different programming languages have their own different annotation symbols, the following is a brief description:


HTML!--Annotation-->
JavaScript//Notes
VBScript ' Notes
VB.net ' note
C #//Notes
/* Multiline Content
The annotation
*/
SQL--Comments

There is no annotation symbol in the start and end tag of the server control, but the server can ignore all properties it does not recognize, so we can insert the annotation by using a property that is not defined. Here is an example:

In Visual Studio. NET is very simple to annotate the source code. Highlight the line you want to comment on, and then press Ctrl+k+c to add a comment. To remove a comment, simply display the annotated code with a high brightness and press the Ctrl+k+u key combination.

In C # Engineering, we can also enter XML annotation bars by using///at the beginning of each line. In the annotation section, we can use the following XML markup to organize comments:






To be in visual Studio. NET to view the formatted report of these XML annotations, we can first select the Tools menu item and then select the Create Comment Web page menu item.

8. Use the trace method and trace property to record the execution of pages in the page directory

One of the oldest techniques for debugging is inserting output statements at a key point in the program, where the output information contains the value of the important variable, and the information can be exported to the screen, log file, or database.

In asp.net, this debugging technique is easier to use by using the Trace attribute in the page command. The page command is a line of code at the beginning of the ASPX file, which provides instructions for the compiler. The page command contains one or more properties that provide the compiler with information such as the programming language used, the location of the code support file, or the name of the class to inherit.

One of the properties in the page command is trace, whose value may be true or false, and the following is a typical page command where the value of the Trace property is true:

<%@ Page language= "C #" trace= "true"%>

If the value of the Trace property is set to True, the Web page generated by the ASPX file is displayed, and a large amount of additional information about the page is displayed in addition to the page itself. This information is displayed in a table in the following sections:

· The request details provide the session ID, the requested time, and the status code of the request.
· Trace information contains a list of trace logs, chronological steps in the Web page life cycle. Alternatively, you can add custom information to it.
• The control tree lists all the controls on a Web page in a hierarchical fashion, including the size of each control in bytes.
· The Cookies collection lists all cookies created by the Web page.
• Head collection HTTP headers and their values.
· The server environment variable that the server variable is associated with for this page.

The trace logs contained in the trace Information section are most useful, where we can insert our own tracking commands. There are 2 methods in the Trace class that can insert commands in the trace log: Trace.Write and Trace.Warn, except that the Trace.Warn command is displayed in red font and the Trace.Write command is displayed in black font. Below is a screenshot of the trace log, which contains several Trace.Warn commands.

The most convenient feature in the trace log is that we can insert trace.write and Trace.Warn statements throughout the code during development and testing, and when the application is finally delivered, you can prevent these commands from working by changing the value of the trace property in the page command, without having to remove the output language before deploying the application software Sentence

9. Using Stored Procedures

Microsoft's SQL Server and other modern relational databases use SQL commands to define and process queries. A SQL statement or a series of SQL statements submitted to SQL Server,sql Server resolves the command, then creates a query plan and optimizes it, and then executes the query plan, which takes a lot of time.

A stored procedure is a series of SQL commands that are resolved and optimized by a query processor that is stored and executed quickly. Stored procedures are also called SPROCs, which can receive input parameters so that a single stored procedure can handle a larger range of specific queries.

Because sprocs are resolved in advance and are more important for complex queries, their query plans are optimized, so invoking the query process is much faster than SQL statements that perform the same functions.

10, use. NET command line

. NET command-line tool runs in a command prompt window. In order for the command to execute, it must reside in the current directory at the command prompt, or by setting the PATH environment variable.

The. NET SDK installs a menu item on the Startup menu that opens a command prompt window that correctly sets the PATH environment variable. We can click "Start"-> "program"-> "Microsoft Visual Studio. NET"-> Visual Studio. NET tool "->" Visual Studio. NET command prompt, and start a command prompts window.

By pressing CTRL + C while you drag the menu item from the menus to the desktop, you can copy the menu item's shortcut to the desktop, which is convenient to use.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.