Sunday, February 1, 2009

ASP.Net Web Forms Syntax

Following are some useful syntax to write ASP.Net/C# code in Html content:

1) Rendering Code Syntax: <% %> and <%= %>

The following example demonstrates the usage of above syntax:



<% for (int i=0; i<8; i++) { %>
<font size="<%=i%>"> Hello World! </font> <br>
<% } %>



Code enclosed by <% ... %> is just executed, while expressions that include an equal sign, <%= ... %>, are evaluated and the result is emitted as content. Therefore <%="Hello World" %> renders the same thing as the C# code <% Response.Write("Hello World"); %>.

Note: In case of C# language it is important to place semicolon (;) correctly to end or separate statements.



2) Data Binding Syntax: <%# %>

Code located within a <%# %> code block is only executed when the DataBind method of its parent control container is invoked. The following example demonstrates how to use the data binding syntax within an <asp:datalist runat=server> control.

Within the datalist, the template for one item is specified. The content of the item template is specified using a data binding expression and the Container.DataItem refers to the data source used by the datalist MyList.


<asp:datalist id="MyList" runat=server>
<ItemTemplate>
Here is a value: <%# Container.DataItem %>
</ItemTemplate>
</asp:datalist>



In this case the data source of the MyList control is set programmatically, and then DataBind() is called.


void Page_Load(Object sender, EventArgs e) {
ArrayList items = new ArrayList();

items.Add("One");
items.Add("Two");
items.Add("Three");

MyList.DataSource = items;
MyList.DataBind();
}



Calling the DataBind method of a control causes a recursive tree walk from that control on down in the tree; the DataBinding event is raised on each server control in that hierarchy, and data binding expressions on the control are evaluated accordingly. So, if the DataBind method of the page is called, then every data binding expression within the page will be called.



3) Server-Side Comment Syntax: <%-- Comment --%>

The following sample demonstrates how to block content from executing and being sent down to a client.



<%--
<asp:calendar id="MyCal" runat=server/>
<% for (int i=0; i<45; i++) { %>
Hello World <br>
<% } %>
--%>






4) Server-Side Include Syntax: <-- #Include File="Locaton.inc" -->

Server-side #Includes enable developers to insert the raw contents of a specified file anywhere within an ASP.NET page. The following sample demonstrates how to insert a custom header and footer within a page.


<!-- #Include File="Header.inc" -->
...
<!-- #Include File="Footer.inc" -->





5) Expression Syntax: <%$ ... %> (New in ASP.Net 2.0 )

ASP.NET 2.0 adds a new declarative expression syntax for substituting values into a page before the page is parsed. This is useful for substituting connection string values or application settings defined in a Web.config file for server control property values. It can also be used to substitute values from a resource file for locaization.


<asp:SqlDataSource ID="SqlDataSource1"
ConnectionString='<%$ connectionStrings:Pubs %>'
runat="server" SelectCommand="sp_GetAuthors" />

<asp:Label ID="Label1"
Text='<%$ Resources: ExchRate, ConvertLabel %>'
runat="server"/>





To see the practical usage of above syntax you can refer following post:
Custom paging in ASP.Net GridView control


Reference:
Web Forms Syntax Reference

Google