I have a template engine, which uses <% and %> for templates, but problem is ASP.NET WebForms think other about that.
In Razor I can escape # symbol just by doubling it — ##. How do it in Web Forms?
UPD: HTML escaping not helps — template engine don't want to use <% and %>, so site just show's them.
I may have found some answers here: http://www.sitepoint.com/forums/showthread.php?65377-escape-asp-tags
text = "<" & "% CODE %" & ">"
<% CODE %>
Temporary solution is creating server-side variables and use like <%= OPEN %> and <%= CLOSE %>.
Related
I'm trying to declare a string variable right after the body tag from the approach below and display the value for the name variable inside a div tag. But why can't we access the variables outside the declared scripting tag<%CODE%>?
In other words, if I declare a variable name somewhere in the HTML code
as <% string name="Stark"; %> and try to access it somewhere down in the code using the code below <div><%=name;%></div>...
Then it prompts me with
The <VARIABLE NAME> doesn't exist in current context
ASP.NET MVC
#{
string name = "Stark";
}
<div>
<h2>#name</h2>
</div>
ASP.NET WebForms
<% string name = "Stark"; %>
<div>
<h2><%=name %></h2>
</div>
<%= %> syntax prints an expression.
It doesn't take a full statement (name; makes no sense as a statement, and it doesn't make sense to print a statement).
Therefore, it must not have a semicolon.
I know this is a bit old of a topic but I want to answer it for anyone that stumbles upon this question:
For Webforms, ASP.NET separates variable contexts by the tags they are located in. Sometimes you need to set the variable within the same tag as the call (<%= %>) you are working with so that ASP.NET will compile it.
So...
<label> <!-- scope -->
<% %>
<%= %>
<\label>
Instead of...
<% %>
<label> <!-- scope -->
<%= %>
<\label>
The differences comes when you use an if/for/foreach code blocks in Webforms. The scope surrounding the HTML and C# allows for C# to become the focus instead of the HTML.
This is most likely a scoping issue based on how ASP.NET webforms handles variables in HTML.
If someone can confirm if the scoping ability has improved for razor, that would be awesome.
I am trying to use Web.Router and I am trying to set it in a hyperlink where some variables need to be binded from a dataTable. Also, this link is a part of a Gridview:-
<%# Eval("title")) %>
But I get error: Server Tag not well formed. What's wrong with the syntax?
Try changing
<%# Eval("title")) %>
To this
<%# Eval("title")) %>
You put a double quot instead of single quot
This question already has answers here:
how to Embedded Code Blocks in ASP.NET Web Pages?
(2 answers)
Closed 8 years ago.
I am a beginner level programmer. I was using C# variable in the aspx page.
I have seen the usage of <% in the aspx page alot.
I need the detail of when to use <% in what requirement like
<% 'When to use this?' %>
<%= 'When to use this?' %>
<%# 'When to use this?' %>
<%# 'When to use this?' %>
I am searching for a useful link regarding this but did not found any help
I hope this could be Helpful.......
http://www.codeproject.com/Articles/384425/Server-side-Delimiters-in-ASP-NET`
You can search with name "delimiter in asp.net" probably google will give you lot of results.
<% %> is server code that executes during the page's render phase which can execute the statements written inside the block which help in interacting with server side at runtime.
<% { Response.Write("Hello !"; }%>
and like wise if you have script function in your page and you want to call that function you can ue this
<% =Callfunc()%>
And by default in all pages and user controls you can see directives. more here
MSDN
<% 'When to use this?' %> Similar to classic ASP and is used to add server-side code within your ASPX page such as:
<% for (int i=0; i < 10; i++) { %>
<p>I am added to the page 10 times</p>
<% } %>
<%= 'When to use this?' %> Similar to the example above only the = allows you to "inject" or reference an expression or variable and not a chunk of code. Belows example refers to MyAnchor which can be declared in the code behind. <a href='<%= MyAnchor %>'></a>
<%# 'When to use this?' %> This is used for page and control declarations: <%# Page Language="vb" AutoEventWireup="false"
<%# 'When to use this?' %> This is used for databinding
<asp:GridView ID="gvMyGrid" runat="server">
<Columns>
<asp:TemplateField HeaderText="E-mail" SortExpression="Email">
<ItemTemplate>
<asp:HyperLink runat="server" Text='<%#Eval("Email").ToString()%>' NavigateUrl='<%#Eval("Email", "mailto:{0}").ToString() %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I searched a way to include a file in a web application (like a menu, so I won't have to edit it on all pages when applying changes), but haven't found something as simple as
<?php include "Menu.html"; ?>
Can you please help?
Have you looked into Master Pages? They would certainly help you add the same layout across several pages.
Or perhaps you want a reusable User Control (that you write yourself)?
We don't use "include page" in asp.net, even though it is possible (with a different syntax of course). Instead, have a look at Master page concept.
MasterPages allow you to maintain a parent/child relationship between a master page which contains content that wraps around any number of child content pages.
Similarly, UserControls allow you to re-use whatever content you want on whatever page you want, whether it's a MasterPage or ContentPage:
<%# Page Language="C#" %>
<%# Register TagPrefix="uc" TagName="Spinner"
Src="~/Controls/Spinner.ascx" %>
<html>
<body>
<form runat="server">
<uc:Spinner id="Spinner1"
runat="server"
MinValue="1"
MaxValue="10" />
</form>
</body>
Methods (C#)
Executable code:
Page include
<!--#include file="a.aspx"-->
Execute independently inside a page
<% Server.Execute("a.aspx"); %>
Non-executable code:
<% Response.WriteFile("a.inc"); %>
I believe this is what you are looking for.
<!--#include file="wisdom.aspx"-->
I use C#.net
<% Response.WriteFile("YourPage.aspx"); %>
and this works real well for me!!
I also use your line,
<!--#include file="wisdom.aspx"-->
when I am in HTML mode.
What am I doing wrong? How come <%= this %> isn't being interpreted as C#?
Here's the code :
And here is what it renders (notice the Firebug display):
What do you think is going on? MVC newb here. :(
And the static Site class:
(If you cannot see the screenshots on the page, view source and use the URLs from the <img> tags.)
<%: %> starts with .NET v4
For pre-v4 it's equivalent is <%= Html.Encode(...) %>
The problem was that I was using <%= %> (or even <%: %>) within a tag that had runat="sever".
Shouldn't that be <% %> or <%= %> for a shorthand of Response.Write?
Here's an MSDN article on Embedded Code Blocks.
This sometimes happens to me when embedding code inside of html attributes. I've never quite pinned down the exact cause but sometimes you can get around it by using single quotes rather than double.