10 most useful attributes of ASP. NET controls

Source: Internet
Author: User

1. ClientIDMode
Rendering ASP. NET controls automatically generate an ID. When we reference them in client scripts, it creates a lot of trouble. Although it is a simple series of named containers and IDs, however, the range of IDS generated cannot be predicted.
ASP. NET 4.0 solves this problem by using the ClientIDMode attribute, which allows you to control the methods for generating these IDs. ClientIDMode has four optional values: AutoID, Static, Predictable, and Inherit. The meanings of these four values are as follows:
The AutoID-is consistent with the version earlier than 4.0, and an ID is automatically generated.
Static-the value of the ID you specify does not change when rendering the control.
Predictable-you specify the suffix and merge it with the ID attribute of the Container Control.
Inherit-inherits the settings of the parent control.
Note: The default ClientIDMode attribute value of Page is AutoID. You can set the Page-level value through the @ Page command. You can also set the application-level value by modifying the Web configuration file. Copy codeThe Code is as follows: <system. web>
<Pages clientIDMode = "Predictable"> </pages>
</System. web>

2. Meta keywords and Meta description
In ASP. in NET 4.0, the Page class adds two new attributes: Meta Keywords and Meta Description. You can set these two attributes at runtime through the database or other source drivers, and allows you to dynamically set tags to describe specific pages. The following Page labels show these two attributes.Copy codeThe Code is as follows: <% @ Page Language = "C #" AutoEventWireup = "true" Keywords = "keyword1, keyword2"
Description = "my description" %>

3. Row persistence selection in the data binding control
ASP. NET data binding controls, such as Grid View, all support row selection, but they should select rows with the same number on each page, but in ASP. in versions earlier than NET 4.0, row persistence selection cannot be implemented, because the previous version selects the rows on the subsequent page based on the row index, ASP. NET 4.0 provides an intuitive way to solve this problem.
The data binding control now provides an EnablePersistedSection attribute, which helps me implement row persistence selection. The following code shows the List View control using the EnablePersistedSelection attribute.Copy codeThe Code is as follows: <asp: ListView ID = "topRanks" runat = "server" EnablePersistedSelection = "True" performanceid = "dsRanks" DataKeyNames = "rankid">
<ItemTemplate>
...
</ItemTemplate>
<SelectedItemTemplate>
...
</SelectedItemTemplate>
</Asp: ListView>

4. AutoEventWireup
AutoEventWireup is a rarely used but well-known ASP. NET Property. To put it simply, when it is set to True, you are allowed to automatically call page events without explicit delegation. The following code snippet shows how to use the AutoEventWireup property.
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs ".... %>
Its default value is True. The disadvantage of the AutoEventWireup attribute is described in detail on MSDN: "It limits the flexibility of your named event handler. Another drawback is its adverse impact on performance, for high-traffic websites, performance is greatly affected ".
5. Page Header attributes
The Page class now provides the Header attribute, which can be bound at runtime. The following code example shows how to explicitly set the Title attribute.
This. Header. Title = "My page title ";
When you dynamically associate a style sheet according to a rule, this attribute is very convenient. In this case, printing pages is an ideal candidate.Copy codeThe Code is 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 attributes
You can associate a control with another server control in a Web form. In this case, you need to use the AssociatedControlID attribute of the server control. When you want to set a hotkey for the associated control based on some behaviors, this attribute can be used.Copy codeThe Code is 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 attribute is an empty string, indicating that the control is not associated with any server control. The following code shows how a Textbox Control is associated with a Label Server Control.
7. ControlState attributes
ASP. NET's most important state management technology is ViewState, which allows you to retain the value on the way to and from the Web server, but because it can be disabled at the parent level, it is not a reliable method to save information.
ASP. NET 2.0 introduces a private ViewState for the server control, called ControlState, which can store key information of the Control. ASP. NET can process its serialization and deserialization.
Note that you must be cautious when using the service because it affects the page performance.
8. Control. PreserveProperty
For the traditional view State usage, Rick Strahl provides us with another option: PreservedProperties, which can save the Control ID and attribute name. For details, see "Implementing an ASP. NET PreserveProperty Control (ASP. NET PreserveProperty Control )".
9. browser-based attributes?
ASP. NET 2.0 provides us with a way to specify a browser filter for attributes. While I am confused, I happened to visit Ryan Farley's blog, he said he was as confused as he saw John Katsiotis's blog.
In fact, you can set different values for attributes based on different browsers. See the example below (the code is from Ryan Farley's blog ).Copy codeThe Code is 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, right?
10. PreviousPageType command
The PreviousPageType command is part of the cross-page delivery mechanism of ASP. NET 2.0. It allows you to specify the virtual path of the Source Page so that you can access the source page with a strong type. Normally, the sent data can be accessed through the PreviousPage attribute and FindControl method. However, the PreviousPageType command of the strong type allows you to access public attributes without calling the FindControl method.
For example, if you have a page named firstpage. aspx, which has a public attribute FirstProperty, you can add the following code in your second page (secondpage. aspx:
<% @ PreviousPageType VirtualPath = "firstpage. aspx" %>
 
Then, call the attributes of the first page.
Var firstPageProperty = PreviousPage. FirstProperty;
How are you familiar with the usage 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.