ASP. NET Common Tips
- 2021-07-01 07:08:19
Today, I will introduce 6 common skills of ASP. NET. It is easy to use and has high practicability. Remember to collect it
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, just add Trace= "True" attribute to Page instruction: setting breakpoints is a common means in the process of page debugging, in addition, error troubleshooting and performance optimization can be carried out by viewing the trace information of the page. 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 you do not need to delete the associated trace code when publishing the application, just remove the Trace attribute from the Page directive.
2. Add client-side properties to server-side controls
a. Adding client-side properties directly to the control
< asp:Button ID="MyButton" Text="ClickMe" onmouseover="this.style.cursor='poi nter'" 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:
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
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, the Page. IsValid attribute is also required, as shown in the following example:
< form id="MyForm" runat="server" >
< div >
Name: < asp:TextBox ID="txtName" runat="server" > < /asp:TextBox >
< asp:RequiredField Validator ID="RequiredFieldValidator1"
ControlToValidate="txtName" ErrorMessage=" Please fill out Name! " Display="Dynamic" runat="server" > < /asp:RequiredFieldValidator >
< /div >
< div >
< asp:Button ID="btnSubmit" Text=" Submit " runat="server" / >
< /div >
< /form >
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, and 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
If this 1 function is implemented through ASP. NET, it will be very simple to add the MaintainScrollPositionOnPostback= "true" attribute to the Page instruction:
< %@ Page Language="C#"
6. Disable unnecessary ViewState
ViewState plays an important role in the operation mechanism of ASP. NET. 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 directive:
< %@ Page Language="C#" EnableViewState="false" >
When this property is added, ViewState will not be available for the entire page and all the 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.
However, the state of TextBox, DorpDownList and other controls can be changed (through input, selection, etc.), so it is useful to keep their ViewState.
The above is the site for everyone to organize ASP. NET 6 tips, I hope you like.