Home / Community & Support / Knowledge Base / RadControls for ASP.NET AJAX / Editor / Saving RadEditor "Prometheus" content directly in the ASPX or ASCX file containing the editor

Saving RadEditor "Prometheus" content directly in the ASPX or ASCX file containing the editor

Article Info

Rating: Not rated

Article information

Article relates to

 RadEditor for ASP.NET AJAX
 Telerik.Web.UI

Created by

 Rumen Jekov

Last modified

 2009/02/24

Last modified by

 Rumen Jekov


HOW-TO
Saving RadEditor "Prometheus" content directly in the ASPX or ASCX file containing the editor.

DESCRIPTION
The code example below demonstrates how to save content directly in the ASPX or ASCX file containing RadEditor "Prometheus". 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.

NOTES
The content will be saved inside the Content inner tags of RadEditor "Prometheus" 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 "Prometheus" 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.

SOLUTION

ASPX/ASCX

<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, Encoding.UTF8);
            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."
    
}
}


 

 

Comments

There are no comments yet.
If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.