Server tag not well formed - c#

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

Related

Escape <% and %> in ASP.NET

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 %>.

bind DataItem with a space in the name

i'm trying to bind dataitem in GridView like that:
<%# DataBinder.Eval(Container, "DataItem.Project No.") %>
and getting the error:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Project No'.
the field is Project No. (with a dot .).
how can i bind that field?
try this : [] to indicatethat it is a column name.
<%# DataBinder.Eval(Container, "DataItem.[Project No.]") %>
<%# Eval("Project No.") %>
note that your datatable/dataset should have Project No. exactly to work
Update
100% Working & Tested
<%#DataBinder.GetPropertyValue(Container.DataItem,"Project No.") %>

C# .NET include file

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.

ASP.Net C# ListView inline conditions

In have a list of objects bound to a ListView that is used to make a nice list of these items. In this ListView I have 1 column that shoud have a specific condition to display a specific string. Is this possible using inline code or should I get a workaround using the codebehind?
This is what I would like to do:
<% if (((Recipe)Container.DataItem).Status == RecipesModel.REJECTED) { %>
Something goes here
<% } %>
But this returns this exception:
The name 'Container' does not exist in the current context
EDIT: this code is used inside <ItemTemplate>
EDIT 2: I found myself using the following code for this problem:
<asp:PlaceHolder id="place_public" runat="server" Visible='<%# ((Recipe)Container.DataItem).Status == RecipesModel.VALIDATED %>'>
Something here
</asp:PlaceHolder>
you cannot use Container.DataItem outside of data binding context
try something like
<%# Container.DataItem ... %>
eg:
<%# ((String)Container.DataItem).ToUpper() == "test" ? "IsTest" : "NotTest" %>
You may want to import the namespace of the container class in your .aspx page.
for example:
<%# Import Namespace="Container Class namespace" %>
It looks like you are trying to use the Container object outside of its scope. Can you post the rest of the code so we can see what is happening on the page?

ASP.NET/MVC: Inline code

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.

Categories

Resources