Sunday, August 29, 2010

Asp.net expressions overview and comparison

 <%# ... %> Executes at the call to DataBind() method.
<%= ... %> Not valid for server controls, will not get executed for server controls. But when place in the mark-up, value get rendered to the output html stream.
<%$ Prefix:Value %> Before page init/At the point of execution.

Example:

Web.condig:
  <appSettings>
    <add key="Label" value="Expressions"/>
  </appSettings>

Page:
<%@ Page Language="C#" %>
<html>
<head id="Head1" runat="server">
    <script runat="server">
        public string Label = "Expressions";
        string a, b, c;
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            a = this.txtNameA.Text; /// Expressions
            b = this.txtNameB.Text; /// ""
            c = this.txtNameC.Text; /// <%= this.Label %>
        }
        protected override void OnLoad(EventArgs e)
        {
            a = this.txtNameA.Text; /// Expressions
            b = this.txtNameB.Text; /// ""
            c = this.txtNameC.Text; /// <%= this.Label %>
            base.OnLoad(e);
            this.DataBind();
            a = this.txtNameA.Text; /// Expressions
            b = this.txtNameB.Text; /// Expressions
            c = this.txtNameC.Text; /// <%= this.Label %>
        }
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            a = this.txtNameA.Text; /// Expressions
            b = this.txtNameB.Text; /// Expressions
            c = this.txtNameC.Text; /// <%= this.Label %>
        }        
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox runat="server" ID="txtNameA" Text="<%$ AppSettings:Label  %>" />
        <asp:TextBox runat="server" ID="txtNameB" Text="<%# this.Label %>" />
        <asp:TextBox runat="server" ID="txtNameC" Text="<%= this.Label %>" />
        <p><%= this.Label %></p>
    </form>
</body>
</html>

Output:

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...