Manipulating HTML from the asp.net code-behind - c#

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.

Related

Generate HTML/XML Markup in C# Console Application?

I am working a console application that generates XML/HTML output. Currently I have the code below that includes hard-coded spaghetti markup.
Is it possible to use razor or some tool to improve this and make it cleaner?
foreach (var file in _files)
{
TagLib.File f = TagLib.File.Create(file.FullName);
string title = f.Tag.Title;
string url = _urlPrefix + file.Name;
StringBuilder sb = new StringBuilder();
sb.Append("<item>\n");
sb.AppendFormat("\t<title>{0}</title>\n", HttpUtility.HtmlEncode(title));
sb.AppendFormat("\t<pubDate>{0}</pubDate>\n", file.CreationTimeUtc.ToString("r"));
sb.AppendFormat("\t<guid isPermaLink=\"false\">{0}</guid>\n", url);
sb.AppendFormat("\t<enclosure url=\"{0}\" length=\"{1}\" type=\"audio/mpeg\" />\n", url, file.Length);
sb.Append("\t<itunes:subtitle></itunes:subtitle>\n");
sb.Append("\t<itunes:summary></itunes:summary>\n");
sb.Append("</item>\n");
items.AppendLine(sb.ToString());
}
Just use the System.XML classes to help you.
https://msdn.microsoft.com/pt-br/library/system.xml(v=vs.110).aspx
sample code:
XmlDocument doc = new XmlDocument();
XmlElement el = (XmlElement)doc.AppendChild(doc.CreateElement("Foo"));
el.SetAttribute("Bar", "some & value");
el.AppendChild(doc.CreateElement("Nested")).InnerText = "data";
Console.WriteLine(doc.OuterXml);
There is an open source project which allows to use Razor as a general templating engine: it's called
razorengine
string template = "Hello #Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });
I think this will helpful for you..
You don't have to use a StringBuilder to output XML, you can use LINQ to XML (https://msdn.microsoft.com/en-us/library/bb387061.aspx) or XmlWriter (https://msdn.microsoft.com/en-us/library/system.xml.xmlwriter(v=vs.110).aspx) or the DOM API (https://msdn.microsoft.com/en-us/library/t058x2df(v=vs.110).aspx).

How to remove some html tags from a web page(aspx page) before rendering in asp.net?

I want to remove some html tags from the web page (aspx page) in asp.net before it is being rendered. During debugging it shows that the tags removed but when the page gets loaded then the tags are generated again. How can I remove the tags ?
Here is the code I am using:-
protected override void Render(HtmlTextWriter writer)
{
var regex = new Regex("<!--SCRIPT[\\s\\S]*?REMOVE-->[\\s\\S]*?REMOVE-->", RegexOptions.Singleline | RegexOptions.IgnoreCase);
using (WebClient client = new WebClient())
{
string htmlCode = client.DownloadString(Server.MapPath("default.aspx"));
string output = regex.Replace(htmlCode, "");
}
}
Try this
String result = Regex.Replace(htmlDocument, #"<[^>]*>", String.Empty);
Or Try this
Regex regex = new Regex(#"</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>", RegexOptions.Singleline);
Source

Change rendered html

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
}

Access __VIEWSTATE & __EVENTVALIDATION in C#

In ASP.NET, is it possible to get the values of __VIEWSTATE and __EVENTVALIDATION hidden fields into a variable in C# (server side) in, let's say, overriding the Render method?
I have tried:
protected override void Render(HtmlTextWriter writer)
{
StringBuilder stringBuilder = new StringBuilder();
StringWriter stringWriter = new StringWriter(stringBuilder);
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
base.Render(htmlWriter);
string temp = stringBuilder.ToString();
}
This gives me the entire ASP.NET output. We can get the values by using a string function, but I did not find it a very clean solution. Is there a better way to do this?
What I actually want is the values of __VIEWSTATE & __EVENTVALIDATION when the first request is made and not after the postback is done. That is when the output stream if formed when the first request is made.
If you look at the Page class using Reflector, you'll see these hidden fields are created during the render phase (look at the methods RenderViewStateFields and EndFormRenderHiddenFields).
You could probably get at some/all of the data using reflection (e.g. the internal property Page.ClientState).
But I don't think there is a clean solution (though to be honest I don't really understand what you're trying to achieve).
To get the event validation you should use HTML Agility Pack.
var eventValidation = HapHelper.GetAttributeValue(htmlDocPreservation, "__EVENTVALIDATION", "value");
public static string GetAttributeValue(HtmlDocument doc, string inputName, string attrName)
{
string result = string.Empty;
var node = doc.DocumentNode.SelectSingleNode("//input[#name='" + inputName + "']");
if (node != null)
{
result = node.Attributes[attrName].Value;
}
return result;
}

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();
}
}

Categories