How to display HTML encoded data in radeditor and other controls in hTML decoded form?

8 posts, 0 answers
  1. soft dazy
    soft dazy avatar
    5 posts
    Member since:
    Dec 2009

    Posted 17 Dec 2009 Link to this post

    Hi,

    I have a SQL database table with a description column which has HTML encoded data. I want to display this table's each row in an asp.net 3.5 details view control.
    This details view has an template field which displays this column's data. This template field has an aspx Lable control which display's data when the details view is in view mode and a RadEditor which display's data when details view is in edit mode.

    But both of these controls display data with HTML tags, but I want to display the data in HTML decoded form. Like plain text with html tag applied on the text, instead of showing along with text in the editor and label.
     
    Here is the code-

    <asp:DetailsView ID="dv" runat="server" Width="748px" DataSourceID="dvSqlDataSource"
                                 DataKeyNames="Id" AutoGenerateRows="False"
            HeaderText="Details" CellPadding="4">    
            <Fields>
             <asp:BoundField DataField="Id" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
    <asp:TemplateField HeaderText="Description" SortExpression="Description" >
              <ItemTemplate>
                    <asp:Label ID="lblDescription" runat="server" Text='<%# Eval("description") %>' Width="580px"></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <telerik:RadEditor ID="RadEditDesc" SkinID="Edit" runat="server"  ToolsFile="~/Editor/BasicTools.xml" EditModes="Design,Preview" Width="580px" Content='<%#Bind("description")%>' />
                    <asp:RequiredFieldValidator ID="valRequireDesc" runat="server" ControlToValidate="RadEditDesc"
                             Display="Dynamic" ErrorMessage="Required" SetFocusOnError="true"  ToolTip="Description is required."  />
                </EditItemTemplate>        
             </asp:TemplateField>       
             
                <asp:CommandField ShowEditButton="True" />       
             
            </Fields>
            <EmptyDataTemplate>No description found!</EmptyDataTemplate>
           </asp:DetailsView>
    <asp:SqlDataSource ID="dvSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
            SelectCommand="SELECT [ld], [description],FROM [tbldescripion]
     
           UpdateCommand="UPDATE [tbldescription] SET [description] = @description  WHERE [Id] = @Id">
            <UpdateParameters>
                 <asp:Parameter Name="Id" Type="Int32" />
     <asp:Parameter Name="description" Type="String" />
            </UpdateParameters>       
        </asp:SqlDataSource>

    I have handled this event handler for decoding the text of label control  but It isn't working

    protected void dvDeal_OnDataBound(object sender, EventArgs e)
        {
            DetailsView d = (DetailsView)sender;
            foreach (DetailsViewRow row in d.Rows)

            {
                if (row.RowType == DataControlRowType.DataRow)
                {

                    foreach (Control c in row.Cells)
                    {
                        foreach (Control c1 in c.Controls)
                        {
                            if (c1.GetType() == typeof(Label))
                            {
                                Label l = (Label)c1;
                                l.Text = System.Web.HttpUtility.HtmlDecode(l.Text);
                            }


                      }
              }
            }
     }

    Anybody knows, how can we display a html encoded string into the lable control and in RadEditor as well in html decoded form. So that the end user can easily view and edit the description without looking onto these confusing html tags.

    Any help will be really appreciated.  

    Regards,
    dazy
  2. soft dazy
    soft dazy avatar
    5 posts
    Member since:
    Dec 2009

    Posted 17 Dec 2009 Link to this post

    Hi,

    Can anybody help? i'm still waiting :(.

    Regards,
    dazy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Dobromir
    Admin
    Dobromir avatar
    1633 posts

    Posted 18 Dec 2009 Link to this post

    Hi dazy,

    If I understand your question correctly you have a string in the database looking like this:
    &lt;span&gt;Some Sample Text&lt;/span&gt;
    and you want to display it in the label and/or RadEditor with entities. In order to do that you need to encode the string twice because it is still HTML Markup and is rendered by the browsers(& needs to be encoded as &amp;).

    I hope this helps solving the issue.

    Greetings,
    Dobromir
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. soft dazy
    soft dazy avatar
    5 posts
    Member since:
    Dec 2009

    Posted 18 Dec 2009 Link to this post

    Hi,

    Thanks for your kind reply!

    Yes you are right I have a string in data base like this
    &lt;span&gt;Some Sample Text&lt;/span&gt;

    As I have mentioned before, I was thinking to Decode this string to display it in RadEditor and Lable controls.

    But you are suggesting to Encode the string twice.

    So I have modified the code like below only for lable control -

    protected void dv_ItemCreated(object sender, DetailsViewCommandEventArgs e)
        {
            DetailsView d = (DetailsView)sender;
            foreach (DetailsViewRow row in d.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {

                    foreach (Control c in row.Cells)
                    {
                        foreach (Control c1 in c.Controls)
                        {
                            if (c1.GetType() == typeof(Label))
                            {
                                Label l = (Label)c1;
                                l.Text = System.Web.HttpUtility.HtmlEncode(l.Text);
                                l.Text = System.Web.HttpUtility.HtmlEncode(l.Text);
                            }
                        }
                    }
                }
            }
        }

    But It does not have any effect on the output.

    Can you tell me what will be the code for encoding db string twice and displaying entities in both the Lable and RadEditor controls.

    Thanks in advance.

    Regards,
    dazy
  6. soft dazy
    soft dazy avatar
    5 posts
    Member since:
    Dec 2009

    Posted 18 Dec 2009 Link to this post

    Hi,

    I have modified the code and added a new function in code behind -

    public string GetHtmlDecode(object obj)
         {
             string content;
          
             content = obj.ToString();
                 if (!(String.IsNullOrEmpty(content)))
                 {
                     content = HttpUtility.HtmlDecode(content);
                 }
             return content;
         }

    and I called this function in the following part of details view-

    <asp:TemplateField HeaderText="Description" SortExpression="Description" >
              <ItemTemplate>
                    <asp:Label ID="lblDescription" runat="server" Text='<%#  GetHtmlDecode(Eval("description")) %>' Width="580px"></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <telerik:RadEditor ID="RadEditDesc" SkinID="Edit" runat="server"  ToolsFile="~/Editor/BasicTools.xml" EditModes="Design,Preview" Width="580px" Content='<%# GetHtmlDecode(Bind("description")) %>' />
                    <asp:RequiredFieldValidator ID="valRequireDesc" runat="server" ControlToValidate="RadEditDesc"
                             Display="Dynamic" ErrorMessage="Required" SetFocusOnError="true"  ToolTip="Description is required."  />
                </EditItemTemplate>        
             </asp:TemplateField>       

    I ran the above code it gave me this error-

    Compiler Error Message: CS0103: The name 'Bind' does not exist in the current context


    And was pointing to the following line of code-


     <telerik:RadEditor ID="RadEditDesc" SkinID="Edit" runat="server"  ToolsFile="~/Editor/BasicTools.xml"

    EditModes="Design,Preview" Width="580px" Content='<%# GetHtmlDecode(Bind("description")) %>'/>

    Then I commented the above line of code and ran the project and then label was perfectly displaying a decoded string.

    But I also want to display this decoded string in RadEditor so that the end user can edit this string and can save it back to database again in encoded form

    Basically it is working fine for the asp 3.5 Lable control but not for the RadEditor control.


    Do you know how can I fix the above error and make it work for the RadEditor too?

    Regards,

    dazy
  7. Dobromir
    Admin
    Dobromir avatar
    1633 posts

    Posted 21 Dec 2009 Link to this post

    Hi Dazy,

    This error looks like a general ASP.NET issue but unfortunately I am not aware of it.
    Nevertheless I would suggest to use Eval instead of Bind in the RadEditor's declaration, and also Encode the data when saving it to the database.

    In addition you can check the following demo which shows how to use Databind Eval : Editor / Datagrid Edit Template.

    If you experience any other problems, please open a support ticket and attach a sample fully working project so we can examine it and hopefully provide a solution.

    Kind regards,
    Dobromir
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  8. Bohuslav Borivoj
    Bohuslav Borivoj avatar
    14 posts
    Member since:
    Aug 2006

    Posted 11 Jul 2011 Link to this post

    Hello, I have similar solution as a Soft Dazy - I need show text (inserted by RADEditor) to quest, but only as a preview - just show him for example only 1000chars. Decode from this topic works as well, but when I will try return limited chars text is shorter but html formated
    again.

    here is my code :

    <asp:label ID="pageDatalbl" runat="server" Text='<%#  GetHtmlDecode(Eval("pageData")) %>' ></asp:label>

    public string GetHtmlDecode(object obj)
    {
        string content; 
        content = obj.ToString();
        if (!(String.IsNullOrEmpty(content)))
        {
            content = HttpUtility.HtmlDecode(content);
            return ((string)obj).Substring(0, 1000) + ".....";
        }
        return content;
    }

    When i delete from cs file line
     "return ((string)obj).Substring(0, 1000) + ".....";"
    its complete text but Decoded as well, when i put that back
     its 1000chars long but html formated again. I think that there will
     be some easy solution but i am not able to find him.
     Thanks a lot for any suggestions or help. BB
  9. Patrick
    Patrick avatar
    5 posts
    Member since:
    Oct 2008

    Posted 22 May 2013 Link to this post

    I know it's an old post, but it's still an issue i such situations... Here's my workaround..

    Reading the Request.Form value got meg the same issue as described here, however, my solution was simular, but I had to use HttpUtility.UrlDecode instead of HttpUtility.HtmlDecode... Then the "garbage" disappeared :-)

    jm2c...
    P@
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017