c# - System.IO.File.WriteAllText will not write to a file -



c# - System.IO.File.WriteAllText will not write to a file -

i've created method when button (butcommit) clicked, value of textbox taken , stored string. string written .txt file using writealltext method. however, when method executed, file not modified @ all.

the button method working fine, have response.redirect method in there works every time. path .txt file right have method will, on page load, display current contents of .txt file (using readalltext method) , exact same path using writealltext method.

here code giving me problems:

void butcommit_click(object sender, eventargs e) { var path = server.mappath(@"~/content.txt"); string content = txthomepagecontent.text; system.io.file.writealltext(path, content); response.redirect("default.aspx"); }

i'll repeat again: above method initialized , works fine response.redirect method, not writealltext method.

edit more clarity:

the purpose of display message on home page of site. message required stored in .txt file on server (named content.txt). in controlpanel.aspx page, user needs able alter context of text file using text box. preferably able view in content.txt using same text box.

the code have viewing bit this:

protected void page_load(object sender, eventargs e) { var path = server.mappath(@"~/content.txt"); string content = system.io.file.readalltext(path); txthomepagecontent.text = content; }

this specifies "path" path content.txt, specifies "content" string containing content.txt's data. after places "content" textbox txthomepagecontent. text box used input new info content.txt.

it seems if remove above section of code, able write text file no problems, of course of study unable view in there first. if leave above code chunk in, whatever write in textbox ignored, , original text submitted content.txt, resulting in no changes.

i understand there easier ways of doing such storing in database, requirements in .txt file.

you can either utilize page_init event handler instead of page_load initialize text box (since submitted postback values load between init , load events, overwriting text box value submitted), or if want remain page_load, check whether there postback first:

protected void page_load(object sender, eventargs e) { if (this.page.ispostback) return; var path = server.mappath(@"~/content.txt"); string content = system.io.file.readalltext(path); txthomepagecontent.text = content; }

c# asp.net

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -