Tips for ASP. NET developers

Source: Internet
Author: User

1. frontend and backend Interaction

1.1. First, let's talk about how to identify and use HTML elements (such as <p> elements) in the background code to bind foreground elements to the background code.

 

In this case, the backend Code cannot be found:

 

So we need to change the front-end to run the P tag on the server:

 

In this way, the background can recognize:

 

:

 

1.2. Let's talk about how to bind the foreground code to the background variable. If you define a variable in the foreground and need it in the background, but do not know how to set the value, do as I said: in the background, define the variable as the public type.

 

Write in front-end as follows:

 

:

 

2. Good at the pop-up box

 

3. Keep the scroll bar position at the current position when the page is refreshed, rather than the top of the page. You only need to add the maintainscrollpositiononpostback = "true" attribute to the page command:
<%@ Page Language="C#" MaintainScrollPositionOnPostback="true">

 

4. 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 tracing Code does not need to be deleted when the application is released. You only need to remove the trace attribute from the page command.

 

5. Set the default focus control after the form is loaded.

Add defaultfocus = "Control ID" to form:

<form id="form1" runat="server" defaultfocus ="TextBox2">

 

6. Set the default form submission button

Add defaultbutton = "Control ID" to form:

<form id="form1" runat="server" defaultbutton="Button2" >

 

7. Press enter to convert to Tab

For example, N items need to be added during the registration process. You can use the tab key to jump to the webpage. However, if you change the Enter key to the tab key, the registration speed will certainly be much faster.

<script   language="JavaScript" type="text/javascript">       window.document.attachEvent("onkeydown", function () {        if (window.event.keyCode == 13) {            window.event.keyCode = 9;        }    });   </script> 

8. Right-click shielding 8. Right-click shielding

<SCRIPT type = "text/JavaScript"> function block (oevent) {If (window. event) oevent = Window. event; If (oevent. button = 2) Alert ("right-click unavailable");} document. onmousedown = block; </SCRIPT>

9. Make the page text unselected and copied

You can move your feet in the body to make the text on the page unselectable. Even if the text is selected, it will be deselected after you open the mouse.

   <body oncontextmenu=self.event.returnValue=false onselectstart="return false" ondragenter='return false' onbeforeprint='return false' onmouseup='document.selection.empty()'> 

10. Shield paste, copy, cut, and right-click in textbox.

<asp:TextBox ID="TextBox1" onpaste="return false" oncopy="return false" oncut="return false" context="noContext" runat="server" ></asp:TextBox>

Onpaste = "Return false" Disable pasting
Oncopy = "Return false" Disable Replication
Oncut = "Return false" Disable cut
Context = "nocontext" Disable context menu

 

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.