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 %>
Related
I want to call one div when the page is default.aspx and to call same div at the same time when the page is another page than default.I have done something like this but is not doing correctly.
<% if(string.Compare(Request.Url.LocalPath,"/default.aspx")==0 || string.Compare(Request.Url.LocalPath,"/") ==0)
{%>
<div class="temples" >
<% } %>
<% else
{ %>
<div class="temples" style="display:none";>
<% } %>
You can try like this:
string s = this.Page.Request.FilePath;
This will get you the current request URL from within the master page
Also check the IsMasterPage property:
Gets a value that indicates whether or not a child element in the
viewer should be used as a master page.
There is a property IsMasterPage to define whether your control element associated to master page. Below is a link to get more information.
DocumentViewerBase.IsMasterPage
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
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.
How do I create a menu in a ASP.NET MVC2 Master Page, dynamically based on the current user's "role"?
The simplest and most straightforward way would be to simply add an if statement in the view markup:
<% if (Page.User.IsInRole("Admin")) { %>
<%= Html.ActionLink("Admin Tools Index", "Index", "Admin") %>
<%= Html.ActionLink("Admin Dashboard", "Dashboard", "Admin") %>
<% } %>
Or, you can separate out several items pertaining to a specific role into a partial view:
<% if (Page.User.IsInRole("Admin")) { %>
<% Html.RenderPartial("AdminMenu"); %>
<% } %>
I'm not sure about MVC but in 'normal' ASP.NET it is possible to select a MasterPage at runtime.
If you are using the sitemap file to generate menus then you can probably do it in there. If not, then it depends.
What is the difference between <%# and <%= in ASPX inline code?
<%= is shorthand for Response.Write()
<%# is used to render data in databound controls