Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai All,

I wanna show the html text in one column of Grid View( This grid is binding from Database)



Thanks Advnce
Aatif
Posted
Updated 11-Jul-13 21:40pm
v3
Comments
Sushil Mate 12-Jul-13 0:55am    
you want to remove html tag from text & show normal text, you need to explain in detail about your problem.
Aatif Ali from Bangalore 12-Jul-13 1:21am    
No..i want to noraml text(some styles applied Text) in grid

1 solution

I got solution

first create the following method in .CS page and call thesse method from ASPX page while binding the data

For implementing this method we use two namespace
> using system.Text.RegularExpression
> using system.Text


C#
public string HTMLToText(string HTMLCode)
        {
            // Remove new lines since they are not visible in HTML
            HTMLCode = HTMLCode.Replace("\n", " ");

            // Remove tab spaces
            HTMLCode = HTMLCode.Replace("\t", " ");

            // Remove multiple white spaces from HTML
            HTMLCode = Regex.Replace(HTMLCode, "\\s+", " ");

            // Remove HEAD tag
            HTMLCode = Regex.Replace(HTMLCode, "<head.*?>", ""
                , RegexOptions.IgnoreCase | RegexOptions.Singleline);

            // Remove any JavaScript
            HTMLCode = Regex.Replace(HTMLCode, "<script.*?>", ""
                , RegexOptions.IgnoreCase | RegexOptions.Singleline);

            // Replace special characters like &, <, >, " etc.
           StringBuilder sbHTML = new StringBuilder(HTMLCode);
            // Note: There are many more special characters, these are just
            // most common. You can add new characters in this arrays if needed
            string[] OldWords = { " ", "&", """, "<", ">", "®", "©", "", "" };
            string[] NewWords = { " ", "&", "\"", "<", ">", "®", "©", "•", "™" };
            for (int i = 0; i < OldWords.Length; i++)
            {
                sbHTML.Replace(OldWords[i], NewWords[i]);
            }

            // Check if there are line breaks (<br>) or paragraph (<p>)
            sbHTML.Replace("<br>", "\n<br>");
            sbHTML.Replace("<br hold=" />            sbHTML.Replace("><p hold=" /><br mode="><[^>]+>", RegexOptions.IgnoreCase); 

            return System.Text.RegularExpressions.Regex.Replace(sbHTML.ToString(), @">", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

        }

Calling above method from ASPX Page

ASP.NET
<asp:label id="lblTrackDescription" runat="server" text="<%# HTMLToText(Eval("Description").ToString())%>" tooltip="<%# (Eval("Description"))%>" xmlns:asp="#unknown" /></p></br></br></br></p></br>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900