I have the following code line in my aspx
<ItemTemplate>
My text
</ItemTemplate>
Now when I load the page I am getting
Compiler Error Message: CS1010: Newline in constant
The code allowed me to compile at first and I was able to run. The page shows the above exception.
If I replace <%# Eval("FileName") %> with a static value there it works fine. Any guess why this happens? Is there something around nesting <% operator? Any help is greatly appreciated.
I have to have that Eval part there so that I get the value from server.`
Okay, I suppose you are inside of a GridView or a Repeater control and you want to correctly evaluate the Url to a file. You should not include another <%%> when already inside of these operators. And also replace = to # in order to correctly bind the data to anchor element. Code below should work for you!
<ItemTemplate>
My text
</ItemTemplate>
Related
Agree my question is duplicate of this one and accepted answer works for me too. Let me clarify why.
When I have <%= in head it gives error.
When I have <%= in body it works.
When I have <%# in head it works.
I am just curious to know the reason for all three scenarios.
Additionally I created test project to emulate the issue but in that case all three situation works.
My page is too big and I am unable to decide what code to paste.
<%= %> is in fact doing Response.Write, which is literally writing symbols to the response. To the final markup that is.
Now notice that your head tag has this attribute runat="server". That makes it a server control. That is, this is not a final markup, and but rather a control that will output some markup to response during the control rendering stage. You cannot call Response.Write on this control, because it is not a final markup yet.
For the same reason it would work/not work in the body of the page. If you put it somewhere in plain markup it would work no problem:
<div><%= "Blah" %></div> <%-- works! --%>
But as soon as it appears inside anything with runat="server" you'll get an error
<div runat="server><%= "Blah" %></div> <%-- error! --%>
<asp:Panel runat="server"><%= "Blah" %></asp:Panel> <%-- error! --%>
Now <%# %> is a different beast. This is a data binding markup, something that is being evaluated when the server side control is being data bound. Thus is makes no sense (and is invalid) inside plain markup, and can be used whenever your control is bound to some data. Using it with header is not very common, use cases with GridView or Repeater are the most typical ones that come to mind.
I have the following line of code:
<asp:HyperLink runat="server" ImageUrl="~/App_Themes/<%=Page.Theme%>/images/buttons/contractqv.png" NavigateUrl="javascript:showhideQuickView()" ID="ShowHideBirdsEye" ToolTip="Show Hide Workload"></asp:HyperLink>
however when i run my code, and i look at the url, it becomes this:
http://localhost:51309/App_Themes/%3C%25=Page.Theme%25%3E/images/buttons/contractqv.png
Any idea why this is happening? i don't understand
What is happening is those characters can't be in a URL, so they get "encoded" to those weird values with %'s in them. The <% %> isn't being interpreted as a command but just as more text
Change your markup to this:
<asp:HyperLink runat="server" ImageUrl= '<%#ResolveUrl(string.Format("~/App_Themes/{0}/images/buttons/contractqv.png", Page.Theme)) %>' NavigateUrl="javascript:showhideQuickView()" ID="ShowHideBirdsEye" ToolTip="Show Hide Workload"></asp:HyperLink>
If the hyperlink is inside any data bound container, it will work fine. Else, you have to add this in Page_Load:
ShowHideBirdsEye.DataBind();
The code above is tested and working. Hope it helps!
So, currently, within an asp gridview, I have the following
<span id="btnEdit" runat="server" onclick="ShowEditCriteriaFilterDialog('<%#Eval("intSMCID")%>', '<%#Eval("strDescription")%>')" class="linkText">Edit</span>
What I'm essentially looking for is the syntax for quotes/double-quotes to actually accomplish this properly, as what I have above doesn't properly work.
First of all, if i encapsulate the entire onclick with single quotes, and not put any other quotes inside, it works for rendering purposes, but when i actually click the link at runtime, nothing happens.
If I encapsulate the entire onclick with double-quotes, like most properties of an ASPX element, it doesn't render properly, and everything after the comma which is after the first <%#Eval %> statement shows up as actual text on the screen. This leads me to believe there needs to be some escaping done to prevent it from thinking the click handler ends somewhere in the middle of that <%#Eval %> statement.
note that if I take away runat="server" and just encapsulate it in double-quotes, that seems to work better...but i need the span to be a server-side control for alot of other functionality I have in the page's code behind where I need to access the control through FindControl
The Eval function needs double-quotes, so you have to wrap the attribute value in single quotes.
You can't mix static content and <%# ... %> blocks in a single property value for a control with runat="server", so you need to build the entire string within a single <%# ... %> block.
Within a string in a <%# ... %> block, you can use ' to insert a single quote:
EDIT That only works in .NET 4.0; in 2.0, it generates ', which won't work. Use double-quotes instead:
onclick='<%# string.Format(
"ShowEditCriteriaFilterDialog(\"{0}\", \"{1}\")",
Eval("intSMCID"), Eval("strDescription")) %>'
Depending on your data, you might also need to encode the string value for JavaScript:
onclick='<%# string.Format(
"ShowEditCriteriaFilterDialog(\"{0}\", \"{1}\")",
Eval("intSMCID"),
HttpUtility.JavaScriptStringEncode(Eval("strDescription", "{0}"))) %>'
(Code wrapped for readability - it needs to be on a single line.)
I think the solution is
onclick='<%# "ShowEditCriteriaFilterDialog(" +Eval("intSMCID") + ","+ Eval("strDescription") +" );" %>'
I'm outputting a few lines of Javascript within a Repeater control on an ASPX page. I want to use a value from my DataSource inside the script tag.
A very basic example might be:
<asp:Repeater ID="RepeaterBlah" runat="server">
<ItemTemplate>
Hello <%# DataBinder.Eval(Container.DataItem, "SomeName")%>
<script>myfunction(<%# DataBinder.Eval(Container.DataItem, "SomeNumber")%>)</script>
</ItemTemplate>
</asp:Repeater>
I'm aware that most people won't repeat script tags like this, but I am using a small snippet of code from a third-party that you can place anywhere on a page to create a Flash object. You pass it a number so it knows which image gallery to display. No problems using several on one page.
To begin with, this worked fine, although I noticed the colours in Visual Web Developer indicated that it didn't really like the <%# being used inside a <script> tag. Intellisense was going a bit nuts in the code-behind too!
So what is the correct way to pass Dataset items into a script tag?
This perhaps? (Can't quite remember if the + signs should in fact be & signs though)
<%# "<script>myfunction(" + DataBinder.Eval(Container.DataItem, "SomeNumber") + ")</script>" %>
Another alternative syntax would be the following:
<%# Eval("SomeNumber", "<script>myfunction({0});</script>") %>
This uses the optional parameter where you can supply a format string.
I must be missing something stupid here but I can not see it. My work uses inline code on their sites, for example:
<panel runat="server" id="myid" visible='<%# MyboolVal %>'>
some stuff
</panel>
That seems to work great for them, the panel will display when their condition is meet.
I am trying to use a similar approach on a site of mine at home (its late friday evening so asking my boss is not the best idea at this point). I can not get it to output anything at all. I have tried it in the visible field which didn't work, so I thought I would just get it to write something to the screen:
<p>some text <%# String.Format("meeee {0}", Mybool) %></p>
But I do not get any output from the inline code. the "some text" appears but no "meeee" or the bool value.
I am doing this inside a user control, at this moment but do not imagine that would be the cause.
any ideas please?
Thanks
EDIT....
OK so thanks to Freddy Rios for the reply I can get the text to appear but when I try that in:
Visible='<%= mybool %>'
I get the compilation error of:
Cannot create an object of type System.boolean from its string representation for the visible property.
I am confused as to what exactly is happening. There must be part of the process under the bonnet I don't get.
EDIT 2:
I get the error on line 123:
<fieldset class="myclass" id="projectarea" runat="server" visible='<%= ShowProjectSearchArea %>'>
ShowProjectSearchArea is my bool value, set to false.
If I double click the error in the Error List window I get the following in a popup, which I have never seen before:
Cannot open file '%1'. It might not be in the solution.
<%# is databinding tag which is used to set values to server side controls, especially databound controls.
<%= is shorthand of Response.Write(), it writes the value to the output. So we use it with static html elements.
Try using = instead of # in your version:
<p>some text <%= String.Format("meeee {0}", Mybool) %></p>
The # is for databinding, so in the original code there must be a call to DataBind somewhere.
I think that problem is because visible property expect value of type string and you are trying to set it with bool.try to cast your value to string
Cheers