Change rendered html - c#

I have a dll thats rendering a div at the top of my page and I need to remove. Is it possible to edit the html that is about to be rendered, so that i can remove the div from the html before its displayed:
protected override void Render(HtmlTextWriter writer)
{
// setup a TextWriter to capture the markup
TextWriter tw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(tw);
// render the markup into our surrogate TextWriter
base.Render(htw);
// get the captured markup as a string
string pageSource = tw.ToString();
// render the markup into the output stream verbatim
writer.Write(pageSource);
// remove the viewstate field from the captured markup
//string viewStateRemoved = Regex.Replace(pageSource,
// "<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\".*?\" />",
// "", RegexOptions.IgnoreCase);
// the page source, without the viewstate field, is in viewStateRemoved
// do what you like with it
}

Related

HTML img tag does not render source image in WebKit.NET browser in C#

I'm trying to set my WebKitBrowser DocumentText to an HTML string containing a local SVG file path as image source.
Actually I want to show the SVG file in a web browser.
Here is my code:
string SVGPath = "file:///D:/MySVGFiles 1/SVGSample01.svg";
StringWriter stringWriter = new StringWriter();
using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
{
writer.AddAttribute(HtmlTextWriterAttribute.Src, SVGPath);
writer.AddAttribute(HtmlTextWriterAttribute.Width, "50%");
writer.AddAttribute(HtmlTextWriterAttribute.Height, "50%");
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
}
string content = stringWriter.ToString();
this.webKitBrowser1.DocumentText = content;
When I run the code, the browser only shows the image canvas, and does not render the SVG file. I have tried this with a JPG image too, and got the same result.
Could anyone please tell what is wrong with this code??
I finally found out what is wrong.
DocumentText property of WebKitBrowser is a string, and in its set method, HTML text is passed to loadHTMLString method.
webView.mainFrame().loadHTMLString(value, null);
DocumentText property is used when no URL is specified. But here I wanted to load an image from a specified address. So in case of using tags like setting DocumentText property would not be valid. I had to call loadHTMLString, and when an image is gonna be added to HTML string using its address, URL must be the directory of the image file.
According to what I have found in https://groups.google.com/forum/#!topic/uni_webview/idiRRNIRnCU, I changed the code and problem is solved!
Here is the code that works:
string fileName = "SVGSample01.svg";
string URL = "file:///D:/MySVGFiles 1/";
StringWriter stringWriter = new StringWriter();
using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
{
writer.AddAttribute(HtmlTextWriterAttribute.Src, fileName);
writer.AddAttribute(HtmlTextWriterAttribute.Width, "50%");
writer.AddAttribute(HtmlTextWriterAttribute.Height, "50%");
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
}
string content = stringWriter.ToString();
(this.webKitBrowser1.GetWebView() as IWebView).mainFrame().loadHTMLString(content,URL);
Just make sure the URL string contains "file:///" and the last "/".

String concatenation or HtmlGenericControl html controls?

I am trying to genrate complex list of items using ListView. For every item i must create something like this
<div>
<ul>
<li>foo<li>
<li>bar<li>
.... Dynamic count of <li>
<ul>
<span>Some dynamic text</span>
.. bunch of other dynamicly generated html
</div>
My question is what is better way to generate the html.By using string concatenation like this
StringBuilder sb = new StringBuilder();
sb.Append("<div>");
.......
sb.Append("</div>")
Or by using HtmlGenericControl like this:
HtmlGenericControl htmlItem = new HtmlGenericControl( "div" );
....
using( TextWriter textWriter = new StringWriter( ) )
using( HtmlTextWriter htmlWriter = new HtmlTextWriter( textWriter ) )
{
HtmlGenericControl htmlItem = null;
CreateMenuItem( menuItem, 0, null );
htmlItem.RenderControl( htmlWriter );
return textWriter.ToString( );
}
I prefer this way because this gives me much more Readability. Looking at this , i can easily imagine, How my output will look like.
StringBuilder sb = new StringBuilder();
sb.Append("<div>");
sb.Append("<ul>");
sb.Append("<li>Item1</li>");
sb.Append("<li>Item2</li>");
sb.Append("<li>Item3</li>");
sb.Append("</ul>");
sb.Append("</div>");
HtmlTextWriter is good because:
HtmlTextWriter is the cleanest and the mark-up is nicely indented
when it is rendered.
There is a performance impact as HtmlTextWriter writes directly to the output stream.
HtmlTextWriter supports encoding HTML automatically
Stringbuilder doesn't write to the output stream until ToString is called on it.

Manipulating HTML from the asp.net code-behind

I am able to get the HTML from the code-behind, like this one:
protected override void OnPreRenderComplete(EventArgs e)
{
StringWriter sw = new StringWriter();
base.Render(new HtmlTextWriter(sw));
sbHtml = sw.GetStringBuilder();
Response.Write(sbHtml + "<!-- processed by code-behind -->");
}
But I need to remove the HTML from the Page, any help?
If I understand well you wish to manipulate the sbHtml, and write it out.
sbHtml = sw.GetStringBuilder();
sbHtml.Replace('anything','to anything');
Response.Write(sbHtml);
(or is something else ?)
Did you want a method like this to strip the HTML?
public static string StripHTML(string HTMLText)
{
var reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase);
return reg.Replace(HTMLText, "").Replace(" ", "");
}
You can put an <asp:placeholder> on the page and set the contents to whatever you want. Add/remove/whatever.

Getting the content of an ASP.NET System.Web.UI.WebControls.PlaceHolder

I have a server control that has a PlaceHolder that is an InnerProperty. In the class when rendering I need to get the text / HTML content that is supposed to be in the PlaceHolder. Here is a sample of what the front end code looks like:
<tagPrefix:TagName runat="server">
<PlaceHolderName>
Here is some sample text!
</PlaceHolderName>
</tagPrefix:TagName>
This all works fine except I do not know how to retrieve the content. I do not see any render methods exposed by the PlaceHolder class. Here is the code for the server control.
public class TagName : CompositeControl
{
[TemplateContainer(typeof(PlaceHolder))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public PlaceHolder PlaceHolderName { get; set; }
protected override void RenderContents(HtmlTextWriter writer)
{
// i want to retrieve the contents of the place holder here to
// send the output of the custom control.
}
}
Any ideas? Thanks in advance.
I just found the solution. I did not see the render methods because of the context of how I was using the PlaceHolder object. Eg I was trying to use it as a value and assign it to a string like so:
string s = this.PlaceHolderName...
Because it was on the right hand side of the equals Intellisense did not show me the render methods. Here is how you render out a PlaceHolder using and HtmlTextWriter:
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.PlaceHolderName.RenderControl(htw);
string s = sw.ToString();
Posting this as a second answer so I can use code formatting. Here is an updated method that uses Generics and also uses the 'using' feature to automatically dispose the text / html writers.
private static string RenderControl<T>(T c) where T : Control, new()
{
// get the text for the control
using (StringWriter sw = new StringWriter())
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
c.RenderControl(htw);
return sw.ToString();
}
}

Is there a way to create and render asp controls to a html string in the code-behind?

How would you create a new DropDownList (or any asp server control) and then render the html to a string in C#?
System.Web.UI.Control has a RenderControl(HtmlTextWriter) method that you can use to get the rendered content of the control as a string:
using(var sw = new System.IO.StringWriter()) // SW is a buffer into which the control is rendered
using(var writer = new HtmlTextWriter(sw))
{
myControl.RenderControl(writer);
return sw.ToString(); // This returns the generated HTML.
}

Categories