In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what the practical skills of ASP.NET are, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
1. Track page execution
Setting breakpoints is a common means in the process of page debugging, in addition, you can also view the tracking information of the page for error troubleshooting and performance optimization. Enabling page tracking in ASP.NET is very convenient, as long as you add the Trace= "True" attribute to the Page instruction: setting breakpoints is a common means in the process of page debugging, in addition, you can also view the page tracking information for error troubleshooting and performance optimization. Enabling page tracking in ASP.NET is very convenient, as long as you add the Trace= "True" attribute to the Page instruction:
<% @ Page Language= "C#" Trace= "true" >
Tracking information can be divided into two categories:
a. Page execution details
It mainly includes the list of events 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 collection, QueryString collection, server variables and other information.
b. Custom tracking information
You can write the specified content to the "Trace Information" section of the trace information by calling the Trace.Write () or Trace.Warn () method in the page code. Even if there is an error on the page, the trace information will still be displayed, and there is no need to delete the relevant trace code when publishing the application, just remove the Trace attribute from the Page directive.
two。 Add client properties to the server-side control
We sometimes add some special properties to the server-side controls, which do not require server-side processing, but simply send them to the client. We might as well call them client-side attributes, such as HTML attributes or custom attributes (which may be used to implement a specific JavaScript function). This can be achieved in the following ways:
a. Add client properties directly to the control
< asp:Button ID= "MyButton" Text= "ClickMe" onmouseover=this.style.cursor='pointer' "runat=" server "/ >
Where onmouseover is a client attribute, note that the compiler allows this writing, but displays a warning.
b. Call the built-in method
You can add client 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. Create a custom control
If you often need to add client properties to a type of server-side control, consider creating a custom control that inherits from that server-side control and contains specific client-side properties.
With this in mind, ASP.NET 2.0 provides the OnClientClick property for button controls (including Button, LinkButton, ImageButton controls), which can be written as follows:
MyButton.OnClientClick = "alert ('hellograms')"
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 side led to the creation of JavaScript, which is a way we still use today. However, this method can play its role only on the premise of ensuring the normal operation of the client JavaScript. Unfortunately, there are always some exceptions, such as browsers do not support JavaScript, or users deliberately turn off the JavaScript feature of the browser, which leads to the failure of heavy protection. It is safer to add a second protection, that is, server-side validation of data submitted by users, but this will undoubtedly increase the workload of developers.
ASP.NET 2.0 provides a series of form data validation controls, which can easily complete both client-side and server-side data validation tasks. However, for server-side authentication to work, you also need to use the Page.IsValid attribute, as shown in the following example:
< form id= "MyForm" runat= "server" > < div > name: < asp:TextBox ID= "txtName" runat= "server" > < / asp:TextBox > < asp:RequiredFieldValidator ID= "RequiredFieldValidator1" ControlToValidate= "txtName" ErrorMessage= "Please fill in your name!" Display= "Dynamic" runat= "server" > < / asp:RequiredFieldValidator > < / div > < div > < asp:Button ID= "btnSubmit" Text= "submit" runat= "server" / > < / div > < / form >
This is a HTML fragment with a RequiredFieldValidator control to check whether a 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: don't omit the judgment of the Page.IsValid attribute {Response.Write ("your name is:" + txtName.Text);}}
Among them, you should pay special attention to the judgment of the Page.IsValid attribute. Only when all the 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 you can proceed to the next step.
4. Skip form validation
In some cases, we need to skip the validation of all controls in the form, while in others, we want to selectively trigger the validation of some controls in the form. Let's take a look at these two situations:
a. Skip all validation
Suppose you have a form that has two buttons in addition to various data entry controls, one is the submit button and the other is the cancel button, and there are some data validation controls 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 specified page.
To achieve this, you can use the CausesValidation property of button controls (including Button, LinkButton, ImageButton controls), which can be set to false to skip all validation in the form.
b. Trigger 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 validation in the login area when the login button is clicked, and only data validation in the registration area when the registration button is clicked.
The solution is to add the relevant data validation control and the data submission control (button control) to the same validation group, which is achieved by setting the ValidationGroup property of each related control to the same value.
5. Keep the scroll bar position
Suppose there is a page that displays some data records in the form of a list, and each time you edit the records, you need to submit the page to the server. In order to provide a good user experience, we want to edit a record and save it each time. The scroll bar position remains the same. The traditional practice is that every time the page is submitted, the location information of the current scroll bar is transmitted to the server in a certain way (Hidden field or QueryString). When the page is returned to the client, the server resets the scroll bar position in the form of JavaScript according to the incoming location information.
If you implement this function through ASP.NET, it will be very simple, simply add the MaintainScrollPositionOnPostback= "true" attribute to the Page instruction:
<% @ 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 every time the page is sent back to the server. Therefore, the use of ViewState brings two problems: the consumption of bandwidth and the consumption of computing resources. Fortunately, not all controls need to enable ViewState, so we can completely disable unnecessary ViewState.
ViewState is enabled by default and needs to be manually disabled:
a. Disable page ViewState
Add the EnableViewState= "false" attribute to the Page instruction:
<% @ Page Language= "C#" EnableViewState= "false" >
When this property is added, the entire page and all controls in it will not be able to use ViewState, so use it with caution.
b. Disable the control ViewState
This is the recommended way to disable the ViewState of a control by setting its EnableViewState property 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 is the 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, and so on), so it is useful to keep their ViewState.
On the practical skills of ASP.NET which are shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.