Inline ASP.NET Tags

There are all sorts of different inline tags, and i haven't found a place that explains




They are all in ONE place, so here is the quick view...

<% ... %>

The most basic inline tag, basically runs normal code:
<% if (User.IsInRole("admin")) { %>
You can see this
<% } else { %>
You are no admin fool!
<%} %>






<%= ... %>

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 %>
*note: <%= is the equivalent of Response.Write() - Courtesy of Adam from the
US,thanks!









<%# .. %>

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>






<%$ ... %>

Used for expressions, not code; often seen with DataSources:
<asp:SqlDataSource ID="party" runat="server" ConnectionString="<%$ ConnectionStrings:letsParty%>"
SelectCommand="SELECT * FROM [table]" />






<%@ ... %>

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" %>






<%-- ... --%>

This is a server side comment, stuff you don't want anyone without code access to
see:
<asp:Label ID="lblAwesome" runat="server" />

<%-- sometimes i like to hide things --%>

<asp:LinkButton ID="lbEdit" Text="Edit" OnClick="Edit_Click" runat="server" />

And that's that.

Popular Posts