jithesh
jithesh

Reputation: 253

How to remove HTML tags from string using asp.net (Bold,strong,font names etc etc)

I am using CKEditor to enter details (The details contains html elements ). after that the details will be bind into a grid. In this I want to show this details without html tags in tooltip.

protected void grdStepMain_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int i = 0;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                i++;
                string description = cell.Text;
                if (cell.Text.Length > 8 && (i == 2))
                    cell.Text = cell.Text.Substring(0, 8) + "....";
              string newdescription = Regex.Replace(description, @"<[^>]+>|&nbsp;", "").Trim();
              cell.ToolTip = Regex.Replace(newdescription, @"<[^>]+>|&nbsp;", "").Trim();
            }

        }
    }

But its not working please help me. Example:

string str= " &lt;h1&gt;&lt;span style=&quot;font-family:courier new,courier,monospace&quot;&gt;&lt;strong&gt;457457&lt;/strong&gt; 544444444444457457457&lt;/span&gt;";

The result in tooltip should be 457457544444444444457457457

Please Help. Regards jithesh

Upvotes: 2

Views: 526

Answers (2)

jithesh
jithesh

Reputation: 253

 int i = 0;
            string newdescription = "";
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                foreach (TableCell cell in e.Row.Cells)
                {
                    i++;
                    string description = cell.Text;
                    if (cell.Text.Length > 8 && (i == 2))
                    {
                        string a = Regex.Replace(description, @"&lt;.+?&gt;|&nbsp;", "").Trim();
                        cell.Text = a.Substring(0, 8) + "....";
                    }
                    if (i == 2)
                    {
                        newdescription = Regex.Replace(description, @"&lt;.+?&gt;|&nbsp;", "").Trim();
                    }
                    cell.ToolTip = Regex.Replace(newdescription, @"&lt;.+?&gt;|&nbsp;", "").Trim();
                }
            }

Upvotes: 2

HEEN
HEEN

Reputation: 4721

IF you want to show the details without HTML tags, you can simply use as shown below in the BoundField of the gridview:

Here you go,

<asp:BoundField DataField="yourcolumnid" HeaderText="Column Text" ItemStyle-Width="30" HtmlEncode="false" />

Hope it helps

Upvotes: 0

Related Questions