In this article I will explain with an example, how to use inline ASP.Net Server Tags.
Inline ASP.Net Server Tags are used with following types:
1. Embedded Code Blocks ( <% %> )
2. Displaying from ASP.Net ( <%= %> )
3. Data-Binding Expressions ( <%# %> )
4. ASP.Net Expressions ( <%$ %> )
5. Server Side Comments ( <%-- --%> )
1. Embedded Code Blocks ( <% %> )
Embedded Code blocks allows to write ASP.Net code within the aspx page using the server tags <% %>.
Following embedded code block is used for loop which prints text ASPSnippets.com three times.
C#
<%@ Page Language="C#"%>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%for (int i= 0; i<3; i++){ %>
<p>ASPSnippets.com</p>
<%} %>
</div>
</form>
</body>
</html>
VB.Net
<%@ Page Language="VB"%>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%For i As Integer = 1 To 3%>
<p>ASPSnippets.com</p>
<%Next%>
</div>
</form>
</body>
</html>
Output
2. Displaying from ASP.Net ( <%= %> )
For displaying from ASP.Net, inline server tags is used with equal-to operator i.e. <%= expression%>.It is generally a short hand for Response.Write statement.
It is used for printing variables and calling functions that returns a value.
Printing Variables
Following HTML Markup consists of variable set with a string value.
Then, the variable is printed using the inline server tags with equal-to operator.
C#
<%@ Page Language="C#"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
string myName = "Mudassar Khan";
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>My name is <%=myName%>.</p>
</div>
</form>
</body>
</html>
VB.Net
<%@ Page Language="VB"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
Dim myName As String = "Mudassar Khan"
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>My name is <%=myName%>.</p>
</div>
</form>
</body>
</html>
Output
Calling Functions
Following HTML Markup consists of a function that returns current server time.
The returned value from the function is printed using the inline server tags with equal-to operator.
C#
<%@ Page Language="C#"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
protected string GetTime()
{
return DateTime.Now.ToShortTimeString();
}
</script>
</head>
<body>
<formid="form1"runat="server">
<div>
<p>The current time is <%=GetTime()%>.</p>
</div>
</form>
</body>
</html>
VB.Net
<%@ Page Language="VB"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
Protected Function GetTime() As String
Return DateTime.Now.ToShortTimeString()
End Function
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>The current time is <%=GetTime()%>.</p>
</div>
</form>
</body>
</html>
Output
3. Data-Binding Expressions ( <%# %> )
The server tags with a hash (#) is used to bind data with multiple rows or items to a DataSource control like GridView, Repeater, FormView, DataList, etc.
Inside the HTML Markup, first the System.Data namespace is inherited.
Then an ASP.Net Repeater control is placed inside the form.
Finally, inside the PageLoad event handler, the Repeater is populated with some data.
C#
<%@ Page Language="C#"%>
<%@Import Namespace="System.Data" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Names");
dt.Rows.Add("Josh");
dt.Rows.Add("Smith");
dt.Rows.Add("John");
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Names</p>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<p style="color: Red"><%# Eval("Names")%></p>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
VB.Net
<%@ Page Language="VB"%>
<%@Import Namespace="System.Data" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As New DataTable()
dt.Columns.Add("Names")
dt.Rows.Add("Josh")
dt.Rows.Add("Smith")
dt.Rows.Add("John")
Repeater1.DataSource = dt
Repeater1.DataBind()
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Names</p>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<p style="color: Red"><%# Eval("Names")%></p>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
Output
4. ASP.Net Expressions ( <%$ %> )
The $ prefix expressions is used for AppSettings, ConnectionStrings, and Resources.
The syntax is as follows.
<%$ Type of expression : Value%>
Note: The Type can be AppSettings, ConnectionStrings, and Resources.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT * FROM [Customers]"
ConnectionString="<%$ ConnectionStrings:ConString %>">
</asp:SqlDataSource>
5. Server Side Comments ( <%-- --%> )
The <%-- --%> tags is used for specifying server side comments in aspx page.
The syntax is as follows.
<%-- Content to be Commented --%>
In the following HTML Markup, the TextBox control is commented.
Note:When a control is commented it is not rendered in the page.
<form id="form1" runat="server">
<div>
<%--
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
--%>
</div>
</form>