views:

116

answers:

2

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.

+2  A: 

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.

Curtis White
A: 

No, server-side controls can't. For example, I have a string property named SkinPath that give me the full App_Themes path to the current theme. I use it in the following way:

<img src='<%= SkinPath %>/Images/myImage.png' />

However, the following doesn't work:

<asp:Image ID='image' runat='server' ImageUrl='<%= SkinPath %>/Images/myImage.png' />

Instead, it renders the src literally in the result <img>.

Brad
@Brad I updated my answer. I can use <%# and that works to databind to a property. Do you have more explanation why <% doesn't work? Is it because the way the render step works?
Curtis White
Curtis, I had a typo: I should have put <%= SkinPath %> and no, I don't have any reasoning for this. I do recall on one project before I got an error that was resolved by changing all the <%= to <%#. Some things you take the time to figure out. Other times, you get it done and get paid. :)
Brad