Click here to Skip to main content
Licence CPOL
First Posted 24 Aug 2011
Views 9,286
Bookmarked 29 times

10 Most Useful Properties in ASP.NET

By | 24 Aug 2011 | Article
10 useful properties in ASP.NET, maybe most useful for some of you

Introduction

As programmers/developers, we work with different ASP.NET controls almost every single day. Here I want to share 10 useful properties in ASP.NET, maybe they will be useful for some of you.

1. ClientIDMode

While rendering ASP.NET controls, an ID will be generated automatically. When we cite them into client script, troubles come together. Although, it's a connect between naming container and ID, it still can't predict the ID's scope.

ASP.NET 4.0 solved this problem by using ClientIDMode, which enables you control these generated IDs. ClientIDMode has 4 optional values, AutoID, Static, Predictable and Inherit.

  • AutoID: Same as the version before 4.0, automatically generate ID
  • Static: Specify ID value, when render control, it won't change
  • Predictable: Specify suffix and merge with ID attribute of container control
  • Inherit: Inherit parent control's setting

Note: Page's default ClientIDMode property value is AutoID which can set page level value through @ Page command. You can also modify Web configure file to set project level value.

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

2. Meta Keywords and Meta Description

In ASP.NET 4.6, page class, 2 new properties added: Meta Keywords and Meta Description which can be set in working process. Through database or other source drivers, it allows dynamic set tags, describes special pages. The page tags below show the 2 properties:

<%@ Page Language=
  "C#" AutoEventWireup=
  "true" Keywords=
  "keyword1, keyword2" Description=
  "my description" %>"C#"
  AutoEventWireup=
  "true"
  Keywords=
  "keyword1, keyword2"
  Description=
  "my description"
  %>"C#"
  AutoEventWireup=
  "true"
  Keywords=
  "keyword1, keyword2"
  Description=
  "my description"
  %>

3. Rows in Data-bound Controls Persistent Selection

ASP.NET Data-bound controls, such as grid View, support rows selection. But they should select rows with the same number in each page. Before ASP.NET 4.0, it was not available for persistent selection. In ASP.NET 4.0, this is available.

Data-bound controls offer EnablePersisted property, which can help us achieve persistent selection. The code below shows List View control which used EnablePersistedSelection property.

<asp:ListView runat=
  "server" EnablePersistedSelection=
  "True" DataSourceID=
  "dsRanks" DataKeyNames=
  "rankid" >"server"
  EnablePersistedSelection=
  "True"
  DataSourceID=
  "dsRanks"
  DataKeyNames=
  "rankid"
  >

4. AutoEventWireup

AutoEventWireup, simply speaking, when set as True, in not explicitly assigned situation, allows you automatically invoke page event. The code below shows the usage of AutoEventWireup property.

  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" …. %>

5. Page Header Property

Page class now offers Header property which can bind it in working process. The code below shows how to set Title property.

 this.Header.Title = "My page title";

When you dynamically associate a style sheet according to some rules, it's very convenient. Print page is one of the ideal choices.

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

You can associate a control to another server control in Web form. And this needs to use server control's AssociatedControlID property. When you want to set hotkeys for associated controls, you need this property.

The default value of AssociatedControlID property is an empty string which means controls are not associated. The code below shows how a Textbox control is associated with Label server control.

  <asp:label AssociatedControlID=
  "txtUserName"
  runat=
  "server"
  Text=
  "User name:"
  />"txtUserName"
  runat=
  "server"
  Text=
  "User name:"
  />

7. ControlState

The most important state management technique of ASP.NET is ViewState, which enables you to keep value in Web server. But it's not a reliable method because it can be shut off from parent.

ASP.NET 2.0 added private ViewState for server control, called ControlState which can be used for storing important information. ASP.NET can serialize and de-serialize it.

Note: It may affect page performance.

8. Control.PreserveProperty

Against traditional view state usage, Rick Strahl offers another choice for us: PreservedProperties.

9. Browser-based Property

We can set different values for property according to different browsers. Please check the code below:

  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
  !
  ");"
  />

10. PreviousPageType Command

PreviousPageType command is a part of ASP.NET 2.0 Cross-page postback mechanism. In normal, data which has been sent can be visited through PreviousPage property and FindControl method. But, use PreviousPageType command allows you to visit public property and you don't need to invoke FindControl method.

For example, suppose we have a page named firstpage.aspx, which has a public property FirstProperty. Now, in our second page (secondpage.aspx), we can add the code below:

  <%@ PreviousPageType VirtualPath=
  " firstpage.aspx" %>" firstpage.aspx"
  %>" firstpage.aspx"
  %>

Then, invoke the first page property:

var firstPageProperty = PreviousPage.FirstProperty;

There are many types of ASP.NET controls and each control property is different from others. Wish this article is useful for you.

Again, I would like to introduce you to two more articles:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

loveyou999


e-iceblue
United States United States

Member

Follow on Twitter Follow on Twitter


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 3 Pinmemberandy(-RKO)23:47 29 Feb '12  
GeneralMy vote of 3 PinmemberIan4eto17:38 13 Sep '11  
QuestionThe default value of the Page ClientID is Predictable, not AutoID PinmemberIan4eto17:24 13 Sep '11  
QuestionEnjoyed reading. PinmemberNurul Howlader12:40 25 Aug '11  
GeneralMy vote of 3 PinmemberErik Anderson5:30 25 Aug '11  
QuestionUseful but please fix the formatting Pinmemberspoodygoon3:22 25 Aug '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 25 Aug 2011
Article Copyright 2011 by loveyou999
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid