How to display HTML content in C# string? - c#

I am trying to achieve something like this:
string html = #"Hello <b> World ! </b>";
The desired output would be
Hello World !
The string html can be used anywhere on label, textbox, etc.
But it is not working. It just displays as it is Hello <b> World ! </b>
Is there any other way to do this?

Try HtmlString like:
HtmlString html = new HtmlString("Hello <b> World ! </b>");

Use #Html.Raw()
#Html.Raw(string);
See here for more: http://forums.asp.net/t/1903975.aspx?how+to+use+html+raw

Depends on the version of ASP.NET, but your safest bet is to create a literal control
<asp:Literal runat='server' id='yourOutput' Text='' />
And then set it on code behind
yourOutput.Text = html;
This should work on all versions of classic ASP.NET - for MVC projects you already got good answers.

Related

Correct html with missing <p> tag

Currently I am working on a solution to correct HTML. In our solution we have a rich text editor where we want to correct the HTML when the user forgets a <p> element, which happens a lot.
var input = new StringBuilder();
input.AppendLine("<h2>title1</h2>");
input.AppendLine("text");
input.AppendLine("<h2>title2</h2>");
input.AppendLine("<p>paragraph</p>");
var expected = new StringBuilder();
expected.AppendLine("<h2>title1</h2>");
expected.AppendLine("<p>text</p>");
expected.AppendLine("<h2>title2</h2>");
expected.AppendLine("<p>paragraph</p>");
Assert.AreEqual(expected.ToString(), input.DoSomething());
Is there something in the HTML agility pack? Of is there another solution?
Not sure if that helps, but there is a similar topic:
How to fix ill-formed HTML with HTML Agility Pack?
Also, not your question, but we had good results with a Markdown Editor instead of an HTML Editor.

how to execute dynamically created html code in string variable in asp.net

I have some HTML code in a String variable in C#.
That code generated and stored in String variable at run-time.
But now I want to execute this code at run time.
All I want to do is see how that code looks in BROWSER.
How can I do this?
Is there any control in asp.net that provides such functionality?
you can use any contol like panel literal.
Suppose you have a string
string str="<p><div>Some Text</div></p>";
Literal1.Text = str;
html
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
in your .aspx put html tag (div, paragraph, label etc) with property runat=server
<p id="dynamicstring" runat="server"></p>
in codebehind
dynamicstring.InnerText= yourstring;
You can use StringBuilder and then Simply use Response.Write for StringBuilder

Cannot add text to <p>

I'm trying to insert some text after the first tag:
<body id="tinymce" spellcheck="false">
<p>
// I want to insert text here
<br>
</p>
</body>
My attempt so far hasn't worked:
IElement tinymice;
string testText = "some text here"
string xPath = string.Format("//body[#id='{0}']/p", "tinymce");
tinymice = GetElementByXPath(xPath);
tinymce.SendKeys(string.Format("{0}", testText ));
Put the text into a span. Paragraph tags are intended to be seperators.
From the docs here: http://jwebunit.sourceforge.net/apidocs/net/sourceforge/jwebunit/api/IElement.html
I infer that you can call:
tinymice.setTextContent("text to insert");
I googled "getelementbyxpath selenium" I hope these are the right docs, it does list selenium at the bottom of the page: http://jwebunit.sourceforge.net/apidocs/net/sourceforge/jwebunit/api/class-use/IElement.html
I realize this is probably a javascript package, but this may be the same methods for the C# dll as well.
I don't think the above person was aware that tinyMCE is tinyMCE and not Tiny Mice..

Dynamically add HTML to ASP.NET page

Could someone please advise what the "correct" method is for adding HTML content to an ASP.NET page dynamically?
I am aware of the following declarative method.
//Declaration
<%= MyMethodCall() %>
//And in the code behind.
protected String MyMethodCall()
{
return "Test Value";
}
Is there a better or best practice way?
EDIT: I am building a Galleriffic photo gallery dynamically depending on the images located in a specific folder.
Depends what you want to do.
For controls/text I normally use a LiteralControl and set the Text property as the HTML I want to add, then this control can be added anywhere on the page that you want it to appear
LiteralControl reference is
here
ok seeing as you want it for Galleriffic, I guess it would pseudo-appear as such...
LiteralControl imageGallery = new LiteralControl();
string divStart = #"<div id='thumbs'><ul class='thumbs noscript'>";
imageGallery.Text += divStart;
foreach ([image in images])
{
string imageHTML = #"<li><a class='thumb' name='optionalCustomIdentifier' ref='path/to/slide' title='your image title'>
<img src='path/to/thumbnail' alt='your image title again for graceful degradation' /></a>
<div class='caption'>[caption]<div></li>";
imageGallery.Text += imageHTML;
}
string divEnd = #"</ul></div>";
imageGallery.Text += divEnd;
this.[divOnPage].Controls.Add(imageGallery);
Aspx :
<div id="DIV1" runat="server"></div>
Code behind :
DIV1.InnerHtml = "some text";
There are several ways to do that, which to use really depends on your scenario and preference.
Web User Controls: Can be added dynamically and you get the full editor support of Visual Studio.
XML literals (VB.NET only): Very convenient way to quickly put together HTML in code.
Templates: Add a plain HTML document to your solution and include it as a resource. Then you'll get editor support and you won't clutter your code with HTML source.
Another option
//.aspx
<asp:Literal ID="myText" runat="server"></asp:Literal>
//.aspx.cs
protected Literal myText;
myText.Text = "Hello, World!";

C# - Cutting HTML String into separate lines without breaking HTML tags

I have to break a HTML content string in to multiple lines...
And each line should have some fixed characters, 50 or 60
Also I don't want to break the word..or html tags...
ex : <p>Email: someone#gmail.com</p>
<p><em>"Text goes <font color=red>Hello world</font> Text goes here and Text goes here &nbsp Text goes here 1976."</em> </p>
How can I acheive this in C# ?
Any help would be appreciated...
I think you will need a HTML parser, and then you will have to serialize it again.
Instead of creating your own parser and serializer you should look into existing libraries.
For the parser I recommend the OSS Html Agility Pack

Categories

Resources