Today for you to introduce 6 asp.net common skills, the use of simple, with a high practicality, remember to collect OH
1. Track page execution
In addition to setting breakpoints as a common means of page debugging, you can also troubleshoot and optimize performance by viewing the page's tracking information. Asp. NET to enable page tracking is very convenient, simply add the trace= "True" attribute in the page directive: setting breakpoints is a common means of page debugging, and you can also troubleshoot and optimize performance by viewing the page's tracking information. Asp. NET to enable page tracking is very convenient, simply add the trace= "True" attribute to the page directive:
Trace information can be grouped into two categories:
A. Page execution details
This includes the list of events in the page lifecycle, the list of control trees (you can view the number of HTML bytes per control and the number of ViewState bytes), session state, application state, cookie collection, QueryString collection, Information such as server variables.
B. Customizing trace information
you can write the specified content to the trace Information section in the trace information by calling the Trace.Write () or Trace.Warn () method in the page code. Even if an error occurs on the page, the trace information is displayed, and the trace attribute is removed from the page instruction without the need to delete the associated tracking code when the application is published.
2. Adding client properties to server-side controls
we sometimes add special properties to server-side controls that do not require server-side processing and simply send to the client, which we might call client-side properties, such as HTML attributes or custom attributes (which may be used to implement a particular JavaScript feature). This can be achieved in several ways:
A. Adding client properties directly to controls
The onmouseover is the client property, note that the compiler is allowed to do this, but displays a warning.
B. Calling built-in methods
You can add client properties to a 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 a custom control
If you frequently need to add client properties to a server-side control of a type, consider creating a custom control that inherits from the server-side control, which contains specific client properties.
It is with this in mind that ASP.net 2.0 provides the OnClientClick properties for button controls, including button, LinkButton, ImageButton controls, which you can write:
Mybutton.onclientclick = "alert (' hello! ')";
What a thoughtful Function!
3. Server-side validation of form data
The process of migrating data validation tasks from the server side to the client has led to the creation of JavaScript, which is one of the ways we have used to date. But only in the premise of ensuring that the client JavaScript is running properly, this way can play its role. Unfortunately, there are always exceptions, such as browsers that do not support JavaScript, or the user deliberately shuts down the browser's JavaScript functionality, which causes the first protection to fail. The safer approach is to add a second protection, which is server-side validation of the data submitted by the user, but it will undoubtedly increase the developer's workload.
ASP.net 2.0 provides a series of form data validation controls that make it easy to perform dual data validation tasks on both the client and server side. However, to make the server-side validation function work, you need to use the Page.IsValid attribute, as shown in the following example:
This is an HTML fragment, where a RequiredFieldValidator control is used to check whether the name has been 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: Do not omit the judgment of the Page.IsValid attribute
{
Response.Write ("Your name is:" + txtName.Text);
}
Among them, pay special attention to the judgment of the Page.IsValid property, only if all the validation controls on the page succeed in validating the data, the Page.IsValid property is true, which means that the submitted data is valid data and you can go to the next action.
4. Skip Form Validation
In some cases, we need to skip validation of all the controls in the form, but in other cases we want to have the validation capabilities of some controls in the selected Touch release form. Take a look at these two things separately:
A. Skip all validation
Suppose you have a form with two buttons in addition to a variety of data entry controls, one for the submit button, the other for the Cancel button, and some data validation controls in the form. We hope that when you click the Cancel button, you do not need to verify the validity of the data in the form, but instead submit the page directly to the server and redirect it to a specific page.
To do this, you can take advantage of the CausesValidation properties of button controls, including buttons, LinkButton, ImageButton controls, and set this property to False to skip all validation in the form.
B. Triggering certain validations
Suppose that there is a form, divided into two functional areas, one for user login, another for user registration, we hope that when the click on the login button to trigger only the login area of data validation, when the click on the registration button only trigger the registration area data validation.
The workaround is to include the associated data validation control and the data submission control (the button control) in the same validation group by setting the ValidationGroup property of each related control to the same value.
5. Keep the scroll bar position
If you have a page that shows some data records in a list, and you need to submit a page to the server each time you edit the record, we want the scroll bar position to stay the same every time you edit a record and save it for a good user experience. The traditional practice is to pass the current scroll bar location information to the server side in some way (hidden field or querystring) each time the page is submitted, and when the page returns to the client, the server side will reset the scroll bar position in JavaScript form based on the location information passed in.
If this functionality is accomplished through ASP.net, it will be very simple to add the maintainscrollpositiononpostback= "true" attribute to the page directive:
6. Disabling unnecessary viewstate
In the operating mechanism of ASP.net, ViewState plays an important role. 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 can lead to two problems: bandwidth usage and computational resource consumption. Fortunately, not all controls need to enable ViewState, we can completely disable unnecessary viewstate.
ViewState is turned on by default and requires manual shutdown:
A. Disable page viewstate
Add the Enableviewstate= "false" attribute to the page directive:
When this property is added, the entire page, and all of its controls, will not be able to use ViewState, and should be used with caution.
B. Disabling controls ViewState
This is the recommended way to disable its ViewState by setting the control's EnableViewState property to False, and here's a simple trick:
If the state of a control cannot be changed by the operator, you can disable its viewstate. The most typical is the label control, which only displays information and cannot be manipulated.
But the state of the controls, such as TextBox, Dorpdownlist, can be changed (through input, selection, and so on), so it is still useful to keep their viewstate.
The above is a small series for everyone to tidy up the asp.net 6 tips, I hope you like.