html helpers text input and line break - c#

public string BannerText {get;set;}
public void SetBanner()
{
BannerText = "This is line 1. \nThis is line 2."
}
in the aspx page I am setting it like this:
<div>
<h1><%: Model.BannerText %></h1>
</div>
However, the text still shows up in a single line. I have tried <br /> and <br> as well but that doesn't seem to work. I am sure it has to be something simple, but need SO help :)
EDIT:Kirk Woll and Mike suggestion worked!
use: <h1><%= Model.BannerText %></h1>

First, \n will absolutely not work. This is HTML. You do need to use <br /> (or wrap them in <div>, etc.). However, the purpose of <%: (as opposed to <%=) is to HTML-encode your string. So presumably your line breaks (<br />) are being encoded. You should try <%= instead and see if that fixes your issue.

What is "<%:"? I haven't seen that before. Have you tried "<%="? That will use an HTMLTextWriter to pass the string to render. Then just use HTML (<br />) in your string.

Related

Remove invalid/incorrectly placed tags from html string

I'm wondering if there is a good (or good enough) way to remove invalid or incorrectly placed HTML tags from an HTML string in C#?
Example 1: <div> </div> </div> should be changed to <div> </div>
Example 2: <div> </section> </div> should be changed to <div> </div>
Basically the transformed html string should be W3C validated markup. I understand that this may be a bit difficult to do, perhaps there is a library that does the job well?
Thanks!
I'd recommend using HTMLTidy.
Since you're using C#, there's the tidy.net project. I think there are dlls that you can just reference and use in your C# code.
Or, you can just use the command line stuff for HTMLTidy.
I ended up fixing the root issue that generated an invalid HTML string. In such a scenario, it is exceedingly better to fix the main problem - if possible - than the symptoms.

How can I convert these tags and attributes to aspx.net?

i would want convert the following html code in aspx with Response.Write and tags + attributes.
<p class="inline-medium-label button-height">
<span class="label">Large with stripes</span>
<span class="demo-progress large" data-progress-options='{"size":false,"style":"large","barClasses":["green-gradient","glossy"],"innerMarks":25,"stripes":true,"darkStripes":false}'>100%</span>
</p>
Ho can i convert it leaving intact the single quotes? Thanks!!
Thanks again!
Cris
You can certainly response.write it:
Response.Write("<p class=\"inline-medium-label button-height\">");
Response.Write(" <span class=\"label\">Large with stripes</span>");
Response.Write(" <span class=\"demo-progress large\" data-progress-options='{\"size\":false,\"style\":\"large\",\"barClasses\":[\"green-gradient\",\"glossy\"],\"innerMarks\":25,\"stripes\":true,\"darkStripes\":false}'>100%</span>");
Response.Write("</p>");
If you are using an HtmlTextWriter, you can use the AddAttribute/RenderBeginTag methods to render data, but then you have no control over the markup, and so I don't think that would work.
It really depends on the context of where this code is at to provide the best answer possible.

MailTo: Asp.net

I have a asp label that I need to be able to change according to the code behind. How can I do this?
ASPX: (The first part works correctly only for "TestA#abc.com" and the second part dynamically changes the label (EmailLabel) according to the "if" statement in the code behind. How can I integrate these two so the label is mailto? Thanks.
<p>Email at TestA#abc.com.</p>
<p>Email at <asp:Label ID="EmailLabel" runat="server"></asp:Label>.</p>
Code Behind:
public changeLabel()
{
if (//Some Condition Here)
{
this.EmailLabel.Text = "TestA#abc.com";
}
else
{
this.EmailLabel.Text = "TestB#abc.com";
}
}
What you are trying to do there won't work. Label's render out as <span> tags, so it will never be "clickable". You want to do something more like this:
<p>Email at TestA#abc.com.</p>
<p>Email at <asp:LinkButton ID="EmailLabel" runat="server"></asp:LinkButton>.</p>
And then instead of changing the Text property, change the NavigateUrl property.
You could also use an HtmlControl, which is basically a standard HTML tag that you add the runat="server" attribute to.
<p>Email at <a id="EmailLabel" runat="server" href=""></a>.</p>
You would then be able to modify this <a> tag via server side code, the properties will be slightly different, but now you've got a real live anchor tag to work with.
This other SO question might also be helpful: How to launching email client on LinkButton click event?
<html>
<body>
<span id="foo"
onclick="document.getElementById('foo').innerHTML = 'never say never'"
>
Click Me!
</span>
</body>
</html>
For your Label, just remember that .Text is the server equivalent of .innerHTML so you can put whatever HTML you want right into the asp:Label when setting .Text. Just watch out for cross-site-scripting exploits.

Surrounding Eval with html tags

Please please help!! I've been searching this for hours but maybe the solution is so obvious I'm completely overlooking it :(
I have a listview that binds to a sql table. The table contains a bunch of fields that hold only 'T' or NULL values, except for the last two fields which hold free-type text for user comments.
For the T/NULL values - The listview is configured so that an empty cell will not display, and where there is a 'T' value, it will instead show a custom text and create a line break for the next item. This syntax does exactly that:
<asp:Label ID="LymeLabel" runat="server" Text='<%# Eval("Lyme","Lyme Disease<br />") %>' />
However, I want the last two fields (the free-type text) do display in a paragraph style, with paragraph tags surrounding the Eval statement. The tags have to form part of the Eval so that they won't render whitespace if the cells are empty. My thinking was to do it this way but it doesn't work - the whitespace shows up either way:
<asp:Label ID="OtherCommentLabel" runat="server" Text='<%# "<p>"+Eval("OtherComment")+"</p>" %>' />
I'm guessing the " "+ ... +" " doesn't care what's in or not in the middle.. but how to fix it? There must be a very simple solution to this and I feel really stupid even asking this. Any advice please?
From the description, you don't need the asp:Label at all. Why not just wrap the Eval() in a P tag?
<p><%# Eval("Lyme","Lyme Disease") %></p>
Even if the label worked, you wouldn't want the resulting markup (paragraph inside a label makes no sense).
To handle the show/hide if the item is empty, you could do something like:
<ItemTemplate>
<p runat="server" visible='<%#!string.IsNullOrEmpty(Eval("OtherComment"))%>'>
<%# Eval("Lyme","Lyme Disease") %>
</p>
</ItemTemplate>
The white space will show up because p is not part of the Eval; <p>Eval()</p>.
Not sure if elegant, but you could wrap the Eval inside a method:
public string ParagraphIfData(string input)
{
if(!string.IsNullOrEmpty(input))
return "<p>" + input + "</p>";
return "";
}
Then:
<%# ParagraphIfData(Eval("Lyme","Lyme Disease")) %>

How to remove the css class

I have a piece of code that is wrapped in a css class, the problem is that there is one particular line there that needs to be in the <span> but does not need the css class. How can I tell it to not take that css? for that line
<span id="myPrice" class="Price" runat="server">
<uc:CustomMessage ID="mPrice" MessageKey="myMsg" runat="server" /> //does not need css
<span ><asp:Literal ID="litPrice" runat="server" /></span>
</span>
So as you can see the second line, we dont want the css applied to it.. but it needs to be within that "myPrice" span.
Is there a way to achieve this?
Thank you
From a design standpoint it would be better to move the styling to the inner <span> instead, but if that's not possible you could probably exclude it like this:
.Price:not(input[type="button"]) { color:red; }
The above excludes buttons, but you can replace this with whatever element you need. Here's a more detailed reference:
http://kilianvalkhof.com/2008/css-xhtml/the-css3-not-selector/
Give your user control its own CSS class, and specify rules that undo the effect of Price.
<uc:CustomMessage CssClass="noStyles" ID="mPrice" MessageKey="myMsg" runat="server" />
Then, have a rule called noStyles that undoes any effects of Price.
Can you apply the css rule just to the first line ?
#myPrice #mPrice { your rule }

Categories

Resources