Search code examples
c#asp.netwebformssummernote

Is it possible to get value from Summernote editor in asp.net web forms?


I'm using summernote editor, and I want to retrieve its content in code behind. I tried to use the following ways , but it returns empty(null)

1-

default.aspx

<div class="summernote"  id="txtTest" runat="server"></div>

Code behind:

string content= txtTest.InnerText;

2-

default.aspx

<div class="summernote"  id="txtTest" ></div>

Code behind:

string name = Request.Form["txtTest"];

Any suggestions?


Solution

  • Have you considered using a <textarea> to handle this as opposed to a <div>? Usually they are a bit easier to use with respect to storing values (as they are designed to do so) :

    <textarea id="txtTest" runat="server"></textarea>
    

    One of the ways that you might handle this would be to register an event using Summernote's available API to set the HTML content for your <textarea> element whenever focus was lost (e.g. the onblur) :

    <script>
        $(function () {
            // Set up your summernote instance
            $("#txtTest").summernote();
            // When the summernote instance loses focus, update the content of your <textarea>
            $("#txtTest").on('summernote.blur', function () {
                $('#txtTest').html($('#txtTest').summernote('code'));
            });
        });
    </script>
    

    Since you are likely going to be storing HTML content within the element, you'll likely want to ensure that .NET doesn't think you are trying to exploit it. You can do this by either explicitly setting this page to ignore that process by updating the ValidateRequest property of your page :

    <%@ Page ... ValidateRequest = "false" %> 
    

    Or you could try simply escaping the content that is being set :

    $('#txtTest').html(escape($('#txtTest').summernote('code')));