private string ReplaceElements(string html) { string patten = @"<\s*div[^>]*>(.*?)<\s*/div\s*>"; string wrapper = @"<div class=""Highlighter"">{0}</div>"; MatchCollection collection = Regex.Matches(html, patten); foreach (Match match in collection) { string value = match.Value; int marker = value.IndexOf(">"); string innterHtml = value.Substring(marker + 1, value.Length - (marker + 7)); if (Regex.Match(innterHtml, patten).Success) innterHtml = this.ReplaceElements(innterHtml); string wrappedText = string.Format(wrapper, innterHtml); string modifiedValue = value.Replace(innterHtml, wrappedText); html = html.Replace(value, modifiedValue); } return html; } protected override void Render(HtmlTextWriter writer) { string html = @" <html> <head> <title>Test Page</title> <style> div.Highlighter { border:solid 1px #f00; margin:10px; } </style> </head> <body> <div class=""Div""><p>Hello World</p></div> <div class=""Div"" runat=""server"">Div 1</div> <div class=""Div"" id=""LoginId"">Div 2</div> <div class=""Div"" width=""50%"">Div 3</div> <div class=""Div"">Div 4</div> </body> </html> "; writer.Write("<h1>Before</h1>"); writer.Write("<hr />"); writer.Write(html); writer.Write("<h1>After</h1>"); writer.Write("<hr />"); html = this.ReplaceElements(html); writer.Write(html); }
Wednesday, June 23, 2010
How to replace html content inside html using c# - Asp.net
This example demonstrate how to replace HTML element content (innerHtml) using c#. I have used regular expressions to match elements. However this example does not support nested eleements:
Subscribe to:
Post Comments (Atom)
Azure Storage Account Types
Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...
-
Why we need asynchronous tasks? Execute a time consuming operations in parallel to the CLR thread which execute the request If you have ...
-
Demo: I was thinking a way to show images before actually uploading them to server. I would say to preview images using javascript. Obv...
No comments:
Post a Comment