Inline Asp.net Code is not triggering - asp.net

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!

Related

Nested inline operator <% giving a new line error

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>

asp.net (4) listview gives me troubles with generating id's

i'm in a asp.net listview, in the itemtemplate.
<asp:ListView runat="server" ClientIDMode="Predictable" ClientIDRowSuffix="Texttranslations_key"ID="lvwTextitems">
This is my code in the itemtemplate:
<span runat="server" onclick="openDiv('<%= EditItemDiv.ClientID%>')" style="width: 450px;"><%# Eval("Translation")%></span>
<asp:panel runat="server" id="EditItemDiv" style="display:none">
<asp:TextBox runat="server" ID = "EditItemArea" TextMode ="MultiLine" Rows="12" Columns="50" Text="<%# Eval("Translation")%>">
</asp:TextBox>
Now i have two problems.
First the span: i want the clientID of the asp:panel in the function openDiv(), so i can create some show hide functionality.
However, i get this as result:
<span onclick="openDiv('<%= EditItemDiv.ClientID%>')" style="width: 450px;">
my code isn't seen as code, but as plain text, and i don't know why?
Second, this line gets me a runtime error (The server tag is not well formed):
<asp:TextBox runat="server" ID = "EditItemArea" TextMode ="MultiLine" Rows="12" Columns="50" Text="<%# Eval("Translation")%>">
Can somebody help me out?
ps
at first i used this code for the generation of the id's: "myid<%# Eval("Id")%>" but that didn't workout either...
ps
i'm always getting in to trouble when using the Eval and the <%# %>, so it's probably some stupid thing (i hope)
For the first part, you definitely need to be using a binding expression:
<%# EditItemDiv.ClientID %>
The <%= %> scriptlet will have no context for each item. I assume you were "paraphrasing" the syntax you say you tried, so what didn't work before?
The "server tag is not well formed" is because you are trying to use double-quotes inside double-quotes. Change the outer to single-quotes:
Text='<%# Eval("Translation")%>'>
Basically, you can't nest similar quote types. Inline script will usually demand you use double-quotes, since single-quotes have a different meaning in c#, but you can use either double or single for markup parameter quoting. The upshot is that if you need to have inline script, use single quotes to wrap the markup parameter, which frees you to use double-quotes inside it.
If you need further single quotes in the output, e.g. to render a javascript parameter, just use '. You could also use " if you wanted to render double-quotes.
OnClientClick='openDiv('EditItem(<%# Eval("something") %>');'
As stated in my comment and by jamietre to fix the binding problem you need to change the code from:
Text="<%# Eval("Translation")%>"
to
Text='<%# Eval("Translation")%>'
As for the problem with the onclick of the span, it should work as you want if you just remove the runat="server" portion. I am not sure why, but it seems that adding this causes the controls to encode the onclick property.
If you need the runat="server" on the span then I will attempt to find another solution, but there are not guarantees.

Why does Visible='<%#false%>' work on a GridView but not label?

I am scratching my head over this, but have no idea what the problem is.
My actual code is
<asp:Label ID="Label1" runat="server" Text="abc"
Visible='<%#Request.QueryString["ListName"] == null %>' />
<asp:GridView ID="gvLists" runat="server"
Visible='<%#Request.QueryString["ListName"] == null %>' />
As you can see, i am trying to only make the visibility of the object be driven by the querystring. It works fine for the GridView, but doesn't work for a label. I also tried Panel and HyperLink with the same results.
I am sure I could get this working by putting my code in the code-behind, but it won't be as clean.
<%# %> works only on databound items.
you need to change it to <%= %> (Notice the "=")
<%= is uses to print to the page directly and the <%# is used for data binding elements. Here is a great explanation of all the inline code directives.
Thanks to Alison for pointing me in the right direction.
I needed to add Page.DataBind() to my Page_Load event in order for the expression to be evaluated.

asp.net inline code <%# MyboolVal %>

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

Binding Label to SiteMap Current Node

What I want to do is something like this:
<asp:Label ID="titleLabel" runat="server"
**Text='<%# SiteMap.CurrentNode.Title %>'**></asp:Label>
Where I can bind the name of the current page node in the Site Map to the title label on that page. We are doing this because, until we get these names finalized, they may change often. The above code does not work, at least for me; it displays nothing.
Any ideas are appreciated.
EDIT: Obviously I could do this in the code behind (i.e. Page Load event or something similar) but I would really rather do it in the aspx code.
It does work with
<span><%= SiteMap.CurrentNode.Title %></span>
which is the same output as asp:Label
As an alternative to using a label, you could also use the SiteMapPath control and hide the parent nodes:
<asp:SiteMapPath ID="SiteMapPath1" runat="server" ParentLevelsDisplayed="0">
The property ParentLevelsDisplayed allows you to specify how many parent nodes of the current sitemap node you want to display.
Its been a while but I believe its <%= #Eval(SiteMap.CurrentNode.Title) %>
Edit:
Text='<%= SiteMap.CurrentNode.Title%>'
Hopefully that works the same as it would <%= SiteMap.CurrentNode.Title%>.

Resources