Textarea.Value Commit issue
-
Hi I am trying to use a textarea control to load some text into. I am then asking the user to update the text and then finally save it to a file. The problem is that the text saved is always the loaded text and not the text updated by the user.... Load code TextReader tr = new StreamReader(textpath); textarea1.Value = tr.ReadToEnd(); tr.Close(); Save code string fullpath = string.Format("{0}RichTextFile{1}.doc", tempfile, dateTime.Now.ToFileTime().ToString()); string htmltext = textarea1.Value; htmltext = htmltext.Replace("<STRONG>", "<B>"); htmltext = htmltext.Replace("</STRONG>", "</B>"); htmltext = htmltext.Replace("<EM>", "<I>"); htmltext = htmltext.Replace("</EM>", "</I>"); StreamWriter sw = new StreamWriter(fullpath); sw.Write(htmltext); sw.WriteLine("</html>"); sw.Close(); Any idea why it is not saving the updated text? :confused:
-
Hi I am trying to use a textarea control to load some text into. I am then asking the user to update the text and then finally save it to a file. The problem is that the text saved is always the loaded text and not the text updated by the user.... Load code TextReader tr = new StreamReader(textpath); textarea1.Value = tr.ReadToEnd(); tr.Close(); Save code string fullpath = string.Format("{0}RichTextFile{1}.doc", tempfile, dateTime.Now.ToFileTime().ToString()); string htmltext = textarea1.Value; htmltext = htmltext.Replace("<STRONG>", "<B>"); htmltext = htmltext.Replace("</STRONG>", "</B>"); htmltext = htmltext.Replace("<EM>", "<I>"); htmltext = htmltext.Replace("</EM>", "</I>"); StreamWriter sw = new StreamWriter(fullpath); sw.Write(htmltext); sw.WriteLine("</html>"); sw.Close(); Any idea why it is not saving the updated text? :confused:
Hi, You will need to ensure that the code to populate the TextArea is not being fired on PostBack. If it is then the control is updated with the old value prior to the button event firing i.e.
if (!IsPostBack) { TextReader tr = new StreamReader(textpath); textarea1.Value = tr.ReadToEnd(); tr.Close(); }
This will ensure your code is only run once when the page first loads. Hope this helps...Clean code is the key to happiness.