In this article I am explaining the ASP.Net Server Tags

 

Embedded Code Blocks ( <% %> )

Embedded Code blocks allows us to write ASP.Net code within the aspx page using the Server Tags

<% %>.

C#

<%@ Page Language="C#"%>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Demo</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <%for(int i=0;i<3;i++){ %>

            <p>Hello</p>

        <%} %>

    </div>

    </form>

</body>

</html>

 

VB.Net

<%@ Page Language="VB"%>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Demo</title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <%For i As Integer = 1 To 3%>

                <p>Hello</p>

            <%Next%>

        </div>

    </form>

</body>

</html>

 

As you can see above in the above code I have used Server Tags to run  for loop which prints Hello 3 times. You can refer the output in the fugure below.

Embedded Code Blocks

Displaying from ASP.Net ( <%= %> )

 

Also you can use embedded server tags to print variables and also call functions that return value when equal to operator is used <%= expression%>. It is generally a short hand for Response.Write statement

Printing Variables

C#

<%@ Page Language="C#"%>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Demo</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>Demo</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>

 

The above code simply prints the variable. You can refer the output in the figure below.

Displaying from ASP.Net

Calling Functions

C#

<%@ Page Language="C#"%>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Demo</title>

      <script runat = "server">

          protected string GetTime()

          {

              return DateTime.Now.ToShortTimeString(); 

          }

    </script>

</head>

<body>

    <form id="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>Demo</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>

 

As you will notice above I am simply calling the GetTime() function using the Server Tags. The output is shown in the figure below.

Displaying from ASP.Net

Data-Binding Expressions ( <%# %> )

The Server Tags with a hash are used to bind data with multiple rows or items to a data source control like Repeater, GridView, FormView, DataGrid, etc.

C#

<%@ Page Language="C#"%>

<%@ Import Namespace ="System.Data" %>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

    <title>Demo</title>

      <script runat = "server">

          protected void Page_Load(object sender, EventArgs e)

          {

              DataTable dt = new DataTable();

              dt.Columns.Add("Names", Type.GetType("System.String"));

              dt.Rows.Add();

              dt.Rows[0]["Names"] = "Josh";

              dt.Rows.Add();

              dt.Rows[1]["Names"] = "Smith";

              dt.Rows.Add();

              dt.Rows[2]["Names"] = "John";

              dt.AcceptChanges();

              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>Demo</title>

   <script runat = "server">

   Protected Sub Page_Load(ByVal sender As Object,ByVal e As System.EventArgs)

   Handles Me.Load

            Dim dt As New DataTable()

            dt.Columns.Add("Names", Type.[GetType]("System.String"))

            dt.Rows.Add()

            dt.Rows(0)("Names") = "Josh"

            dt.Rows.Add()

            dt.Rows(1)("Names") = "Smith"

            dt.Rows.Add()

            dt.Rows(2)("Names") = "John"

            dt.AcceptChanges()

            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>

 

Here I first created a new DataTable object then added column called Names to it, after that I added three rows in order to add names of three persons to the DataTable.

Then I assigned the datatable to a Repeater control which finally displayed the same. You will notice I have used Eval statement to bind the data. You can also use Bind instead of Eval the only difference is Bind can be used to get as well as update data hence generally Bind is used with Textbox controls

The output is shown in the figure below.

DataBinding ASP.Net

ASP.Net Expressions ( <%$ %> )

   

The $ prefix expressions for AppSettings, ConnectionStrings, or Resources. The format of the Expression is as follows

<$ Type of expression : Value%>

The type can be AppSettings, ConnectionStrings, or Resources based what you want to assign

<asp:SqlDataSource ID="SqlDataSource1" Runat="server"

   SelectCommand="SELECT * FROM [Customers]"

   ConnectionString="<%$ ConnectionStrings:ConString %>">

</asp:SqlDataSource>

 

As you can see above I have assigned connection string value to data source using the ASP.Net Expressions

 

Server Side Comments ( <%-- --%> )

 The <%-- --%>  Tags are used for specifying Server side comments in aspx page.

The syntax is as follows

<%-- Content to be Commented --%>

 

<form id="form1" runat="server">

<div>

    <%--

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    --%>

</div>

</form>

 

As you will notice above I have commented a textbox control in the aspx page.Note when a control is commented it is not rendered.


This completes this article on Inline ASP.Net Server Tags.