Click here to Skip to main content
15,897,334 members
Articles / Web Development / ASP.NET

Editable Panel/Text Box on a Webpage with a Capability to Display HTML Text Inside it

Rate me:
Please Sign up or sign in to vote.
4.75/5 (3 votes)
7 Apr 2010CPOL3 min read 42.2K   530   9  
editable panel/text box on a webpage with a capability to display HTML text inside it
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
    .editable {
        border-top-width : 1px;
        border-right-width : 1px;
        border-bottom-width : 1px;
        border-left-width : 1px;
        border-style  : dotted;
        white-space:pre-line;
        width:500px;
        height:200px;
        overflow:scroll;
        overflow-x:hidden;
        }
    </style>
    <script type="text/javascript">

        function ReplaceTags(xStr) {
            var regExp = /<\/?[^>]+>/gi;
            xStr = xStr.replace(/<\/P>/gi, "\r\n");
            xStr = xStr.replace(/<BR>/gi, "\r\n");
            xStr = xStr.replace(/<P>/gi, "");
            xStr = xStr.replace(regExp, "");
            return xStr;
        }
        function onsaveclicked() {
            var reqDiv = document.getElementById("<%=request.ClientID %>");
            var hhrequestBox = document.getElementById("<%=hrequest.ClientID %>");
            var responseText = ReplaceTags(reqDiv.innerHTML);
            hhrequestBox.value = responseText;
            
            return true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <div id="TextEdit" class="editable">
            <div id="request" runat="server" contenteditable="true"></div>
            <div id="requestInner" style="border-left:solid 1px gray; margin-left:10px; margin-right:10px;" 
                contenteditable="false" UNSELECTABLE="ON"><b>I am the inner div</b><br />with multple lines. You can check me on this.<hr />
                This is the next line of text in <b>bold</b>, <i>Italic</i> and 
                <span style="text-decoration:underline">underlined</span>. Click here for more details <a target="_blank" href="http://www.articledean.com">Articledean.com</a></div>
        </div>
        <asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return onsaveclicked();" onclick="btnSave_Click" />
        <br />
        <textarea id="hrequest" runat="server" cols="50" rows="10" style="display:none;"></textarea> <br />
        Server Response:<br />
        Output in a Div (With out saving the format):<br />
        <div style="border:solid 1px gray; width:300px; " id="response" runat="server"></div><br />
        Output in a textbox (With the format):<br /><textarea id="txtoutput" runat="server" cols="50" rows="10"></textarea>
    </div>
    </form>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Founder Articledean.com & conveygreetings.com
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions