Introduction to Asp.net website optimization 2: Selection of server controls and client controls (HTML tags)

Source: Internet
Author: User

This article first reprinted http://blog.codepubs.com/archives/130 please indicate the source

I remember that during an interview, the interviewer asked me the following question: "Let's talk about your understanding of viewstate ."

My answer at the time was: "I rarely use viewstate, and I usually choose to disable viewstate"

I don't know if I am satisfied with the answer and whether I have any side effects on my interview results! Today we will talk about this viewstate.

 

I recently changed my work. After a week of reading documents and getting familiar with the system, I started to participate in coding. Although I am not familiar with my skills, I have always tried my best to do better in my previous work experience. I often visit technical blogs, such as csdn, cnblogs, and 51cto, OKArticleI usually add my favorites and read them carefully. I have something wrong with this person. I will keep it in mind when I see good things and practical things, And I will apply them and practice them. So my skills have been continuously improved over the past two years. So it is no exaggeration to say that maybe I amAlgorithmI'm an idiot, but in terms of product experience and basic performance, I have even surpassed some people who have been working longer than me. Of course, I have not met any project that requires advanced algorithms. This problem is actually determined by attitude.

A colleague once asked me: "You thinkProgramDevelopment knows how to use important, or knows why it is more important ." My answer is: "I don't want to engage in scientific research. I know how to use it if I can solve my work problems ". Yes, I did. I don't want to do research. I don't have to study any algorithms every day, because those algorithms are useless. It's like the physics and chemistry we learned at school. If you are engaged in software development after graduation, these will not be used at all (the sorrow of education ).

The previous paragraph is a bit different from the topic of this article. I said that I only know how to use it, but I will not go into the reason why it is used. However, it should be noted that this article is only intended for beginners, and the necessary optimization of the system. So .... This article is very subtle, and we can avoid performance issues.

 

It seems that I have run the question! Okay, I admit that my thinking is messy, and I admit that my writing skills are poor (haha). I suddenly remembered that some of my children's shoes said these days: "I want to go to school that year,CompositionI don't have to worry about writing tens of thousands of words. Now I am tired of writing a user manual (if I remember it wrong !"

 

The reason for writing this article is that the source self is working on a project. Currently, I do not discuss architecture and database design issues. The original intention of this article is to improve the performance of beginners. At least, it is the most basic improvement.

First, try to reduce the number of useless replies, See the figure:

For example, the hyperlink "edit" in a red box does not discuss whether the list uses the gridview or repeater.

The editing implementation is to generate a page connected to the edit. ASPX page and pass a parameter over. Let's take a look at the current implementation:

Aspx:

 
<IMGSRC= "../Images/edt.gif"Width= "16"Height= "16"Align= "Absmiddle"/>
<ASP:LinkbuttonID= "Lbtnedit"Runat= "Server"Commandargument='<%# Eval ("EDR ")%>'Commandname= "Edit">Edit</ASP:Linkbutton>

CS:

Protected voidRepemployeeinfo_userinfocommand (ObjectSource,RepeatercommandeventargsE ){If(E. commandname ="Edit"){StringId = E. commandargument. tostring (); response. Redirect ("Maid? Id ="+ Id );}

 

The above can be modified to write directly on the ASPX page as follows:

<AHref= 'Employeeinfoedit. aspx? Id =<%# Eval ("EDR ")%>'>Edit</A>

What are the advantages of writing like this:

This is the HTML a tag of the client. You know whether the ASPX page is parsed and finally presented to the client or the HTML Tag. Since it is all HTML tags, why should IIS parse it again? Don't you think it's cumbersome?

In IIS<ASP:LinkbuttonParse it into HTML a tag, and use commandname to parse the connection of a tag into href ......

I will not talk about deleting the above one. If it is me, I will still write a tag, delete it elsewhere, or delete it using Ajax. Of course, sometimes it is written directly on the page for convenience! This can be written in this way. After all, it is the advantage of the. NET control! This article first reprinted http://blog.codepubs.com/archives/130 please indicate the source

Second: Minimize the page size

To reduce the page size, this is actually returned to viewstates! If the current page requires interaction. You are too lazy to use server controls. But we can compress viewstate! About the compression order. Now let's talk about the selection of server controls,

For example, if we have such a requirement, we need to display the data on the page (only display). Generally, most people will select the label control! Why? It's a habit! I don't know if you have found that the label control will generate a span tag on the client, and this span is useless. (Don't tell me this can be rewritten. I will not discuss this issue now.) Of course, there may not be more than N labels on one page that can make the page too large. Here we only mention this detail, details. In fact, we can use <% = %> to display data on the page. The worst is literal. He will not generate useless garbage in the clientCode(Be careful when affecting the CSS effect)

 

In the existing project (B/S web external project), I can still find some effects achieved by hiding the display panel. This is simple... we can use jquery to achieve these effects when jquery is so common today.

I want to mention that on pages that only display data without interaction, do not use server-side controls. Disable viewstate, if the page requires interaction, consider other solutions as much as possible (except for internal projects)

 

 

Really, I have already had an incoherent speech. When I found that all the functions of the. NET Server controls were completed in the project, I really had internal injuries! (Internet project)

Third: selection of data binding controls

As we all know,. NET Has n data binding controls. It is estimated that list controls are the most common ones! I will select two For example: gridview and repeater.

First, let's talk about the advantages and disadvantages of gridview and repeater:

The gridview is relatively powerful and has all of its functions. Paging, editing, and Shenma .. However, if viewstate is disabled, it seems that only data can be displayed .. In addition, many garbage codes are generated. You can also play for yourself. If you have a BS project with high performance requirements, forget it! Besides, if viewstate. gridview is disabled, it becomes a chicken fault.

Compared with the gridview, the repeater has the flexibility of templates. The repeater does not generate any code and relies entirely on templates for implementation. It is more controllable.

 

In short, for personal projects or small projects, internal projects can use the gridview, as long as the boss agrees, but if the project is released to the Internet. Or use repeater. At least the style is easy to control and the page size is increased without generating additional code. Sometimes the page size is too large, and the network speed is slower. The loading speed of the page is still quite large!

Of course, if your project is not used by yourself, but used to fool the customer, you can also use the gridview

 

 

I can't write any more...

In the end, the performance of the Internet B/s project is sometimes not only affected by the speed of the network. The speed of page loading is related to many things. At the very least, this article mentions the page size. Reducing page size as much as possible is also part of ASP. NET Website optimization!

 

This article first reprinted http://blog.codepubs.com/archives/130 please indicate the source

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.