New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Save in File

The code example below demonstrates how to save content directly in the ASPX or ASCX file containing RadEditor for ASP.NET AJAX. This feature can greatly simplify development in many practical scenarios,such as when you have pages with more static content ( e.g. welcome messages, FAQ, company information, contacts etc) that can be equipped with an editor, without the need to be connected to a database.

The content will be saved inside the Content inner tags of RadEditor for ASP.NET AJAX so please remember to add the content tag to the editor declaration in the ASPX file so it can be correctly updated.In order for RadEditor for ASP.NET AJAX to save content directly to the control files, you have to give full permissions to the ASPNET / Network Service user for the folders and files in which the editor resides. You can find info on how to set the permissions at the following link: Setting ASPNET (Network Service) Permissions.

ASP.NET
<telerik:radeditor id="RadEditor1" runat="server">
	<content>
	   Please remember to add the content tag to the editor declaration in the ASPX file so it can be correctly updated
	</content>
</telerik:radeditor>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" /> 
C#
protected void btnSubmit_Click(object sender, EventArgs e)
{
	//To update the page with the editor,
	//call this function and pass a reference to the RadEditor control
	SaveInCurrentFile(RadEditor1);
}

private bool _contentUpdated = false;
private string _editorContent = string.Empty;

private string EditorMatchEvaluator(Match m)
{
	_contentUpdated = true;
	string newContent = m.Groups[1].Value + _editorContent + "</content>";
	return newContent;
}

private void SaveInCurrentFile(RadEditor editor)
{
	try
	{
		//read file
		string physicalFileName = MapPathSecure(this.AppRelativeVirtualPath);
		FileStream fs = new FileStream(physicalFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
		StreamReader sr = new StreamReader(fs);
		string pageContent = sr.ReadToEnd();

		//update content
		_contentUpdated = false;
		_editorContent = editor.Content;
		MatchEvaluator mEvaluator = new MatchEvaluator(EditorMatchEvaluator);
		pageContent = Regex.Replace(pageContent, "(<(\\w+):RadEditor[^>]+?ID\\s*=\\s*['\"]?" + editor.ID + "['\"]?[^>]*>[\\s\\S]*?<content>)[\\s\\S]*?</content>", mEvaluator, RegexOptions.IgnoreCase | RegexOptions.Compiled);
		//update file
		if (_contentUpdated)
		{
			fs.Seek(0, SeekOrigin.Begin);
			StreamWriter sw = new StreamWriter(fs);
			sw.BaseStream.SetLength(0);
			sw.Write(pageContent);
			sw.Flush();
			sw.Close();
		}
		else
		{
			sr.Close();
		}
	}
	catch (Exception)
	{
		//"The File Encoding you have provided is not supported!"
		//"Refresh the screen and try again!"
		//"Another process or user is using the resource (ascx/aspx file) you are trying to update."
		//"Check if the ASPNET user (IIS5) / NETWORK SERVICE account (IIS6) has write privileges for this file."
	}
} 

See Also

In this article
See Also
Not finding the help you need?
Contact Support