Fundamental Concepts: Loading and Saving Documents

Documents can be easily loaded in the ASP.NET code-behind code using the Web.TextControl.LoadText and Web.TextControl.SaveText methods that accepts file names, FileStreams and memory variables such as byte and string arrays.

To load a document from a physical file, the following code is required:

[C#]
TextControl1.LoadText(Server.MapPath("/documents/sample.docx"), 
	TXTextControl.Web.StreamType.WordprocessingML);
[Visual Basic]
TextControl1.LoadText(Server.MapPath("/documents/sample.docx"), _
	TXTextControl.Web.StreamType.WordprocessingML)

In order to save a document, the SaveText method is used:

[C#]
TextControl1.SaveText(Server.MapPath("/output/" + sFilename), 
	TXTextControl.Web.StreamType.WordprocessingML);
[Visual Basic]
TextControl1.SaveText(Server.MapPath("/output/" + sFilename), _
	TXTextControl.Web.StreamType.WordprocessingML)

Web.TextControl must be initialized completely in order to accept LoadText calls. Therefore, it exposes another Load method: Web.TextControl.LoadTextAsync. This method loads the text asynchronously when the HTML5 Web Editor is initialized and loaded completely.

In order to load a document in the Page_Load event of an ASPX Web Form, the following code is used:

[C#]
protected void Page_Load(object sender, EventArgs e)
{
	TextControl1.LoadTextAsync(Server.MapPath("/documents/template.docx"),
		TXTextControl.Web.StreamType.WordprocessingML);
}
[Visual Basic]
Protected Sub Page_Load(sender As Object, e As EventArgs)
	TextControl1.LoadTextAsync(Server.MapPath("/documents/template.docx"), _
		TXTextControl.Web.StreamType.WordprocessingML)
End Sub

Web.TextControl is updated and synchronized with the server automatically using the Web Sockets protocol. In case, you want to use other server-side (runat="server") elements such as buttons on the same form, make sure that they are embedded in an AJAX UpdatePanel and doesn't post back the complete page. A typical body would look like this:

<body>
 <form id="form1" runat="server">
  <div>
   <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
   <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
     <asp:Button ID="Button1" runat="server" Text="Load Document" />
    </ContentTemplate>
   </asp:UpdatePanel>
   <cc1:TextControl ID="TextControl1" runat="server" />
  </div>
 </form>
</body>

Loading and saving documents is shown in detail in the Sample Explorer that is explained in the article Example - Loading and Saving.