Wednesday, July 22, 2009

Data Binding Expressions and Their Symbols

All data Binding Expressions are evaluated at runtime. There are three ways I know to include data binding expressions in ASP.NET. I don't find that Microsoft has defined any specific terms to distinguish them. For clarify and easy discussion, I would define ones here so that I can discuss and compare them.

These three (3) ways data binding expressions are:


Page-Level Data Binding
<%#  ...  %>

Example:

 <%# Request.MapPath("mytext.txt") %>
 <%# GetUserName() %>
 <%# MyAddress %>
 <%# 1 + 2 * 3 %>

The expression will be evaluated when you call DataBind() of the Page class or any other controls that support it such as the GridView, DetailsView, and FormView.  This expression can be applied to a reference to another control's property, a value of an object property, a member variable or a return value of a function. It can be placed anywhere in the ASPX file whenever it fits. For example,

  <title>
    <%# PageTitle %>
  </title>

  <img src="<%# GetImageFile() %>" alt="" />

  <asp:TextBox ID="txtFirstName" 
               runat="server" Text="<%# GetFirstName() %>" />

  <asp:HyperLink ID="lnkExample" 
                 NavigateUrl="<%# Example.Link.Value %>" 
                 Text="Example" runat="server" />

Page-level data binding is very common and easy to use. It also comes with two methods Eval and Bind. Eval provides readonly capability on data while data with Bind is updatable.

 <%# Eval("LastName") %>
 <%# Bind("FirstName") %>

For the detail how to use them, you can read more on your own.

Control-Level Data Binding
<%$  ...  %>

Example:

 <%$ ConnectionStrings:Northwind %>
 <%$ AppSettings:MyVariable %>

The expression will be evaluated when the page is rendered. There is no need to call DataBind(), unlike the page-level.

In addition, control-level data binding cannot be placed anywhere in the page by itself. Instead, it must be wrapped inside a control tag. The result of the expression is used to set that control's property. For example,

  <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:Northwind %>"  ... /%>
where the <%$ and %> is used to extract the custom connection string from web.config.

Currently, there are only two build-in expressions available. One is to extract connection string from the web.config (see the above example) and the other is to extract application settings from web.config.

  <asp:Literal Text="<%$ AppSettings:MyVariable %>"  ... /%>

Although you can always make your own custom expressions through CodeDOM (Code Document Object Model), creating a custom expression to use is not an easy task.

Script-Level Data Binding
<%=  ...  %>

Example:

 <%= Request.MapPath("mytext.txt") %>
 <%= GetUserName() %>
 <%= MyAddress %>
 <%= 1 + 2 * 3 %>
 <%= myClass.Name %>
 <%= myClass.Execute %>

  <script language="javascript">
     location.href = <%= MyForwardPageUrl %>;
     ...
  </script>

Similar to the control-level, the expression will be evaluated when the page is rendered. Then, ASP.NET automatically inserts the value of the expression. There is no need to make any call to any DataBind() method either including Page.DataBind(). Similar to page-level, it can be placed anywhere in the markup page.

5 comments:

  1. script level data binding the url end with %%>; is the % comes in double?

    ReplyDelete
  2. It is a typo. All data binding expressions end with %>. It is fixed now. Thanks.

    location.href = <%= MyForwardPageUrl %>;

    ReplyDelete
  3. by the way, do u know about server and its service?I am desperately want to know about servers stuff..could not get suitable information. I am actually doing a project about services that going to be installed in 3 servers where the server OS are linux and Window 2003.Eg: i want to install email server in server 1, i dunno whether then i need to install DNS,DHCP,Web Server in the same server(server 1) or i need to install in different servers..@_@..

    ReplyDelete
  4. I am not sure if I fully understand what you're trying to accomplish. All daemons or services can be installed or turned on in a single box. A very small company usually likes to operate in this way. However, most companies (especially mid-size or big ones) will separate them in different machines for load distribution based on their infrastructure. Thus, how you want to implement them depends on your design and how you architect your network. I would advise you to take this to the networking forums for discussion. They may probably have a better advice and suggestions.

    ReplyDelete
  5. ok...thanks lots .I going to faint to design this..since is a self-learning, i not sure whether my concept for whole design will be accepted. Finding logic through internet..and bump on your nice blog..(applause)..:P
    thanks anyway..

    ReplyDelete