Summary: Some simple and useful tips on ASP.net 2.0

Source: Internet
Author: User
Tags define eval microsoft website
Asp.net| Tips

1, after the callback to keep the scroll bar position

In asp.net1.1, it is very painful to keep the scroll bar in place after callback, especially if there is a grid in the page and you want to edit specific rows. In order not to stay in the desired row, the page will reload and must scroll down at the top. In Asp2.0, simply add the Maintainscrollpostiononpostback attribute to the page's properties:

<%@ Page language= "C #" maintainscrollpositiononpostback= "true" autoeventwireup= "true" codefile= "" inherits= ""% >

2, after the page load set the default focus on the control

This is also a very simple example that does not require the help of JavaScript to complete. If there are one or two textbox in the page, why should you let the user click on the textbox to start entering data? Is it possible to allow the cursor to be stuck in a textbox and then enter data? You can easily do this by using the DefaultFocus property of the HtmlForm control:

<form id= "frm" defaultfocus= "txtUserName" runat= "Server" >

</form>

3, the user clicks "Enter" the key when triggers the default button click Practice.

In Asp1.1, the click event of a server segment that can be associated with a button when the user clicks on the "Enter" key is painful because it requires javascript. Fortunately, you can now use the HtmlForm control's DefaultButton property to set it. This property can also be set on the Panel control, and when the user moves to a different panel on the page, click the ENTER key to trigger the Click event of a different button control.

<form id= "frm" defaultbutton= "btnsubmit" runat= "Server" >

</form>

4, simple to find a fixed control.

It's painful to look at the controls in a page on a hierarchical basis, but if you know how the controls are anchored to the page, you can use the shorthand "$" to find the control without writing recursive code. Take a look at the code below and note the use 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 using the FindControl () function on the server side:

TextBox TB = this. FindControl ("Form1$formvw$txtname") as TextBox;
if (TB!= null)
{
Access TextBox Control
}

5. Strongly type access controls for cross-page submissions

This is a bit more than anything else, but it's useful. ASP.net 2.0 introduces the concept of cross-postback so that one page can send information back to a different page, and you can set the button control's PostBackUrl property to a page that accepts postback data. Generally, the postback data can do something like the previous page. However, if you want to get the properties of the previous page to make a control, you need a cast (), and if you add a public property to the Code-behide page that caused the postback, You can add previouspagetype directly to a strongly typed way to point to the page that caused the postback to access that property.

If there is a page default.aspx, he has a public property to return this page to a textbox, the Data Postback destination page (searchresults.aspx) can be in a strongly typed manner (without the need for the FindControl () method), Add at the top of the page:

<%@ PreviousPageType virtualpath= "default.aspx"%>

In this way, the code in searchresults.aspx can access the Default.aspx textbox in a strongly typed manner. The following example assumes that the Default.aspx-defined property name is SearchTextBox:

TextBox TB = Previouspage.searchtextbox; PreviousPageType also has a TypeName property, you can define a basic type, one or more pages inherited from this type, so that this technology can be used on multiple pages.

Previouspage.iscrosspagepostback can determine whether to submit for a collapsed page.

For a cross-page submit supplement:

In the ASP.net 1.X version, the page is submitted to itself, and it is not convenient to specify the destination page to be submitted. For example, a button in firstpage.aspx can only be committed to firstpage.aspx, not to secondpage.aspx. Most of the time, the way the ASP.net 1.X works has led to a lot of restrictions on how we develop. Familiar with asp/jsp/php friend probably very unaccustomed, because used to submit the way suddenly cannot use, although also has solves this problem the method (wants to understand the reader to be able to go to the Microsoft website to watch webcast), but the process is too cumbersome, inconvenient. To our delight, ASP.net 2.0 has a simple way of submitting across pages. The button in the first page can be added to the PostBackUrl property to accept the submitted page, and the page will add PreviousPageType instructions, to be the target page open in a new window you can add target= to the source page

tag Blank ' property.

6, strongly typed controls to access the master page

The PreviousPageType directive is not the only method that can strongly type access the control. If you define a public property in a master page and you want to access it in a strongly typed manner, you can add a mastertype directive at the top of the page (note: The MasterType directive allows you to define a typename like PreviousPageType)

<%@ MasterType virtualpath= "Masterpage.master"%>

You can access the properties of the target master page by writing the following code in the content page:

This. Master.headertext = "Label updated using MasterType directive with virtualpath attribute.";

7. Validation Groups Validation Group

There may be multiple controls and multiple buttons in a page. When one of the buttons is clicked, you want a specific validator to be fired instead of all the validator in the page. There is no better way to asp.net 1.1 than to hack code. ASP.net 2.0 adds Validatorgroup properties to all the validator controls and buttons (button, LinkButton, and so on), which can be easily resolved. If there is a TextBox on the page and there is a RequiredFieldValidator and button control next to it, You can set the ValidationGroup property of the RequiredFieldValidator and the button to the same value so that when you click the button, it triggers only the validor of the RequiredFieldValidator. Any other validator that is not defined within the ValidationGroup will be ignored. Look at the following example:

<form id= "Form1" runat= "Server" >
Search Text: <asp:textbox id= "Txtsearch" runat= "Server"/>
<asp:requiredfieldvalidator id= "Valsearch" runat= "Server"
Controltovalidate= "Txtsearch" validationgroup= "Searchgroup"/>
<asp:button id= "btnsearch" runat= "Server" text= "Search"
validationgroup= "Searchgroup"/>.
Controls with validators and buttons defined
</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.