if a text box contain contents of a file like code of a aspx file. now we want to edit that code which is displayed on that text box what "should be code behind edit button"?? and after "that file should be saved and overwrite in the same location from which it is opened previously".

Dani AI

Generated

Summary: asked how to let a textbox show a file (for example an .aspx) and then save edits back to the original file. steered the thread toward a file-based save, and suggested a richer editor control for a better editing experience. Below is a concise, practical pattern for ASP.NET WebForms: load the file into a multiline editor on first load, and on save write the editor text back to disk with a simple backup and error handling.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string virtualPath = "~/Files/EditableFile.aspx"; // validate / restrict this value
        string physicalPath = Server.MapPath(virtualPath);
        try
        {
            EditorTextBox.Text = System.IO.File.ReadAllText(physicalPath);
            ViewState["EditingPath"] = virtualPath;
        }
        catch (Exception ex)
        {
            // log the error (do not reveal server paths to users)
        }
    }
}

protected void SaveButton_Click(object sender, EventArgs e)
{
    string virtualPath = (string)ViewState["EditingPath"];
    string physicalPath = Server.MapPath(virtualPath);
    try
    {
        System.IO.File.Copy(physicalPath, physicalPath + ".bak", true);
        System.IO.File.WriteAllText(physicalPath, EditorTextBox.Text, System.Text.Encoding.UTF8);
    }
    catch (Exception ex)
    {
        // log the error
    }
}

Cautions and links: restrict editable files to a known folder and never accept arbitrary paths from user input (see OWASP on path traversal). Ensure the IIS application pool identity has write permission to the folder; editing files under the app root can force recompilation or break the site, so consider storing templates outside the web app or in a database. See Server.MapPath, File.WriteAllText, , and Path traversal for details.

Recommended Answers

All 4 Replies

Can you please rephrase the question.

It sounds like you want open a file and display it's contents in a text box, make changes to the text and then save it back to the same location. If that is correct, all you need to do in the save bottom is use a stream writer to convert the new text into a stream and save to a file.

yes u got right. can u plz send the code

Rather than using textbox you should go for Richtextbox control...
Load the file in Richtextbox using loadfile() and SaveFile() to save file

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.