Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a TextArea displaying textfile's content for editing. A button to save content again in textfile.

XML
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />

    <textarea id="code" name="code" runat="server"  wrap="off" rows="30" cols="100" >
                                      </textarea>


And in code behind:

C#
private string path;
       protected void Page_Load(object sender, EventArgs e)
       {
           code.Attributes.Add("Style", "display:block");

           path = Request.QueryString["path"];// path= "D:\\C#Projects\\website\\Lecturer\\giangvien\\ProxyClass.txt"
           string content = File.ReadAllText(path);
               code.Value = content;
       }

       protected void btnSave_Click(object sender, EventArgs e)
       {
           string newcontent = code.Value;
           File.WriteAllText(path,newcontent);
       }


All I want is: I edit the ProxyClass.txt content displayed in the TextArea `code` then click on button Save so `ProxyClass.txt` is saved again with new content.

But after I edit the `content` and then click `Save`, the `saved-content` is still the `old content` (no change).

Try to debug, the newcontent is the first content of the textfile.

Why cannot I save the new content???

I just think that using `string newcontent= code.Value;` can get the current text in the TextArea.

Is there any mistake in my code above. I need your opinion.

Help!!!
Posted
Updated 24-Oct-13 6:02am
v3

1 solution

You are reading file on each postback and assigning it's content to textarea - overwriting changed content posted from browser.
Try:
C#
protected void Page_Load(object sender, EventArgs e)
{
    code.Attributes.Add("Style", "display:block");

    path = Request.QueryString["path"];// path= "D:\\C#Projects\\website\\Lecturer\\giangvien\\ProxyClass.txt"
    if (!IsPostBack) {
        string content = File.ReadAllText(path);
        code.Value = content;
    }
}
 
Share this answer
 
Comments
Vy Clarks 24-Oct-13 13:06pm    
thanks, now it works fine, thank again

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900