{"id":660,"date":"2011-09-15T19:33:33","date_gmt":"2011-09-15T19:33:33","guid":{"rendered":"http:\/\/dirk.schuermans.me\/?p=660"},"modified":"2016-01-07T10:46:33","modified_gmt":"2016-01-07T10:46:33","slug":"asp-net-inline-tags","status":"publish","type":"post","link":"https:\/\/dirk.schuermans.me\/?p=660","title":{"rendered":"ASP.NET inline tags"},"content":{"rendered":"<p>Hey everyone!<\/p>\n<p>Recently I&#8217;ve started on my &#8220;Professional ASP.NET 4 in C# (and VB)&#8221; book to get my knowledge of ASP.NET up.<\/p>\n<p>One of the things I&#8217;ve noticed was that nowhere in the first chapter(s) they explained the difference between all the ASP.NET inline tags (such as &lt;% &#8230; %&gt;, &lt;%$ &#8230; %&gt;, etc)<\/p>\n<p>Therefor I&#8217;ve decided to put together this small overview of all the different inline tags and what they do. (Yes, it&#8217;s for my convenience too).<\/p>\n<p>This post will cover the following inline tags:<\/p>\n<ol>\n<li>&lt;% &#8230; %&gt;<\/li>\n<li>&lt;%= &#8230; %&gt;<\/li>\n<li>&lt;%# &#8230; %&gt;<\/li>\n<li>&lt;%&#038; &#8230; %&gt;<\/li>\n<\/ol>\n<p>I think it&#8217;s obvious what the &lt;%@ &#8230; %&gt; tags do \ud83d\ude09<br \/>\nThe &lt;%&#8211; &#8230; &#8211;&gt; tags serve as server-side comments. Meaning those comments will not be visible in the HTML source code.<\/p>\n<p><!--more--><\/p>\n<h3>The &#8220;&lt;% &#8230; %&gt;&#8221; tags<\/h3>\n<p>This one is probably the most used inline ASP tag there is.<br \/>\nThis tag serves the purpose of an <strong>embedded code block<\/strong>. It allows a developer to use code (C#\/VB) in between normal HTML code.<\/p>\n<p>An example:<\/p>\n<pre name=\"code\" class=\"html\">\r\nHello world!&lt;br \/&gt;\r\nIt is\r\n&lt;% if(DateTime.Now.Hour &lt; 6 &amp;&amp; DateTime.Now.Hour &gt;= 0){ %&gt;\r\nnight now!\r\n&lt;% } else { %&gt;\r\nday now!\r\n&lt;% } %&gt;\r\n<\/pre>\n<h3>The &#8220;&lt;%= &#8230; %&gt;&#8221; tags<\/h3>\n<p>These tags allow the developer to display something, for example a variable declared in the code behind file, on the page.<\/p>\n<p>They can also be used to display properties of .NET object, such as DateTime:<\/p>\n<pre name=\"code\" class=\"html\">\r\nHello world!&lt;br \/&gt;\r\nThe time now is &lt;%= DateTime.Now.ToString() %&gt;&lt;br \/&gt;\r\n<\/pre>\n<h3>The &#8220;&lt;%# &#8230; %&gt;&#8221; tags<\/h3>\n<p>These tags are used for <strong>data binding<\/strong> in databound controls, such as the ListBox, DropDownList, &#8230;<br \/>\nThe <strong>&#8220;Page.DataBind()&#8221;<\/strong> must be called first, all &lt;%# &#8230; %&gt; tags will be evaluated then.<\/p>\n<p>An example that will display the text the user has inputted in another label:<\/p>\n<pre name=\"code\" class=\"html\">\r\n&lt;form id=\"form1\" runat=\"server\"&gt;\r\n\tEnter some text:\r\n\t&lt;asp:TextBox ID=\"TextBox1\" runat=\"server\"&gt;&lt;\/asp:TextBox&gt;&lt;br \/&gt;\r\n\t&lt;br \/&gt;\r\n\t&lt;asp:Button ID=\"Button1\" runat=\"server\" OnClick=\"Button1_Click\" \/&gt;&lt;br \/&gt;\r\n\t&lt;br \/&gt;\r\n\t&lt;asp:Label ID=\"Label1\" runat=\"server\" Text=\"&lt;%# TextBox1.Text %&gt;\"&gt;&lt;\/asp:Label&gt;\r\n&lt;\/form&gt;\r\n<\/pre>\n<p>And then in the code behind:<\/p>\n<pre name=\"code\" class=\"csharp\">\r\nprotected void Button1_Click(object sender, EventArgs e)\r\n{\r\n\tPage.DataBind();\r\n}\r\n<\/pre>\n<h3>The &#8220;&lt;%$ &#8230; %&gt;&#8221; tags<\/h3>\n<p>These tags define an expression of one of the following types:<\/p>\n<ul>\n<li>AppSettings<\/li>\n<li>ConnectionStrings<\/li>\n<li>Resources<\/li>\n<\/ul>\n<p>By using this tag you can gain access to properties or resources within your ASP application.<br \/>\nYou could for instance, retrieve a connection string that has been defined inside your web.config file:<\/p>\n<pre name=\"code\" class=\"html\">\r\n&lt;asp:SqlDataSource ID=\"SqlDataSource1\" runat=\"server\"\r\n\tConnectionString=\"&lt;%& ConnectionStrings:myCon %&gt;\"\r\n\tSelectCommand=\"SELECT * FROM Customers\"&gt;\r\n&lt;\/asp:SqlDataSource&gt;\r\n&lt;asp:CheckBoxList ID=\"CheckBoxList1\" runat=\"server\"\r\n\tRepeatDirection=\"Vertical\" RepeatColumns=\"3\"\r\n\tDataSourceID=\"SqlDataSource1\" DataTextField=\"CustomerName\"&gt;\r\n&lt;\/asp:CheckBoxList&gt;\r\n<\/pre>\n<p>Mind the &#8220;ConnectionString&#8221; attribute on the SqlDataSource!<\/p>\n<p>There we go, all inline tags explained!<\/p>\n<p>Happy coding \\o\/<\/p>\n<p>References:<\/p>\n<ul>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-gb\/library\/ms178135(vs.80).aspx\">Embedded code blocks (&lt;% &#8230; %&gt;)<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6dwsdcf5(VS.71).aspx\">Displaying something (&lt;%= &#8230; %&gt;)<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bda9bbfx.aspx\">Data binding (&lt;%# &#8230; %&gt;)<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/d5bd1tad.aspx\">Expressions (&lt;%&#038; &#8230; %&gt;)<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hey everyone! Recently I&#8217;ve started on my &#8220;Professional ASP.NET 4 in C# (and VB)&#8221; book to get my knowledge of ASP.NET up. One of the things I&#8217;ve noticed was that nowhere in the first chapter(s) they explained the difference between all the ASP.NET inline tags (such as &lt;% &#8230; %&gt;, &lt;%$ &#8230; %&gt;, etc) Therefor &hellip; <a href=\"https:\/\/dirk.schuermans.me\/?p=660\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ASP.NET inline tags<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[38],"tags":[],"_links":{"self":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/660"}],"collection":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=660"}],"version-history":[{"count":38,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/660\/revisions"}],"predecessor-version":[{"id":715,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=\/wp\/v2\/posts\/660\/revisions\/715"}],"wp:attachment":[{"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dirk.schuermans.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}