ASP.NET offers a variety of ways to inject the results of a server-side expression (such as DateTime.Now.ToString()) into the rendered markup of an ASP.NET page.
The most common way is to add a Label Web control to the page and then from the Page_Load event handler (or some other suitable event handler) assign the value
to display to the Label's Text property. If you ever created web applications using ASP.NET's predecessor, classic ASP, or if you are familiar with
Microsoft's ASP.NET MVC, then you know that another way to inject server-side information is to add <%= expression %> to the declarative content like so:
The current time is: <%= DateTime.Now.ToString() %> |
The <%= expression %> syntax is translated into Response.Write(expression), injecting the value of expression into the
page's rendered output. Because <%= expression %> is translated into (essentially) a Response.Write these statements cannot be used to set the values
of Web control properties. In other words, you cannot have markup like the following:
<asp:Label runat="server" id="CurrentTime" Text="<%= DateTime.Now.ToString() %>" /> |
An alternate way to display server-side information is to assign it to a Web control property directly from the declarative markup using an expression builder. An
expression builder is denoted using the syntax <%$expression %> (note the $ after <%).
The expression cannot be an arbitrary snippet of code as with <%= ... %>, but instead is limited to what expression builders the website is configured
to use. Moreover, expression builders must be assigned to a Web control property; they cannot appear in any which place in the declarative markup like <%= expression %>.
ASP.NET ships with three built-in expression builder classes that let you declaratively access the values in the configuration's AppSettings collection, the ConnectionStrings collection, and in the website's resources (typically defined in the App_LocalResources and App_GlobalResources folders).
With a little bit of code you can create your own expression builders. This article provides an overview of how expression builders work and shows how to create your
own expression builders. Read on to learn more!
Read More >
0 comments:
Post a Comment