Top 10 skills in ASP. NET Programming page 1/2

Source: Internet
Author: User

In this document, we will discuss how programmers develop applications using ASP. NET. Program 10 tips that need to be paid attention to. These skills involve changing the default control and form name to the use of the stringbuilder class, which helps programmers adapt to the. NET environment as soon as possible.

1. When using Visual Studio. NET, do not use the default name unless it is a direct or non-referenced object.

. one of the benefits of net is that all Source Code and configuration files are pure text files, you can use any text editor such as NotePad or WordPad for editing. If you do not want to, we do not have to use Visual Studio. NET as the integrated development environment. However, with Visual Studio. NET, we can see the file in Windows File Manager or browse the file content from a text editor outside Visual Studio. NET.
using Visual Studio. NET as an integrated development environment has many advantages. The most significant advantage is that it greatly improves production efficiency. With Visual Studio. NET, we can develop software faster at a low cost. As part of the integrated development environment, intelliisense provides automatic Code completion, dynamic help when entering methods or functions, and real-time prompts for syntax errors, and other functions that can improve production efficiency.
as with other complex tools, Visual Studio. net brings us a sense of frustration before learning how to give full play to its role and grasp its "habits. Sometimes, it is like a hard-to-understand black box, it will generate a large number of files and a lot of useless code.
one function of Visual Studio. NET is to provide a default name for a new object, whether it is an object in a class, control, or form. For example, if we create a new ASP. NET web application, the default name is webapplication1. In the "new project" dialog box, we can easily change the name of the application, but only the name of the application's namespace and its virtual directory are changed, the default names of source code files are still webform1.aspx and webform1.aspx. CS (C # project) or webform1.aspx. VB (VB.. Net project ).

We can change the file names used by aspx and code in the solution browser, but the webpage class name will still be webform1. If a button is generated on the web form, the default name is button1. In fact, the names of all controls are composed of the control type and number.
We can also change the names of all forms and controls in the application to meaningful names. For a small demo program, the default name is also competent, but if the application consists of multiple forms, each form contains many buttons and labels, form names such as frmstartup, frmdataentry, and frmreports are easier to understand and maintain than form1, form2, and form3.
If the form control needs to be referenced elsewhere in the code, it is more important to have a meaningful name. The names such as btnok, btncancel, and btnprint make it easier for code users to understand and maintain controls such as button1, button2, and button3.
A good way to modify a name that appears in all files in a project is in Visual Studio. in the. NET menu, select "edit"> "discover and replace"> "replace.
When looking at the code we wrote two weeks ago, we often see the code for the first time, so it is necessary to make them have a name that helps us understand its meaning.

2. Even if you do not use Visual Studio. NET for programming, using code to support files can also improve application performance.

Visual Studio. NET uses code support files in all ASP. NET web projects such as Web applications, Web Services, and Web controls. Code support files make the project better organized and coherent, and more suitable for development teams composed of multiple people. It also improves the performance.
The content of the Code support file is compiled into a class in the composite file, which is generally a DLL file and sometimes an EXE file. The file resides in the high-speed buffer of the application assembly, and can be obtained immediately when the application starts.
If the code is included in the <SCRIPT> mark or the aspx file code, it will still be compiled into a webpage class. In this case, every time the web page is loaded for the first time in the application conversation, it needs to be re-compiled, and the compiled class will reside in the memory. This file must be re-compiled whenever the computer is started, IIS is stopped, restarted, or the source code or configuration file is changed. Although not big, this causes considerable performance loss.

3. Minimize form delivery

Every time you click the button, linkbutton, or imagebutton control on a web page, the form is sent to the server. If the autopostback attribute of the control is set to true, if the status of the control such as checkbox and checkboxlist is changed, the form will be sent back to the server.
Each time a form is sent back to the server, it is reloaded, The page_load event is started, and all code in the page_load event processing program is executed. It is most appropriate to put the webpage initialization code here. We often want to execute some code each time a webpage is loaded, but we want to execute other code only when the webpage is loaded for the first time, I even want some code to be executed each time except for the first loading.
You can use the ispostback feature to complete this function. When a webpage is loaded for the first time, the value of this attribute is false. If the webpage is reloaded due to sending back, the value of the ispostback attribute is set to true. Through testing, you can execute the Specified Code at any time. The following is the relevant C # code:
Protected void page_load (Object sender, eventargs E)
{
// Operations performed each time a webpage is loaded
If (! Ispostback)
{
// The operation performed when the page is loaded for the first time
}
Else
{
// The operation performed during the delivery.
}

// Operations performed when a webpage is loaded
}
We hope to avoid sending back as much as possible (the server is required to perform a series of operations each time), even if it causes sending back. We also hope to perform as few operations as possible. Large-scale and time-wasting operations (such as database searches) should be avoided in particular because they can prolong the application response time.

4. Use the stringbuilder class

The string is immutable In the. NET Framework, which means that the operators and methods for changing the string will return the modified copy of the string, which means there is still room for improvement in performance. When performing a large number of string operations, using the stringbuilder class is a good choice.
The following C # code tests the time required to generate a string from the 10000 substring in two ways. A simple String concatenation operation was used for the first time, and the stringbuilder class was used for the second time. To view the result string, remove the annotation symbol of the annotation line in the following code:

<% @ Page Language = "C #" %>

<SCRIPT runat = "server">
Void page_load (Object source, eventargs E)
{
Int int intlimit = 10000;
Datetime starttime;
Datetime endtime;
Timespan elapsedtime;
String strsub;
String strwhole = "";

// First perform the string connection operation
Starttime = datetime. now;
For (INT I = 0; I <intlimit; I ++)
{
Strsub = I. tostring ();
Strwhole = strwhole + "" + strsub;
}
Endtime = datetime. now;
Elapsedtime = endtime-starttime;
Lblconcat. Text = elapsedtime. tostring ();
// Lblconcatstring. Text = strwhole;

// Use the stringbuilder class for the same operation
Starttime = datetime. now;
Stringbuilder sb = new stringbuilder ();
For (INT I = 0; I <intlimit; I ++)
{
Strsub = I. tostring ();
SB. append ("");
SB. append (strsub );
}
Endtime = datetime. now;
Elapsedtime = endtime-starttime;
Lblbuild. Text = elapsedtime. tostring ();
// Lblbuildstring. Text = sb. tostring ();
}

</SCRIPT>

<HTML>
<Body>
<Form runat = "server">

<H1> String concatenation benchmark

Concatenation:
<Asp: Label
Id = "lblconcat"
Runat = "server"/>

<Br/>

<Asp: Label
Id = "lblconcatstring"
Runat = "server"/>

<Br/>
<Br/>

Stringbuilder:
<Asp: Label
Id = "lblbuild"
Runat = "server"/>

<Br/>

<Asp: Label
Id = "lblbuildstring"
Runat = "server"/>

</Form>
</Body>
</Html>
The difference between the two methods is quite large: The append method using the stringbuilder class is nearly 200 times faster than the string connection. The comparison result is as follows:
(Figure: picture01)

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.