ASP. NET practical skills (1)

Source: Internet
Author: User
This article mainly introduces some common methods in ASP. NET programming. Code Skills, performance optimization, etc.

1. execution on the tracking page
Setting breakpoints is a common method in page debugging. In addition, you can view the tracing information of the page for error troubleshooting and performance optimization. It is very convenient to enable page tracing in ASP. NET. You only need to add the trace = "true" attribute to the page command: <% @ Page Language = " C # " Trace = " True " >

There are two types of tracking information:
A. Page execution details
It mainly includes the list of events in the page lifecycle and the Control tree list (you can view the number of HTML bytes for each control and the number of viewstate bytes) session Status, application status, Cookie set, querystring set, server variables, and other information.
B. Custom tracking information
By calling the trace. Write () or trace. Warn () method in the page code, you can write the specified content to the "trace Information" section in the trace information. Even if an error occurs on the page, the trace information is displayed and the application is being published.ProgramYou only need to remove the trace attribute from the page command.


2. Add client properties to the server control
We sometimes add some special attributes to the server-side controls. This type of attribute does not need to be processed by the server, but simply needs to be sent to the client. We may consider it as the client property, for example, HTML or custom attributes (which may be used to implement certain JavaScript Functions ). You can achieve this through the following methods:
A. directly add client properties to the control

< ASP: button ID = " Mybutton " Text = " Clickme " Onmouseover = " This. style. cursor = 'pointer' " Runat = " Server "   />

Onmouseover is a client attribute. Note that the compiler allows this write method, but a warning is displayed.
B. Call the built-in method
You can call webcontrol. Attributes. Add () to add client properties for the control, as shown below:

Mybutton. Attributes. Add ( " Onmouseover " , " This. style. cursor = 'pointer' " );

This is also the most common method.
C. Create a custom control
If you need to add client properties to a type of server-side control, you can create a custom control that inherits from the server-side control, which contains specific client properties.
With this in mind, ASP. NET 2.0 provides the onclientclick attribute for the button control (including the button, linkbutton, and imagebutton controls), which can be written as follows:

Mybutton. onclientclick =   " Alert ('Hello! ') " ;

This is a caring feature!

3. server-side verification of form data
The process of migrating data verification tasks from the server to the client promotes the generation of JavaScript, which is also a method we have used so far. However, this method can only be used to ensure the normal operation of the client JavaScript. Unfortunately, there are always some exceptions, such as the browser does not support JavaScript, or the user deliberately closes the JavaScript function of the browser, which leads to the first protection failure. It is safer to add the second protection, that is, to verify the data submitted by the user on the server, but this will undoubtedly increase the workload of developers.
ASP. NET 2.0 provides a series of form data verification controls to easily complete dual data verification tasks on the client and server. However, the page. isvalid attribute must be used for server-side verification. See the following example:

< Form ID = " Myform " Runat = " Server " >
< Div >
Name: < ASP: textbox ID = " Txtname " Runat = " Server " > </ ASP: textbox > & Nbsp; < ASP: requiredfieldvalidator ID = " Requiredfieldvalidator1 " Controltovalidate = " Txtname " Errormessage = " Enter the name! " Display = " Dynamic " Runat = " Server " > </ ASP: requiredfieldvalidator >
</ Div >
< Div >
< ASP: button ID = " Btnsubmit " Text = " Submit " Runat = " Server "   />
</ Div >
</ Form >

This is an HTML clip, with a requiredfieldvalidator control used to check whether a name is filled in. The following is the server code executed when you click the button:

Protected   Void Btnsubmit_click ( Object Sender, eventargs E)
{
If (Page. isvalid) // Note: Do not omit the judgment on the page. isvalid attribute.
{
Response. Write ("Your name is:" +Txtname. Text );
}
}

Pay special attention to page. isvalid attribute judgment, page. the isvalid attribute is true, which indicates that the submitted data is valid and you can proceed to the next step.

4. skip Form Verification
in some cases, we need to skip the verification of all controls in the form. However, in other cases, we want to have the verification function of some controls in the selected trigger form. Let's take a look at these two situations:
. skip all verification
assume that there is a form. In addition to various data entry controls, there are two buttons, one is the submit button and the other is the cancel button, there are also some data verification controls in the form. You do not need to verify the validity of the data in the form when you click the cancel button. Instead, you can directly submit the page to the server and redirect it to a specified page.
to implement this function, you can use the causesvalidation attribute of the button control (including the button, linkbutton, and imagebutton controls) to set this attribute to false to skip all verifications in the form.
B. trigger some verification
assume that there is a form divided into two functional areas: one for user login and the other for user registration, we hope that only the data verification of the logon zone will be triggered when the logon button is clicked, and only the data verification of the registration zone will be triggered when the Registration button is clicked.
the solution is to add related data verification controls and data submission controls (button controls) to the same verification group, this is achieved by setting the validationgroup attribute of each related control to the same value.

5. Keep the scroll bar position
Assume that there is a page where some data records are displayed in the form of a list. Each time you edit the records, you must submit the page to the server. In order to provide a good user experience, we hope that the position of the scroll bar will remain unchanged after a record is edited and saved. The traditional method is to send the information of the current scroll bar location (hidden field or querystring) to the server every time a page is submitted. When the page returns to the client, the server resets the scroll bar position in Javascript format based on the input location information.
If you use ASP. NET to implement this function, you only need to add the maintainscrollpositiononpostback = "true" attribute to the page command:

<% @ Page Language = " C # " Maintainscrollpositiononpostback = " True " >

 
6. disable unnecessary viewstate

Viewstate plays an important role in the ASP. NET operating mechanism. After viewstate is encoded, it is saved to the form hidden field and decoded whenever the page is returned to the server. Therefore, viewstate may cause two problems: bandwidth usage and computing resource consumption. Fortunately, not all controls need to enable viewstate. we can disable unnecessary viewstate.
Viewstate is enabled by default and needs to be disabled manually:
A. Disable page viewstate
Add the enableviewstate = "false" attribute to the page command:

<% @ Page Language = " C # " Enableviewstate = " False " >

After this attribute is added, viewstate cannot be used for the entire page and all controls in it, so you need to use it with caution.
B. Disable the viewstate control.
This is a recommended method. Set the enableviewstate attribute of the control to false to disable its viewstate. Here is a simple trick:
If the status of a widget 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 operated.
However, the status of Textbox, dorpdownlist, and other controls can be changed (through input, selection, and other operations), so retaining their viewstate is still useful.

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.