ASP.NET control for the 10 most useful properties

  • 2020-05-12 02:29:29
  • OfStack

1, ClientIDMode
Rendering the ASP.NET control automatically generates an ID, which causes a lot of trouble when we refer to them in the client script. Although it is a simple concatenation of the named container and ID, it still does not predict the generated ID range.
ASP.NET 4.0 solves this problem using the ClientIDMode property, which allows you to control how these ID are generated. ClientIDMode has four optional values: AutoID, Static, Predictable, and Inherit. The following are the meanings of these four values:
The contents of AutoID and the previous 4.0 versions are kept at 1, and ID is automatically generated.
You specify the value of ID. It will not change when rendering the control.
You specify the suffix Predictable and merge it with the ID property of the container control.
Inherit inherited the Settings of the parent control.
Note that the default ClientIDMode property for Page is AutoID, which you can set at the page level via the @Page directive, and you can also set at the application level by modifying the Web configuration file.
 
<system.web> 
<pages clientIDMode="Predictable"></pages> 
</system.web> 

2. Meta keyword and Meta description
ASP.NET 4.0 adds two new properties to the Page class: Meta Keywords and Meta Description, which can be set at run time, via a database or other source driver, and allows you to dynamically set labels describing specific pages, as shown in the Page TAB below.
 
<%@ Page Language="C#" AutoEventWireup="true" Keywords="keyword1, keyword2" 
Description="my description" %> 

3. Row persistence selection in data bound controls
ASP. NET data-bound controls, such as Grid View, support line choice, but they should choose the same number on each page, but in ASP. NET 4.0 previous versions, persistence choice cannot be implemented, because previous versions choose row on the subsequent pages are based on the row index, ASP. NET 4.0 provides an intuitive method to solve the problem of the 1.
The data bound control now provides an EnablePersistedSection property that helps me with line persistence selection, and the code below shows the List View control using the EnablePersistedSelection property.
 
<asp:ListView ID="topRanks" runat="server" EnablePersistedSelection="True" DataSourceID="dsRanks" DataKeyNames="rankid"> 
<ItemTemplate> 
 ...  
</ItemTemplate> 
<SelectedItemTemplate> 
 ...  
</SelectedItemTemplate> 
</asp:ListView> 

4, AutoEventWireup
AutoEventWireup is a rarely used but well-known ASP.NET property. Simply put, when set to True, it allows you to automatically call page events without explicitly delegating them. The code snippet below shows the use of the AutoEventWireup property.
< % @Page Language="C#" AutoEventWireup="true" CodeFile=" Default.aspx.cs "... %. >
The default value is True, and the disadvantage of the AutoEventWireup property is described in detail on MSDN: "it limits the flexibility of your named event handler. Another disadvantage is the adverse impact on performance, which can be significant for high-traffic websites."
5. Header property of Page
The Page class now provides the Header property, which can be bound at run time, and the code example below shows how to explicitly set the Title property.
this.Header.Title = "My page title";
This property is handy when you are dynamically associating a style sheet according to a rule, in which case a printed page is an ideal candidate.
 
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 one control with another server control in one Web form. You need to use the AssociatedControlID property of the server control, which is useful when you want to set hotkeys for the associated control based on some behavior.
 
<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 indicating that the control is not associated with any server control. The code below shows how an Textbox control is associated with an Label server control.
7. ControlState property
ASP.NET's most important state management technique is ViewState, which allows you to keep values on your way to and from the Web server, but because it can be turned off at the parent level, it is not a reliable way to keep information.
ASP.NET 2.0 introduces the private ViewState, called ControlState, for server controls, which can be used to store critical information about the control, and ASP.NET to handle its serialization and deserialization.
Note that it must be used with caution, as it can affect the performance of the page.
8, Control PreserveProperty
For traditional use of view state, Rick Strahl provides us with another option: PreservedProperties, which can save the control ID and the property name. For details, please refer to "Implementing an ASP.NET PreserveProperty Control(implement ASP.NET PreserveProperty control)".
9. Browser-based properties?
ASP.NET 2.0 gives us a way to specify a browser filter for a property, and while I was confused, I happened to visit the Ryan Farley blog, and he said he was confused when he saw the John Katsiotis blog.
In fact, you can set different values for properties depending on the browser, as shown in the following example (code from Ryan Farley's blog).
 
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 instruction
The PreviousPageType directive is part of the ASP.NET 2.0 cross-page echo mechanism, which allows you to specify a virtual path to the source page for strongly typed access to the source page. Normally, the data sent can be accessed via the PreviousPage property and the FindControl method, but the strongly typed PreviousPageType directive allows you to access public properties without calling the FindControl method.
For example, if you have a page called firstpage.aspx, which has a public property FirstProperty, now on your second page (secondpage.aspx), you can add the following code:
< %@ PreviousPageType VirtualPath=" firstpage.aspx" % >

The properties of the first page are then called
var firstPageProperty = PreviousPage.FirstProperty;
So, are you familiar with the use of these properties?

Related articles: