Databinding block generating only ampersands - asp.net

I am trying to generate a number of characters (in this case stars) in a databinding block. All it is generating on the client side is ampersands (the correct number though).
<span class="review-stars">
<%# New String("★", Eval("CustomerReviewScore"))%>
</span>
I am not sure what I need to do for the characters to actually come up on the client.
EDIT:
HTML is:
<span class="review-stars">&&&&&</span>

You could use a Literal control instead and set it's Mode property to PassThrough:
<asp:Literal ID="Literal1"
Mode="PassThrough"
Text= '<%# New String("★", Eval("CustomerReviewScore"))%>'
runat="server">
</asp:Literal>

Related

Display HTML in ASP.NET Literal Control

I have the following ASP.NET Literal Control displayed on my page
<div class="col-md-8 text-justify">
<p>
<asp:Literal ID="Literal1"
Mode="PassThrough"
Text= "<%#:Item.Description %>"
runat="server">
</asp:Literal>
</p>
</div>
This control is bound to a field in my SQL Server Table and a column called Description in the table contains the text with HTML tags like <p> <ul> <br> etc.
However when the text is displayed in the Literal control, it is displayed as-is i.e the HTML does not get rendered.
If the Literal control does not support rendering, what other options do I have?
Your problem isn't with the Literal control. It's the way you embedded the HTML. Try this:
<asp:Literal ID="Literal1"
Mode="PassThrough"
Text= "<%# Item.Description %>"
runat="server">
</asp:Literal>
Notice I left out the colon? That colon is there to prevent injection attacks, where the contents of the value being embedded might come from a user. But if you trust the HTML content, then removing the colon will embed the HTML and allow it to render properly instead of escaping it.

ASP.NET server tags rendered in client HTML, not values?

Maybe I've forgotten how to use these, but I am going crazy trying to inject a server-side value into an HTML output. There are reasons why I am doing this inline, and not server-side, so please don't suggest that as a solution.
This code on the server side:
<asp:Label ID="Label1" runat="server" Text='<%= DateTime.Now.ToString() %>' />;
Renders as this in the client HTML sent to the browser:
<span id="Label1"> <%= DateTime.Now.ToString()></span>;
And it displays as big fat empty space, and nothing output to the interface.
If I change the ASP source to using the "#" character to define as data-binding syntax, then the rendered output to browser becomes:
<span id="Label1"></span>
EDIT:
Setting Label text was just a simplified object for the sake of asking the question. In real life, I am setting the CssClass attribute, which does not allow me to use the "wrapping" workaround some have suggested. I wanted to set a public property and have all the controls update from it dynamically on page load.
Ideally, since I already have all the controls laid out on the aspx page. Just looking to add an attribute. I wanted to have:
<asp:textbox ID='MyTxtBox1' CssClass='<% strVal1 %>' />
<asp:textbox ID='MyTxtBox2' CssClass='<% strVal1 %>' />
<asp:textbox ID='MyTxtBox3' CssClass='<% strOtherVal %>' />
<asp:textbox ID='MyTxtBox4' CssClass='<% strVal1 %>' />
Now what it looks like I need to do is repeat all my (250+) controls on the codebehind in a block of code that looks like:
MyTxtBox1.CssClass=strVal1
MyTxtBox2.CssClass=strVal1
MyTxtBox4.CssClass=strVal1
MyTxtBox3.CssClass=strOtherVal
I believe that may not work on a compiled Web Application as it's not interpreted at run-time like a C# "Web Site". However, I was able to get it to work wrapping the label around the value:
<asp:Label runat="server"><%= DateTime.Now.ToString() %></asp:Label>
Set the Label1.Text = value instead of trying to use server side attrs inside of the server control

findcontrol() oddities in html and asp.net control/elements?

Can anyone offer an explanation as to why the asp:imagebutton gives me a badly formed html error while the html input element does not? I know it's about the findcontrol() in the onclientclick
assignment. They're written in exactly the same format but maybe they shouldn't be?
<ItemTemplate>
<input type="image" src="Resources/info.png" onclick="toggle('<%# Container.FindControl("PresetUploadDescription").ClientID %>');return false;" />
<asp:ImageButton ImageUrl="Resources/info.png" OnClientClick="toggle('<%# Container.FindControl("PresetUploadDescription").ClientID %>');return false;" ToolTip="info" ID="Description" runat="server"/>
....
You cannot use a <%...%> construct in a control that is executed at the server. (runat="server")
<%#...%> is used for databinding or Eval type statements.
<%=...%> is equivalent to a Response.Write statement, which looks like what you are trying to do (write out the ClientID of a certain control). Unfortunately, this won't work either - you'll get a
Server tags cannot contain <% ... %> constructs. Error
To fix, you need to Add the OnClientClick attribute to the Imagebutton control via the Code Behind page:
Description.Attributes.Add("OnClientClick",
"toggle('" + FindControl("PresetUploadDescription").ClientID + "');return false;");

Outputting From Resource File

So I am working on localization for a website, and I have ran into many scenarios where I need to output the localized string from the resource, such as in the following markup:
<cc1:TabPanel HeaderText="<%= Culture.Strings.labelImageA %>">
<HeaderTemplate>
<img id="ImageA" runat="server"/>
</HeaderTemplate>
<ContentTemplate>
<uc2:Charter ID="CharterA" runat="server" />
</ContentTemplate>
</cc1:TabPanel>
In the instance above, I am attempting to output form the resource Culture.Strings.labelImageA but am receiving a warning that
This is not a scriplet.Will be output as plain text
Is there an escape character I can use in the markup in order to allow this to pull from the resource file? Otherwise, I will have to jump through many hoops to set this property in the code behind.
What surely works is to use binding markup <%# %>. This, however, would probably require calling DataBind manually on your control.
I doubt there exists a simpler way.

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.

Resources