SoFunction
Updated on 2025-04-12

Top 10 Tips in Programming Page 2/2


5. Only use server-side controls when necessary

China New introduced a control called Web Server Controls running on the server side. In the code, they are often explained by the following syntax:

<asp:TextBox  size="40" runat="server" /> 
They are sometimes called ASP controls. The server-side control is indicated by the runat attribute, and its value is always "server".
By adding the runat attribute, general HTML controls can be easily converted to the server side to run. Here is a simple example:

<input type="text"  size="40" runat="server" /> 
By using the name specified in the id attribute, we can refer to the controls in the program, and we can set properties and obtain values ​​programmatically. Therefore, the server-side processing method has greater flexibility.

This flexibility comes at a price. Each server-side control consumes resources on the server. In addition, unless the control, web page, or application explicitly prohibits view state, the state of the control is contained in the hidden domain of view state and is passed in every loopback, which can cause serious performance degradation.
A good example in this regard is the application of control tables on web pages, using HTML tables that do not require server-side processing if they do not need to reference elements in the table in the code. We can still place server controls in HTML table cells and reference server controls in our code. If you need to reference any table element, such as the specified cell, the entire table must be a server control.


6. The difference between HyperLink control and LinkButton control

For web visitors, HyperLink and LinkButton controls are the same, but they still have big differences in functionality.
When the user clicks on the control, the HyperLink control immediately "navigates" the user to the target URL, and the tableware will not be returned to the server. The LinkButton control first sends the table file back to the server and then navigates the user to the target URL. If server-side processing is required before "reaching" the target URL, use the LinkButton control; if server-side processing is not required, use the HyperLink control.


7. Comment code

This technique is not targeted, but it is a good programming habit.
Comments should not only indicate what operations the code will perform, but also indicate the reason. For example, do not just state in the comments that it is traversing the array, but rather that it is traversing the array to calculate a value based on an algorithm. Unless the algorithm is quite simple, the algorithm should be briefly explained.
Different programming languages ​​in .NET engineering have different annotation symbols. Here is a brief description:
HTML <!--
JavaScript // Comments
VBScript ' Comment
' Comment
C#// Comment
/* Multiple lines of content
Comments
*/ 
SQL -  Comment

There are no comment symbols in the start and end tags of the server control, but the server is able to ignore all attributes that it cannot recognize, so we can insert comments by using undefined attributes. Here is an example:

<asp:TextBox 
 
size="40" 
comment="This is my comment"
runat="server" /> 
Commenting the source code in Visual Studio .NET is very simple. Highlight the lines that need to be commented, and then press Ctrl+K+C to add comments. To delete a comment, just highlight the commented code and press Ctrl+K+U key combination.
In C# projects, we can also enter the XML comment section by using /// at the beginning of each line. In the Comments section, we can organize the comments using the following XML tags:
<summary></summary> 
<remarks></remarks > 
<param></param> 
<returns></returns> 
<newpara></newpara> 
To view formatted reports of these XML comments in Visual Studio .NET, 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 attribute to record the execution of the web page in the Page directory.

An ancient technique for debugging programs is to insert output statements at the key points in the program. Usually, the output information will contain the values ​​of important variables, and the relevant information can be output to the screen, log file or database.
In this way, by using the trace property in the Page command, the use of this debugging technique is easier. The Page command is a line of code at the beginning of the ASPX file, which provides the compiler's instructions. The Page command contains one or more properties, providing the compiler with information such as the programming language used, the location of the code support file, or the name of the class to be inherited.
One of the attributes in the Page command is trace, whose value may be true or false. Here is a typical Page command, where the value of the trace attribute is true:

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

If the value of the trace attribute is set to true, the web page generated by the ASPX file will be displayed, and a large amount of other information about the page will be displayed in addition to the web page itself. These information are displayed in a table in the following subsections:
·Request details Provide Session ID, request time and request status code.
·Trace Information contains a list of tracking logs and steps in the web page life cycle in chronological order. In addition, custom information can also be added to it.
·Control Tree Lists all controls on a web page in a hierarchical manner, including the size of each control in bytes.
·Cookies Collection Lists all cookies created by this page.
·Head collection HTTP headers and their values.
·Server variables Server environment variables related to this web page.

The trace log included in the Trace Information section is the most useful, where we can insert our own trace commands. There are 2 methods in the trace class that can insert commands into the trace log: and, except that the commands are displayed in red fonts and the commands are displayed in black fonts, they are the same. Below is a screenshot of the tracking log containing several commands.

The most convenient feature in tracking logs is that we can insert and statements throughout the code during development and testing, and when the application is finally delivered, we can disable these commands from working by changing the value of the trace attribute in the Page command without removing these output statements before deploying the application.


9. Use 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 is submitted to SQL Server. SQL Server will parse the command, then create a query plan and optimize it, and then execute the query plan. This takes a lot of time.
Stored procedures are a series of SQL commands pre-parsed and optimized by the query processor. These commands will be stored and can be executed quickly. Stored procedures, also known as sprocs, can receive input parameters, allowing a single stored procedure to handle a wide range of specific queries.
Because sprocs are pre-parsed and are more important for complex queries, and their query plans are pre-optimized, so calling the query process is much faster than SQL statements that perform the same function.


10. Use the .NET command line

The .NET command line tool runs in the command prompt window. In order for the command to be executed, it must reside in the current directory of the command prompt, or by setting the PATH environment variable.
.NET SDK installs a menu item on the "Startup" menu, which can open a command prompt window with the PATH environment variable correctly set. We can start the command prompt window by clicking "Start" -> "Programs" -> "Microsoft Visual Studio .NET" -> "Visual Studio .NET Tools" -> "Visual Studio .NET Command Prompt".
By dragging this menu item from the menu to the desktop and pressing Ctrl+C at the same time, you can copy the shortcut of the menu item to the desktop, which will be very convenient to use.



Previous page12Read the full text