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:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...