Saturday, November 5, 2011

Inline ASP.Net Tags, Code Render Blocks... (<%$, <%=, <%, <%#, etc.)

Code render blocks define inline code or inline expressions that execute when the page is rendered. There are two styles of code render blocks: inline code and inline expressions.
Use inline expressions as a shortcut for calling the Write method.
<% inline code %>
<%=inline expression %>

thee tags are evaluated during the Render portion of the page load cycle.

<% ... %>


The most basic inline tag, basically runs normal code:

<% if (User.IsInRole("admin")) { %> 
You can see this 
<% } else { %> 
You are no admin fool! 
<%} %>

http://msdn2.microsoft.com/en-us/library/ms178135(vs.80).aspx

<%= ... %>

Used for small chunks of information, usually from objects and single pieces of information like a single string or int variable:
The Date is now <%= DateTime.Now.ToShortDateString() %>

The value of string1 is <%= string1 %>

http://msdn2.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx


*note: <%= is the equivalent of Response.Write().

<%# .. %>

Used for Binding Expressions; such as Eval and Bind, most often found in data controls like GridView, Repeater, etc.:


<asp:Repeater ID="rptMeetings" DataSourceID="meetings" runat="server"> 
<ItemTemplate> 
<%# Eval("MeetingName") %> 
</ItemTemplate> 
</asp:Repeater>



http://msdn2.microsoft.com/en-us/library/ms178366.aspx
<%$ ... %>
Used for expressions, not code; often seen with DataSources:


<asp:SqlDataSource ID="party" runat="server" ConnectionString="<%$ ConnectionStrings:letsParty %>" SelectCommand="SELECT * FROM [table]" />

http://msdn2.microsoft.com/en-us/library/d5bd1tad.aspx

<%@ ... %>

This is for directive syntax; basically the stuff you see at the top your your aspx pages like control registration and page declaration:


<%@ Page Language="C#" MasterPageFile="~/MasterPage.master"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %> 
<%@ Register TagPrefix="wp" Namespace="CustomWebParts" %>

http://msdn2.microsoft.com/en-us/library/xz702w3e(VS.80).aspx

<%-- ... --%>

This is a server side comment, stuff you don't want anyone without code access to see:

<asp:Label ID="lblAwesome" runat="server" /> 
<%-- sometimes end users make me angry --%> 
<asp:LinkButton ID="lbEdit" Text="Edit" OnClick="Edit_Click" runat="server" />
http://msdn2.microsoft.com/en-us/library/4acf8afk.aspx

No comments :

Post a Comment