ASP. NET 2.0 simple and useful tips

Source: Internet
Author: User
Tags microsoft website

1. Keep the position of the scroll bar after callback
In ASP. net1.1, it is very painful to keep the position of the scroll bar after callback, especially when there is a grid in the page and you want to edit a Specific Row. The page is reloaded and must be rolled down at the top of the page to keep the page unchanged. In asp2.0, you only need to simply add the maintainscrollpostiononpostback attribute to the page attribute:
<% @ Page Language = "C #" maintainscrollpositi autoeventwireup = "true" codefile = "" inherits = "" %>
2. After the page is loaded, set the default focus to the control.
This is also a very simple example, which can be completed without the help of JavaScript. If there are two textbox in the page, why do you want the user to click textbox to start inputting data? Can the cursor be placed in the textbox to input data? You can easily use the defaultfocus attribute of the htmlform control:
<Form ID = "frm" defaultfocus = "txtusername" runat = "server">

</Form>
3. Click the default button when you click Enter.
In asp1.1, it is very painful to enable users to click the "enter" key to associate the Click Event of the server segment of a button with JavaScript. Fortunately, now you can use the defaultbutton attribute of the htmlform control to set it. This attribute can also be set on the panel control. When a user moves to a different Panel on the page, click Enter to trigger the click events of different button controls.
<Form ID = "frm" defaultbutton = "btnsubmit" runat = "server">

</Form>
4. Simple search for fixed controls.
It is very painful to search for controls on the page by Level in the control, but if you know how the control is fixed in the page, you can use the abbreviation "$" to search for controls without writing recursive Code . Please refer to the following code and pay attention to the usage of "$:
<Form ID = "form1" runat = "server" defaultfocus = "formvw $ txtname">
<Div>
<Asp: formview id = "formvw" runat = "server">
<Itemtemplate>
Name:
<Asp: textbox id = "txtname" runat = "server"
TEXT = '<% # eval ("firstname") + "" + eval ("lastname") %>'/>
</Itemtemplate>
</ASP: formview>
</Div>
</Form>
This tip can also be used when the findcontrol () function is used on the server side:
Textbox TB = This. findcontrol ("form1 $ formvw $ txtname") as textbox;
If (TB! = NULL)
{
// Access Textbox Control
}
5. Controls for cross-Page Submission with strong type access
This is more useful than others. ASP. NET 2.0 introduces the concept of cross-sending so that a page can send information back to another different page. You can set the postbackurl attribute of the button control to the page for receiving the send-back data. Generally, the resend data can do something like the previous page. However, if you want to obtain the attributes of the control set on the previous page, you need a cast (). If you add a public attribute to the code-behide page that causes the sending back, you can directly add previouspagetype in a strongly typed manner to point to the page that causes the sending back to access that attribute.
If there is a page default. aspx, which has a public attribute to return a textbox on this page and the data sending target page (searchresults. aspx) can be added to the top of the page in a strongly typed mode (the findcontrol () method is not required:
<% @ Previouspagetype virtualpath = "default. aspx" %>
In this way, the code in searchresults. aspx can access the textbox of default. aspx in a strongly typed manner. The following example assumes that the property defined by default. aspx is searchtextbox:
Textbox TB = previouspage. searchtextbox; previouspagetype also has a typename attribute. You can define a basic type and inherit one or more pages from this type, so that this technology can be used on multiple pages.
Previouspage. iscrosspagepostback can be used to determine whether the page is submitted.
Supplement cross-page submission:
In ASP. NET 1.x, all pages are submitted to themselves, and it is not convenient to specify the target page to be submitted. For example, a button in firstpage. aspx can only be submitted to firstpage. aspx, but not secondpage. aspx. Most of the time, the way ASP. NET 1.x works imposes many restrictions on our development methods. Friends who are familiar with ASP, JSP, and PHP are probably not used to it, because the submission method that is frequently used in the past suddenly cannot be used, although there are also ways to solve this problem (readers who want to know more about it can watch webcast on the Microsoft website), the process is too cumbersome and inconvenient. We are glad that ASP. NET 2.0 has a simple cross-Page Submission method. You can add the postbackurl attribute on the button on the first page to the page that accepts the submission, and add the previouspagetype command on this page, if the target page is opened in a new window, you can add the target = '_ blank' attribute to the <form> mark on the Source Page.
6. Controls for accessing the master page with a strong type
The previouspagetype command is not the only method that allows strong-type access to controls. If you define a public attribute on the master page and want to access it in a strongly typed manner, you can add the mastertype command at the top of the page (note: the mastertype command can define a typename like the previouspagetype command)
<% @ Mastertype virtualpath = "masterpage. Master" %>
You can write the following code on the Content Page to access the attributes of the Target Master page:
This. master. headertext = "label updated using mastertype directive with virtualpath attribute .";
7. Validation Group
A page can contain multiple controls and buttons. When one of the buttons is clicked, you want a specific validator to be fired, instead of all validator on the page. There is no better way except hack code in ASP. NET 1.1. In ASP. NET 2.0, the validator group attribute is added to all validator controls and buttons (such as buttons and linkbuttons) to easily solve this problem. If there is a textbox in the page and there is a requiredfieldvalidator and button control next to it, you can set the validationgroup attributes of requiredfieldvalidator and button to the same value so that only the validator of requiredfieldvalidator is triggered when you click button. Any other validator that is not defined in the validationgroup will be ignored. See the following example:
<Form ID = "form1" runat = "server">
Search Text: <asp: textbox id = "txtsearch" runat = "server"/>
<Asp: requiredfieldvalidator id = "valsearch" runat = "server"
C validati/>
<Asp: button id = "btnsearch" runat = "server" text = "Search"
Validati/>.
Other controls with validators and buttons defined here
</Form>

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.