Asp. The ten techniques in net programming 1th/2 page _ Practical Tips

Source: Internet
Author: User
In this document, we will discuss 10 techniques that programmers need to pay attention to when using ASP.net to develop applications that involve changes from the default controls, table Single-name to StringBuilder classes, and help programmers adapt as quickly as possible. NET environment.

1, in the use of visual Studio. NET, do not use the default name except for objects that are directly or not referenced.

. NET, one of the benefits is that all source code and configuration files are plain text files that can be edited using any text editor such as Notepad or WordPad. If not, we are not necessarily using visual Studio. NET as an integrated development environment. But with Visual Studio. NET, we can see files in Windows File Manager, or in Visual Studio. NET to browse the contents of a file from a text editor.
Use Visual Studio. NET as an integrated development environment has many benefits, the most notable of which is that it greatly improves productivity. Use Visual Studio. NET, we are able to develop the software faster with a small cost. IntelliSense, as part of the integrated development environment, provides automatic code completion, provides dynamic help when entering methods or functions, real-time hints of syntax errors, and other features that improve productivity.
Like other complex tools, Visual Studio before learning how to give full play to its role and grasp its "habits". NET also gives us a sense of frustration. Sometimes it's like a black box that's hard to understand, generating a lot of files and lots of useless code.
Visual Studio. NET is the ability to provide a default name for a new object, whether it is a class, a control, or an object in a form. For example, if we create a new ASP.net Web application, the default name will be WebApplication1. We can easily change the name of the application in the "New Project" dialog box, but at the same time change only the name of the application's namespace and its virtual directory, the source code file's default name is still WebForm1.aspx and WebForm1.aspx.cs (C # Engineering) or WebForm1.aspx.vb (vb.net engineering).

We can change the file name used by ASPX and code in the scenario browser, but the name of the Web page class will still be WebForm1. If a button is generated on the Web form, the default name will be Button1. In fact, the names of all controls are made up of the type and number of controls.
We can, and should, change the names of all the forms and controls in the application to meaningful names. The default name is also available for smaller demos, but if the application is composed of multiple forms, with many buttons and labels on each form, tables such as Frmstartup, Frmdataentry, and Frmreports are single-name more than Form1, Names like Form2 and Form3 are easier to understand and maintain.
If a control on a form is to be referenced elsewhere in the code, it is even more important to have a meaningful name. Names such as Btnok, Btncancel, and btnprint make it easier for people looking at the code to understand, and therefore easier to maintain than controls named Button1, Button2, and Button3.
A good way to modify a name that appears in all files in a project is in Visual Studio. NET menu, select Edit-> to find and replace-> the Replace command.
When we look at the code we wrote two weeks ago, it's often like we saw it for the first time, so it's necessary to have a name that helps us understand its meaning.


2, even if you do not use visual Studio. NET programming, using code support files also helps improve application performance

In all asp.net Web projects, such as Web applications, Web services, or Web controls, Visual Studio. NET uses code support files. The code support file makes the project better organized, modular, and more suitable for the development team composed of many people. In addition, it also brings performance improvements.
The contents of the code support file are compiled into a class in a composite file, typically a DLL file, or sometimes an EXE file. The file resides in the application's assembly cache, and can be immediately available when the application starts.
If the code is contained in the <script> tag or in the ASPX file code, it will still be compiled into a Web page class. In this case, each time the page is loaded for the first time in the application dialog, it needs to be compiled again, and the compiled class resides in memory. Each time the computer starts, IIS stops, restarts, or the source code, configuration file changes, the file must be recompiled. Although not large, the resulting loss of performance is considerable.


3. Minimize form Loopback

Whenever you click a button, LinkButton, or ImageButton control on a Web page, the form is sent to the server. If the control's AutoPostBack property is set to True, the form is sent back to the server if the state of the checkbox, CheckBoxList, and so on is changed.
Each time the form is sent back to the server, it is reloaded, the Page_Load event is started, and all the code in the Page_Load event handler is executed. It is most appropriate to put the initialization code of the Web page here. We often want to execute some code every time we load a Web page, but we want to execute some code when the page first loads, and even want some code to execute on every load except for the first load.
You can use the IsPostBack feature to do this. The value of this property is false the first time the page is loaded. If the page is reloaded by loopback, the value of the IsPostBack property is set to true. By testing, you can execute the specified code at any time. The following is the related C # code:
protected void Page_Load (Object sender, EventArgs e)
{
Some of the actions that are performed each time a Web page is loaded
if (! IsPostBack)
{
What to do when a Web page is loaded for the first time
}
Else
{
Actions to perform when loopback
}

What to do when a Web page is loaded each time
}
We would like to try not to cause loopback (each loopback will require the server to do a series of operations), even if caused by loopback. You also want to be able to perform as few operations as possible. Large-scale, waste-time operations, such as database lookups, should be especially avoided because they can prolong the response time of an application.


4, the use of StringBuilder class

String in the. NET Framework, which means that changing the operator and method of a string returns a changed copy of the string, which means there is room for improvement in performance. When doing a lot of string manipulation, using the StringBuilder class is a better choice.
The following C # code tests the time that is required to generate a string from 10,000 substrings in two different ways. A simple string concatenation operation was used for the first time; the StringBuilder class was used for the second time. To see the result string, you can remove the callout symbol for the callout line in the following code:

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

<script runat= "Server" >
void Page_Load (Object Source, EventArgs E)
{
int intlimit = 10000;
DateTime StartTime;
DateTime Endtime;
TimeSpan ElapsedTime;
String strsub;
String strwhole = "";

To perform a string concatenation operation first
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;

Doing the same thing with the StringBuilder class
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>

<body>
<form runat= "Server" >


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>
The difference in the two ways is quite large: the Append method using the StringBuilder class is nearly 200 times times faster than using string concatenation. The results of the comparison are as follows:
(Figure: PICTURE01)

Current 1/2 page 12 Next read the full text
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.