ASP.NET <%# versus <% [duplicate] - c#

This question already has answers here:
When should I use # and = in ASP.NET controls?
(3 answers)
In ASP.Net, what is the difference between <%= and <%# [duplicate]
(4 answers)
Closed 9 years ago.
I'm working on various asp.net pages .
For inline functions I do see 2 different formats are used:
Example 1:
<p><%Response.Write(now())%></p>
I also see another one with #:
Example 2:
<Asp:TextBox id="Textbox5" width="40" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' runat="server" />
I want to know what is the exact different , <%# vs <%

Here is a good explanation here on stack -
In ASP.Net, what is the difference between <%= and <%# [duplicate]
Summary from those answers:
There are a several different 'bee-stings':
<%# - Page/Control/Import/Register directive
<%$ - Resource access and Expression building
<%= - Explicit output to page, equivalent to <% Response.Write( ) %>
<%# - Data Binding. It can only used where databinding is supported,
or at the page level if you call
Page.DataBind() in your code-behind.
<%-- - Server-side comment block
<%: - Equivalent to <%=, but it also html-encodes the output.

The former is simply denotes some .NET code in the markup that outputs to the page.
The later uses Data Binding Expression Syntax to bind to a specific object.

Related

Call MVC Html helper extension in ASPX page

I am working on the MVC project in which one of the aspx page also exists for SSRS reports. Here, I have a HTML helper written for returning some multi-lingual text value. So, For all the report labels, I need to call HTML helper extension to get the string text. Is there any way that I can call that method from my aspx page??
Note: I cannot do this from Code behind since all the labels exists in aspx that I do not want to migrate it to code behind.
So far I tried
<%# Import Namespace="Vibrant.HtmlHelperExtension" %>
<asp:Label Text="<%# ReturnKeyValue("Brief") %>" meta:resourcekey="lblBrief" ID="lblBrief" CssClass="label"></asp:Label>
The above one is not able to call the method and I am getting The name 'ReturnKeyValue' does not exist in the current context exception.
This is what I found. Maybe you can try.
http://www.asp.net/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs
HTML Helper is rendered with <%= %> tags instead of <% %> tags. If you
don't include the equal sign, then nothing gets rendered to the
browser.

Difference between special tags asp.net

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

How to set the default value of ko.observable to the default value of the control itself in ASP.NET and KnockoutJS? [duplicate]

This question already has answers here:
How to extend knockout observables to read default value from binding?
(2 answers)
Closed 8 years ago.
I have an ASP.NET C# web page with KnockoutJS bound controls:
<asp:TextBox runat="server" ID="txtOfferName" placeholder="Offer Name" data-bind="value: planName"></asp:TextBox>
and
planName: ko.observable("")
...set on document load. It all works great when txtOfferName should start off as blank. Now, I want to set the value of txtOfferName to something that's not blank during Page_Load. If I just do
txtOfferName.Text = "New Value";
it won't work because ko.observable("") will overwrite the value once it gets bound. Is there a way to bind an observable and have it default to the initial value of the control it's bound to? I can certainly do
ko.observable("New Value")
but the values are coming from the database, so it'd be much more difficult to set them in the front-end, rather than backend. Worst case scenario, I can serialize all those values, put them into a hidden field and assign them using KO, but I wanted to check in case there's an easier way first.
Thanks!
<%# Page ClientIDMode="Static" %>
<input type="hidden" runat="server" id="hdnOfferName" />
<asp:TextBox runat="server" ID="txtOfferName" placeholder="Offer Name" data-bind="value: planName"></asp:TextBox>
//code behind
hdnOfferName.Value = "New Value";
//javascript (this is $jquery, use document.getElementById() for javascript)
planName = ko.observable($("#hdnOfferName").val());
How to extend knockout observables to read default value from binding?
This answer to basically the same question is your best, most flexible option. When the binding initializes, it will store whatever the current value of the input is in the observable
I haven't used KO with WebForms, but I think it's the same idea with MVC. In ASP.NET MVC, I set defaults before calling ko.applyBinding like this...
<asp:TextBox runat="server" data-bind="value: planName"></asp:TextBox>
<script src="viewModel.js" />
<script type="text/javascript">
myViewModel.planName('<%=Stuff.planName%>');
ko.applyBinding(myViewModel);
</script>

How to mix embedded code blocks with data-binding expression

I read this document http://support.microsoft.com/kb/976112
where explains all the embedded code blocks available but I want to combine two of them.
I want to use base <% ... %> embedded code blocks with <%# ... %> data-binding expression
Example I want to add an "If" condition to this code:
<asp:Label ID="lblHello" runat="server" Text="<%# DataBinder.Eval(Container.DataItem, "[\"Txt\"]")%>"></asp:Label>
Regards.
The difference in the <% and <%# is mainly in when they're run (the former at render time, the latter at data binding). As such, it makes no sense to "combine" them.
What you likely want to do, is to run some additional code when data binding to do your if statement. If it's a simple expression, you can just inline it:
<%# MyProperty ? Eval("Txt") : Eval("OtherTxt") %>
If it's more complicated, then it's usually best to just call a code-behind method to do it for you:
<%# MyMethod(Eval("Txt")) %>

List of what each of <% means (<%#, <%=, etc...) [duplicate]

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.

Categories