I know that we can <%: %> syntax for html encoding that is introduced in .Net 4. But I was reading new features of Asp.Net 4.5, and I got that we have another type i-e <%#: %> that is used for encoding the result of databind expression.
I am confuse with this.
What is the difference between <%: %> and <%#: %> in Asp.Net
Please explain both of them.
The same way that <%: %> is the HTML encoded version of <%= %>, the <%#: %> tag is the HTML encoded version of <%# %>.
The <%#: %> tag does the same as <%# %>, but then it calls Server.HTMLEncode on the string.
ASP.NET provides what's called a "binding" syntax to link HTML markup and controls to values extracted from data sources or other variables; that binding syntax is seen as something like:
<%# someVariable %>
The following colon merely extends the new "auto-HtmlEncode" behavior to the results of those bnding expressions.
Hope that helps.
Related
I'm developing a front-end part of an application right now, and a question came to my mind.
What is the difference between asp.net special tags:
<%= %>
<%# %>
<%# %>
And if exists another special tag please describe its function.
<%= prints the raw value of the expression within.
This syntax can cause XSS vulnerabilities and should not be used.
<%: prints and HTML-escapes the value of the expression within.
<%# is like <%=, but is used for data-binding
<% executes a block of code and ignores and return values
<%# is used for directives like Page or Imports.
Check the below site Once..You will get an idea
http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c-3c252c-etc).aspx
These are some useful special tags
<% %> 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
<%: %> Like <%= %> But HtmlEncodes the output (new with Asp.Net 4). http://weblogs.asp.net/scottgu/archive/2010/04/06/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2.aspx
<%= %> Code Render Block - For evaluate inline expressions
<%# %> Directive Syntax - Usualy for linking the codebehind and a
asp.net page.
<%# %> Data binding
You can find more information at:
http://msdn.microsoft.com/en-us/library/fy30at8h(v=vs.85).aspx
Is there is a way to get the current user in the aspx page,the page with the html definitions? (i know how to get it in the aspx.cs page)
This should work:
<%= Page.User.Identity.Name %>
Page.User Property
Use the following code block in the aspx where you want it to show up.
<%= System.Environment.UserName %>
See ASP.NET "special" tags for more info.
Use code tags
<%= YourMethodToGetTheCurrentUser() %>
You can access any public or protected variable from the code-behind file in it's .aspx file by wrapping your code in these characters:
<% if(PublicObject.Property > 0) { %>
<p>You can write normal HTML, or write strings like this: <%= PublicObject.Property %></p>
<% } %>
<% %> let's you add logic or commands into a page. You can use this to add for loops or if statements. <%= %> is used to write content directly to the page. This is used to print the contents of a variable.
<%= System.Web.HttpContext.Current.User.Identity.Name%>
Yet another one:
<%= HttpContext.Current.User.Identity.Name %>
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
ASP.NET “special” tags
You know how you can embed property value from code-behind in your page by simply using <%= PropertyFromCodeBehind %> in your .aspx?
Well, I only recently discovered that and I can't seem to find any tutorial that would explain this (and related stuff) in more depth (I only know that <%# is used in conjuction with Eval) - probably because I'm using <% for searches.
So, can anybody provide me with more detail explanation of these tags, or give a link to some tutorial that explains all this? I'm interested in anything that can be learned on this subject; somewhere I saw that you can do fancy stuff like <% for ... %>.
Here is a good place to get started.
There are several different syntaxes:
<%$ %> Expression Syntax
<%# %> Data-Binding syntax
<% %> Evaluated Code Blocks
<%= %> Statement and Expression
New to ASP.NET 4 is the HTML encoding syntax (haacked). This is the same as <%= %> except the result is HTML encoded (for non IHtmlString types). The new syntax is intended to replace <%= %>.
<%: %> HTML Encoded output
See ScottGU's post post to get you started.
What is the difference between <%# and <%= in ASPX inline code?
<%= is shorthand for Response.Write()
<%# is used to render data in databound controls
I'm currently creating a CMS system and found that the following doesn't work.
I do have a work around that isn't exactly ideal and feels dirty. I'm cool with it for now and not really that interested in a different approach (but don't let that stop you answering). What I am after is some kind of explaination on why it doesn't work - is it a bug in ASP.NET MVC?
It's hard to explain so I'll let my code (minus alot of fluff) do the talking... hope it makes sense!
EDIT: It seems that the compiler totally ignores the second masterpage's 'inherits' attribute - see at the bottom of the question.
ContentViewData.cs - notice it inherits from BaseViewData
public class ContentViewData : BaseViewData
{
public MyCMS.Data.Models.Content ContentItem { get; set; }
}
Site.Master - Notice the strongly typed viewdata of type BaseViewData
<%# Master
Language="C#"
Inherits="System.Web.Mvc.ViewMasterPage<MyCMS.WebSite.ViewData.BaseViewData>" %>
Content.Master - Notice the strongly typed viewdata of type ContentViewData and the fact that it's a child masterpage of Site.Master
<%# Master
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewMasterPage<MyCMS.WebSite.ViewData.ContentViewData>" %>
...blah blah blah...
<% Html.RenderPartial("ContentItemImage", Model.ContentItem); %>
ContentItemImage.ascx
<%# Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<MyCMS.Data.Models.Content>" %>
<% if (Model.HasPrimaryPhoto)
{ %>
<img src="/content/photos/<%= Model.GetPrimaryPhoto.ThumbFileName %>"
title="<%= Model.GetPrimaryPhoto.Caption %>" />
<% } %>
Now inside the Content.Master if I try and render the ContentItemImage partial and refer to a property on the ContentViewData object (specifically the 'ContentItem' property) like I have - repeated below.
<% Html.RenderPartial("ContentItemImage", Model.ContentItem); %>
If falls over on that line with the following error
Compilation Error
CS1061: 'object' does not contain a definition for 'ContentItem' and no
extension method 'ContentItem'
accepting a first argument of type
'object' could be found (are you
missing a using directive or an
assembly reference?)
BUT if I change things up like so, it all works fine and dandy.
Content.Master - Notice I'm passing into RenderPartial() the whole Model (ContentViewData object) rather than trying to refer to a property on the ContentViewData object
<%# Master
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewMasterPage<MyCMS.WebSite.ViewData.ContentViewData>" %>
...blah blah blah...
<% Html.RenderPartial("ContentItemImage", Model); %>
ContentItemImage.ascx - notice the changed strongly typed viewdata from MyCMS.Data.Models.Content to the ContentViewData class.
<%# Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<MyCMS.WebSite.ViewData.ContentViewData>" %>
<% if (Model.ContentItem.HasPrimaryPhoto)
{ %>
<img src="/content/photos/<%= Model.ContentItem.GetPrimaryPhoto.ThumbFileName %>"
title="<%= Model.ContentItem.GetPrimaryPhoto.Caption %>" />
<% } %>
So yeah, that works but it aint go not alibi.
Thanks in advance,
Charles.
EDIT: Interestingly it seems that the compiler totally ignores the second master page's 'inherits' attribute.
Eg. I can do this and it still compiles without a complaint...
<%# Master
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewMasterPage<ThisDoesntExist.AtAll>" %>
Interesting...A resonable guess would be that your ContentViewData object is being upcast to BaseViewData due to some interaction with your nested master page (if that is indeed the case, someone else will need to weigh in as to why).
You could verify trying this:
<% Html.RenderPartial("ContentItemImage", ((MyCMS.WebSite.ViewData.ContentViewData)Model).ContentItem); %>
The reason your workaround "works" is because your partial view is typed for ContentViewData, so when you pass in Model it is downcast to that type.
As you can see from my edit, it seems that the compiler totally ignores the nested master page's 'inherits' attribute.
This leads me to believe that a nested masterpage in ASP.NET MVC will always inherit from it's parent masterpage and as I've witnessed, totally ignore the inherits attribute.
EDIT: There must be some magic going on here... If I remove the 'inherits' attribute it won't compile because it doesn't know about the HtmlHelper class. But if I have the 'inherits' attribute in there with garbage inside it, it does compile.
Doesn't work
<%# Master
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master" %>
Does work
<%# Master
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="Sysasfdaasdtem.Web.Mvsdfc.ViewMasterPage<ThisDoesntExist.AtAll>" %>
Very odd indeed.
Just try to do the next thing:
<%# Master
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewMasterPage<MyCMS.WebSite.ViewData.ContentViewData>" %>
...blah blah blah...
<%= string.Empty %>
...blah blah blah...
<% Html.RenderPartial("ContentItemImage", Model.ContentItem); %>
Don't ask anything, just try to put <%= string.Empty %> in your master page's code and then use your strongly typed Model property.