在Textarea中编辑文本文件的内容并在文本文件中再次保存时出现问题 [英] Got problem when edit textfile's content in Textarea and save it again in textfile
问题描述
我有一个TextArea显示文本文件的内容以供编辑。用于在文本文件中再次保存内容的按钮。
I have a TextArea displaying textfile's content for editing. A button to save content again in textfile.
<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:
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);
}
我想要的是:我编辑TextArea`代码中显示的ProxyClass.txt内容`然后单击按钮保存,以便再次使用新内容保存`ProxyClass.txt`。
但是在我编辑`content`然后点击`Save`之后, `saved-content`仍然是'旧内容'(没有变化)。
尝试调试,newcontent是文本文件的第一个内容。 />
为什么我不能保存新内容???
我只是认为使用`string newcontent = code .Value;`可以在TextArea中获取当前文本。
上面的代码中是否有任何错误。我需要你的意见。
帮助!!!
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!!!
推荐答案
你正在读取每个回发的文件和将其内容分配给textarea - 覆盖从浏览器发布的更改内容。
尝试:
You are reading file on each postback and assigning it's content to textarea - overwriting changed content posted from browser.
Try:
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;
}
}
这篇关于在Textarea中编辑文本文件的内容并在文本文件中再次保存时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!