Asp. NET control 10 most useful properties detailed _ practical Tips

Source: Internet
Author: User
1, ClientIDMode
Rendering the ASP.net control automatically generates an ID that, when referenced in client script, creates a lot of trouble, although it is a simple concatenation of naming containers and IDs, but it still cannot predict the range of IDs generated.
ASP.net 4.0 solves this problem with the ClientIDMode property, which allows you to control how these IDs are generated, ClientIDMode has four selectable values: Autoid,static,predictable and inherit. The following is an explanation of the meaning of these four values:
autoid– is consistent with previous versions of 4.0 and automatically generates IDs.
static– the value of the ID you specify, which will not change when the control is rendered.
predictable– by you specify the suffix and then merge with the container control's ID attribute.
inherit– inherits the settings of the parent control.
Note that the value of the default ClientIDMode property for page is Autoid, you can set page-level values through the @ Page directive, and you can also set application-level values by modifying the Web configuration file.
Copy Code code as follows:

<system.web>
<pages clientidmode= "Predictable" ></pages>
</system.web>

2, meta keywords and meta description
In asp.net 4.0, the page class adds two new properties: Meta keywords and meta Description, which can be set at run time by a database or other source driver, and allows you to dynamically set tags, describe specific pages, The following page label shows the two properties.
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" keywords= "Keyword1, Keyword2"
Description= "My Description"%>

3. Row persistence selection in data-bound controls
Asp. NET data-bound control, grid View supports row selection, but they should select the same numbered row on each page, but in the previous version of ASP.net 4.0, row persistence selection is not possible because the previous version selects rows on subsequent pages based on the row index, asp.net 4.0 provides an intuitive way to solve this problem.
The data-bound control now provides a Enablepersistedsection property that can help me implement a row persistence selection, and the following code shows the list view control using the Enablepersistedselection property.
Copy Code code as follows:

<asp:listview id= "topranks" runat= "Server" enablepersistedselection= "True" datasourceid= "Dsranks" DataKeyNames= " Rankid ">
<ItemTemplate>
...
</ItemTemplate>
<SelectedItemTemplate>
...
</SelectedItemTemplate>
</asp:ListView>

4, AutoEventWireup
AutoEventWireup is a asp.net attribute that is rarely used but highly well-known, and simply, when set to True, allows you to automatically invoke page events without explicitly delegating, and the following code fragment shows the use of the AutoEventWireup property.
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" ...%>
Its default value is the disadvantage of the True,autoeventwireup attribute, which is described in detail on MSDN: "It limits your flexibility in naming event handlers, and another disadvantage is the negative impact on performance, which is a huge performance impact for high-traffic sites."
5, Page Header property
The page class now provides the header property, which can be bound at run time, and the following code example shows how to explicitly set the title property.
This. Header.title = "my page Title";
This property is handy when you dynamically associate a style sheet with a rule, in which case the printed page is the ideal candidate.
Copy Code code as follows:

Htmllink Printlink = new Htmllink ();
PRINTLINK.ATTRIBUTES.ADD ("type", "text/css");
PRINTLINK.ATTRIBUTES.ADD ("rel", "stylesheet");
PrintLink.Attributes.Add ("href", "css/print.css");
This. HEADER.CONTROLS.ADD (Printlink);



6, Associatedcontrolid property
You can associate a control with another server control in a Web form, and you need to use the Associatedcontrolid property of the server control, which can come in handy when you want to set a hotkey for the associated control based on some behavior.
Copy Code code as follows:

<asp:label id= "Lblusername" associatedcontrolid= "txtUserName" runat= "Server" text= "User Name:"/>
<asp:textbox id= "txtUserName" runat= "Server"/>

The default value of the Associatedcontrolid property is an empty string that indicates that the control is not associated with any server control, and the following code shows how a TextBox control is associated with a label server control.
7, ControlState property
Asp. NET's most important state management technology is viewstate, which allows you to preserve values on the way to and from the Web server, but it is not a reliable way to save information because it can be closed at the parent level.
ASP.net 2.0 introduces a private viewstate for server controls, called ControlState, which can be used to store key information about the control, ASP. NET can handle its serialization and deserialization.
Note that you must use caution because it affects the performance of your page.
8, Control.preserveproperty
For traditional view state usage, Rick Strahl offers us another option: preservedproperties, which saves control IDs and property names, for more information, refer to implementing an ASP.net Preserveproperty control (implements asp.net Preserveproperty controls) ".
9. Browser-based properties?
ASP.net 2.0 gives us a way to specify a browser filter for the attribute, and when I was puzzled, I happened to visit Ryan Farley's blog, saying he was as confused as when he saw John Katsiotis's blog.
In fact, you can set different values for a property based on a different browser, see the following example (code from Ryan Farley's blog).
Copy Code code as follows:

ie:onclientclick= "Javascript:alert (' Hello ie! ');" mozilla:text= "FF button"
mozilla:onclientclick= "Javascript:alert (' Hello firefox! ');"
text= "General button" onclientclick= "Javascript:alert (' Hello everyone else! ');" />

Interesting, isn't it?
10, PreviousPageType instructions
The PreviousPageType directive is part of the ASP.net 2.0 cross page loopback mechanism, allowing you to specify the virtual path of the source page for strongly typed access to the source page. Normally, the data sent can be accessed through the PreviousPage property and the FindControl method, but using a strongly typed PreviousPageType directive allows you to access public properties without invoking the FindControl method.
For example, suppose you have a page called firstpage.aspx, which has a public property firstproperty, and now in your second page (secondpage.aspx), you can add the following code:
<%@ previouspagetype virtualpath= "firstpage.aspx"%>
 
Then call the properties of the first page
var firstpageproperty = Previouspage.firstproperty;
What, are you familiar with the use of these attributes?
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.