Saturday, May 24, 2014

ASP.Net Webforms Inline tags and code blocks

There are many elements inside a webforms (.aspx) page. To somebody beginning asp.net webforms these may seem quite similar and be confusing at first. (Well it was for me when I started learning asp.net webforms)
So hopefully this might help another soul.

Elements of a asp.net page (Inline tags and code blocks) can be listed as follows,

1. Directives

These come in many flavors like Page directives,
 ex : <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"  Trace="true" %>

or  Import directives,
ex : <%@ namespace="System.Drawing" %>

There are many more directives other than these two.

2. ASP.Net expressions

This is expression builder syntax, typically used to get values from web.config
ex: <%$ ConnectionStrings:AdventureWorks2008 %>  

3. User Controls

These are references to user created controls which are similar to web controls that enhances reusability 
ex: <PH:MainMenu Title="Menu Control" id="mainmenu" runat="server" />

4. Render blocks

Used for outputting elements like variables
ex:  If you have a variable named age then,
<%= age %>
will output the value of age

5. Code blocks

These are similar to javascript tags just that these run on the server,
<script runat="Server" language="c#" > ....some server side code here .... </script>  

6. Server Controls

These are the controls we drag and drop from the designer (Labels, Buttons etc)
ex: <asp:Label id="myLabel" runat="Server" />

7. Data binding controls

Used for biniding expressions such as Eval or Bind, these are typically used with data controls like Gridview
ex : <%# Eval("DBFieldName") %>