ASP. NET tips you may not know

Source: Internet
Author: User

<!--page and profile--

1. Keep the scroll bar position after the page is submitted

You can add the MaintainScrollPositionOnPostBack directive to the page directive

<%@ page language= "C #" maintainscrollpositiononpostback= "true" autoeventwireup= "true" codefile= "..." Inherits= "..."%>

2. After the page is loaded, move the focus to a control, just specify the DefaultFocus property of the form.

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

3. Set the default corresponding button of the form via the DefaultButton property, which is the button that is triggered when the user strikes a return

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

4. You can use the $ symbol to easily find a control using the FindControl method

<form id= "Form1"  runat= "Server"  defaultfocus= "Formvw$txtname";
    <div>
        <asp:formview id= "FORMVW"  runat= "Server"
            <itemtemplate>
                 name: 
                 <asp:textbox  id= "txtname"  runat= "Server" &NBSP;
                     text= ' <%# eval ("FirstName") + "" + eval ("LastName")% > '  />
            </ItemTemplate>
        </asp:formview>
    </div>
</ Form>

In the example above, the DefaultFocus property of the form is used to specify the control where the page is loaded with focus, and the $ symbol is used to easily locate the Txtname

You can also use the following code to easily find a control

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

5. About cross-page submissions the method of making a strong type of submit page control, see the original

6. To access the MasterPage attribute members using a strongly typed method, see the original

7. We can use the properties of the validation control ValidationGroup to specify which group the validation control belongs to, and specify the validation group that the button needs to activate in the ValidationGroup property of the button.

<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" />
....
Other controls with validators and buttons defined here
</form>

This article is translated, the original address is as follows:

http://weblogs.asp.net/dwahlin/archive/2007/04/17/ Simple-asp-net-2-0-tips-and-tricks-that-you-may-or-may-not-have-heard-about.aspx

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.