Detailed introduction of practical skills of ASP. NET

  • 2021-07-02 23:53:36
  • OfStack

In fact, we have been exposed to a lot of practical skills about ASP. NET. The following is a summary for everyone for your reference.

1. Track page execution

Setting breakpoints is a common tool in page debugging. In addition, you can check the trace information of the page for error troubleshooting and performance optimization. It is very convenient to enable page tracing in ASP. NET by adding the Trace= "True" attribute to the Page directive:


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

Trace information can be divided into two categories:

a. Page execution details

It mainly includes the event list in the page life cycle, the control tree list (you can view the number of HTML bytes and ViewState bytes of each control), Session status, Application status, Cookie set, QueryString set, server variables and other information.

b. Custom Trace Information

The specified content is written to the "Trace Information" section of the trace information by calling the Trace. Write () or Trace. Warn () methods in the page code. Trace information is displayed even if an error occurs on the page, and the Trace attribute is removed from the Page directive without removing the associated trace code when publishing the application.

2. Add client-side properties to server-side controls

Sometimes we add a special property to a server-side control that requires no server-side processing and is simply sent to the client. We might as well call it a client-side property, such as an HTML property or a custom property (which may be used to implement a specific JavaScript function). This can be achieved in the following ways:

a. Adding client-side properties directly to the control


<this.style.cursor='pointer'" runat="server" /> 

Where onmouseover is a client-side property, note that the compiler allows this writing, but will display a warning.

b. Call built-in method

You can add client-side properties to the control by calling the WebControl. Attributes. Add () method, as follows:


MyButton.Attributes.Add("onmouseover", "this.style.cursor='pointer'"); 

This is also the most commonly used method.

c. Creating custom controls

If you often need to add client-side properties to a certain type of server-side control, consider creating a custom control that inherits from the server-side control and contains specific client-side properties.

With this in mind, the OnClientClick property is provided for button controls (including Button, LinkButton, ImageButton controls) in ASP. NET 2.0, which can be written as follows:


MyButton.OnClientClick = "alert('Hello!')"; 

What an intimate function!

3. Server-side validation of form data

The process of migrating data validation tasks from the server to the client prompted the emergence of JavaScript, which is also a method we have used up to now. However, this method can only play its role on the premise of ensuring the normal operation of the client JavaScript.

Unfortunately, there are always one exception, such as the browser does not support JavaScript, or the user deliberately turns off the browser's JavaScript function, which leads to the failure of the first protection. It is safer to add the second protection, that is, to verify the data submitted by users on the server side, but this will undoubtedly increase the workload of developers.

ASP. NET 2.0 provides a series of forms data validation controls, which can easily complete the dual data validation tasks on the client and server sides. But for server-side authentication to work, you also need to use the Page. IsValid attribute, as shown in the following example:

Name:


ator ID="RequiredFieldValidator1" ControlToValidate="txtName" 
ErrorMessage=" Please fill in your name! " Display="Dynamic" runat="server"> 

This is an HTML fragment with an RequiredFieldValidator control to check that the name is filled in. Here is the server-side code that executes when the button is clicked:


protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
if (Page.IsValid) // Note: Don't miss the right Page.IsValid Judgment of attributes  
{ 
Response.Write(" Your name is: " + txtName.Text); 
} 
} 

Among them, special attention should be paid to the judgment of Page. IsValid attribute. Only when all validation controls in the page verify the data successfully, the Page. IsValid attribute is True, which means that the submitted data is valid data and can be moved to the next step.

4. Skip form validation

In some cases, we need to skip validation of all controls in the form, while in other cases, we want to selectively touch the validation function of some controls in the form. Take a look at these two situations respectively:

a. Skip all validation

Suppose you have a form with two buttons in addition to various data entry controls, one is a submit button, the other is a cancel button, and there are one data validation control in the form. Instead of verifying the validity of the data in the form when we click the Cancel button, we want to submit the page directly to the server and redirect it to a specific page.

To do this, you can take advantage of the CausesValidation property of the button controls (including the Button, LinkButton, ImageButton controls), which is set to false to skip all validation in the form.

b. Triggers some validation

Suppose there is a form that is divided into two functional areas, one for user login and the other for user registration. We want to trigger only data verification in the login area when clicking the login button, and only data verification in the registration area when clicking the registration button.

The solution is to add the related data validation controls and data submission controls (button controls) to the same validation group by setting the ValidationGroup property of each related control to the same value.

5. Hold the scroll bar position

Suppose you have a page with a list of data records, and you need to submit the page to the server every time you edit the records. To provide a good user experience, we want to keep the scroll bar position unchanged every time you edit and save one record. The traditional way is to send the current position information of the scroll bar to the server in some way (Hidden field or QueryString) every time the page is submitted. When the page returns to the client, the server resets the position of the scroll bar in the form of JavaScript according to the incoming position information.

If this 1 function is implemented through ASP. NET, it will be very simple to add the MaintainScrollPositionOnPostback= "true" attribute to the Page directive:


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

6. Disable unnecessary ViewState

ViewState plays an important role in the operation mechanism of ASP. NET. The ViewState is encoded and stored in the form Hidden field, which is decoded whenever the page is returned to the server. Therefore, the use of ViewState will bring two problems: bandwidth occupation and consumption of computing resources. Fortunately, not all controls need to enable ViewState, so we can completely disable unnecessary ViewState.

ViewState is turned on by default and needs to be turned off manually:

a. Disable page ViewState

Add the EnableViewState= "false" attribute to the Page instruction:


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

When this property is added, ViewState will not be available for the entire page and all controls in it, so use it with caution.

b. Disable control ViewState

This is the recommended way to disable ViewState by setting the EnableViewState property of the control to False. Here is a simple trick:

If the state of a control cannot be changed by the operator, its ViewState can be disabled. The most typical one is Label control, which can only display information and cannot be operated.

But the state of TextBox, DorpDownList and other controls can be changed (through input, selection, and so on), so it is useful to keep their ViewState.

Hopefully, the six aspects of asp. net practical techniques described in this article will help you.


Related articles: