Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Code Expression, Data Binding, and other Declarative Expressoins

What are the differences in these tags?

<% <%# <%= <%$

More importantly, how do I display a page property using declarative syntax in an ASP.NET control? I'm trying to do this in an ASP.NET control. The task is to set the text of a label but I do not want to do this pro grammatically in the event I want to change the output control. I get an error about server side controls can't contain this syntax. I'm not sure that I need a databound control for what I want to do but that is another option.

Partial answer coming up.

Update There is a new tag I've seen in ASP.NET 4.5? site

<%:

like image 462
Curtis White Avatar asked Sep 16 '10 14:09

Curtis White


1 Answers

Partial answer

quoted from Mike Banavige

<% %> An embedded code block is server code that executes during the page's render phase. The code in the block can execute programming statements and call functions in the current page class. http://msdn2.microsoft.com/en-gb/library/ms178135(vs.80).aspx

<%= %> most useful for displaying single pieces of information. http://msdn2.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx

<%# %> Data Binding Expression Syntax. http://msdn2.microsoft.com/en-us/library/bda9bbfx.aspx

<%$ %> ASP.NET Expression. http://msdn2.microsoft.com/en-us/library/d5bd1tad.aspx

<%@ %> Directive Syntax. http://msdn2.microsoft.com/en-us/library/xz702w3e(VS.80).aspx

<%-- --%> Server-Side Comments. http://msdn2.microsoft.com/en-US/library/4acf8afk.aspx

Update:

Okay this appears to work

<asp:Label ID="MyLabel" runat="server" Text='<%# MyProperty%>'></asp:Label>

If I use the eval syntax then I get an error about databound control or I use the <% then I get a server side controls error. Any more color appreciated.. not sure I really understand what is going on.

Perhaps it has something to do with the render phase.

Few more observations:

I can use <%= without databinding and get the property value but can not use it in a server side control without getting error.

If I use <%# in server side control but I'm required to do a Page.Databind.

Interestingly, I can use either <%= or <%# when I want to render text that is not inside a control. Although the latter requires databinding.

The new <%: syntax is explained, also called code expression syntax

With ASP.NET 4 we are introducing a new code expression syntax (<%: %>) that renders output like <%= %> blocks do – but which also automatically HTML encodes it before doing so.

http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2

like image 154
Curtis White Avatar answered Nov 15 '22 03:11

Curtis White