Some tips about ASP. NET 2.0 that you have not heard of yet [go]

Source: Internet
Author: User
Asp.net is an awesome framework for developing web applications. If you have been using it for a while, it is no secret. It provides some very powerful new features, and you only need a small amount of code to implement them. I used to list a list of simple (or even cool) functions that you can implement with only a small amount of or no C #/VB.net code. If you have other suggestions, you can add comments. If your suggestion is a task that can be easily applied, I will further update my list.

1,When the page is postbacks, keep the position of the scroll bar.
In ASP. NET 1.1, it is really painful to maintain the position of the scroll bar when performing the PostBack operation, especially when there is a grid (table?) on the page ?) When you want to edit a specific row. The page will be reloaded. the scroll bar is at the top of the page, not the expected position, so you have to drop down the scroll bar. In ASP. net2.0, you can simply add the maintainscrollpostiononpostback attribute to page ctictive (to implement the same function ). <% @ Page Language = "C #" maintainscrollpositiononpostback = "true" autoeventwireup = "true" codefile = "" inherits = "" %>

2,When a page is loaded, the control obtains the default focus.
This is another simple task, instead of writing the commit Crip script. If there is only one (or two) text input box on your page, why do users have to click the text box before entering it? The cursor cannot be automatically located in the text box. Can Users enter it immediately? You can easily use the defaultfocus attribute of the htmlform control. <Form ID = "frm" defaultfocus = "txtusername" runat = "server">

</Form>

 
3,When you press enter, set the default trigger button.
In ASP. NET 1.1, this is a very painful thing. When you press enter, you need to write some JavaScript code to ensure that the appropriate button on the page triggers a "click" event on the server side. Fortunately, when you press enter, you can use the defaultbutton attribute of htmlform to set which button to click. In another case, whenever a user (is the cursor more appropriate ?) You can set the defaultbutton attribute of the panel control when different buttons are triggered on the page. <Form ID = "frm" defaultbutton = "btnsubmit" runat = "server">

</Form>

4,It is easy to locate the nested controls (nested control? Neatly arranged controls? Cannot express it... haha ~).
It is really a headache to find some controls at the control level of a page. However, if you know how controls are nested (nest), you can use the less commonly used shortcut "$" to find controls without writing recursive code. If you're looking for a great way to recursively find a control (in cases where you don't know the exact control nesting) Check out my good buddy Michael Palermo's blog entry. (This sentence is an advertisement, but it will not be turned over ~). The following code uses the defaultfocus attribute to set the focus for the text box nested in the formview control. Note: Use "$" to define the nested mode (nesting): <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>

It is also a little tricky to call the findcontrol () method in the server code. For more details, visit I blogged about this later. Here is an example: textbox TB = This. findcontrol ("form1 $ formvw $ txtname") as textbox;
If (TB! = NULL)
{
// Access Textbox Control
}

5,Stronugly-Typed Access to cross-page PostBack controls to access the cross-page PostBack control in a strongly-typed manner.
This is more involved ("include "? Not like, it should be "not commonly used"), but it is very useful. Submit information on one page to another. Here ASP. NET 2.0 introduces the concept of cross-Page Submission. The button submits data to a page, and sets the postbackurl attribute of the button as the name of the target page. This method is used (cross-page submission ).
Generally, you can use previouspage. findcontrol ("controlid") to access submitted data. However, this depends on the situation (requires a cast), if you need to access the attributes on the previous page (this is often required ). If you add a public attribute to the background code of the page that initiates the return operation, you can add previouspagetype ve to the target page of this return operation to access these public attributes in a strongly typed manner. If you haven't tried it, it may sound confusing, so I can explain it more.

Assume that a page is called default. aspx and a public attribute is provided to return the textbox value defined in the page. You can access these attributes in a strongly typed manner on the page (called searchresult. aspx) where data is to be submitted. You only need to add previouspagetype ve at the top of the searchresult. ASPX page:

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

By adding this directive, the code in searchresult. aspx can access the textbox defined in default. aspx in a strongly typed manner. In the following example, assume that the property name defined in default. aspx is searchtextbox.

Textbox TB = previouspage. searchtextbox;

Obviously, this line of code runs normally only when the previous page (Previous Page) is default. aspx. At the same time, previouspagetype also has a typename attribute. Based on this attribute, you can define a base type, so that one or more pages can obtain the value of this base type to support multiple pages. You can learn more about previouspagetype here.
(This code is not easy to translate and is free from errors. It is only part of the meaning. Hope you can add it. The original Article is as follows: This code obviusly only works if the previous page is default. aspx. previouspagetype also has a typename property as well where you coshould define a base type that one or more pages derive from to make this technique work with multiple pages. you can learn more about previouspagetype here .)

6,Access the masterpages control in a strongly typed manner.
Previouspagetype direve is not the only method for providing strong access controls. To access the control defined in masterpages in a strongly typed manner, you can add mastertype ctictive to the page to be displayed (Remember, mastertype directive is the same as previouspagetype direve, you can also define a type name)

<% @ Mastertype virtualpath = "masterpage. Master" %>

In this way, you can access the attributes of the Target Master page in the content page using the following code:

This. master. headertext = "label updated using mastertype directive with virtualpath attribute .";

You can also find some tips on using the master page, including "sharing the master page to access the IIS virtual directory" mentioned in my previous blog.

7,Validation group validation groups
You have such a page that contains several controls and buttons. When you click one of the buttons, you want the button specified by evaluate to be evaluated ...... , Instead of all the buttons on the page. In ASP. NET 1.1, if you do not resorting to some hack code ?) It is very difficult to solve this problem. ASP. NET 2.0 adds a validationgroup attribute to all verification controls and buttons (buttons and linkbuttons), which can easily solve the problem. If there is a textbox and a button control at the top of the page, the textbox is a requiredfieldvalidator control, and the validationgroup attribute values on the button and requiredfieldvalidator control are set to the same value, when you click the button, you can fire ("Activate "? ~) One verification. When you click the button, any verification defined outside the validationgroup is ignored. Here is an 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"/>
.
Other controls with validators and buttons defined here
</Form>

8,The name of the control or variable.
This technique is not directly related to Asp.net, but is more related to vs.net. This technique is useful for those of you who only remember the first few letters of the control or variable name, but do not remember the full name. This gives me the opportunity to mention two great downloads from Microsoft. First, press Ctrl + spacebar when entering the first few letters of the Control name. vs.net will pop out a brief list of matching items. Indeed, this makes it easier to find the control or variable name. (Translator's note: why is there no second ?) For interested friends, Microsoft provides a complete set of vs.net shortcut keys to download: C # version here and VB. NET version here

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.