首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

关于写入文件有关问题

2011-12-23 
关于写入文件问题我有2个textbox,1个用来显示文件的内容,另一个用于修改,程序如下%@PageLanguage C# A

关于写入文件问题
我有2个textbox,1个用来显示文件的内容,另一个用于修改,程序如下


<%@   Page   Language= "C# "   AutoEventWireup= "true "   CodeFile= "HtmlTo.aspx.cs "   Inherits= "HtmlTo "   validateRequest= "false "     %>

<!DOCTYPE   html   PUBLIC   "-//W3C//DTD   XHTML   1.0   Transitional//EN "   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<html   xmlns= "http://www.w3.org/1999/xhtml "   >
<head   runat= "server ">
        <title> 无标题页 </title>
</head>
<body>
        <form   id= "form1 "   runat= "server ">
        <div>
                <asp:DropDownList   ID= "DropDownList1 "   runat= "server "   AutoPostBack= "True ">
                        <asp:ListItem   Value= "1.html "> 计算机科学与技术 </asp:ListItem>
                        <asp:ListItem   Value= "2.html "> 生物工程 </asp:ListItem>
                        <asp:ListItem   Value= "3.html "> 国际经济与贸易 </asp:ListItem>
                        <asp:ListItem   Value= "4.html "> 园林 </asp:ListItem>
                </asp:DropDownList> <br   />
                <br   />
                <asp:TextBox   ID= "TextBox1 "   runat= "server "   Height= "480px "   TextMode= "MultiLine "   Width= "386px "> </asp:TextBox> &nbsp; <br   />
                <asp:TextBox   ID= "TextBox2 "   runat= "server "> </asp:TextBox> <br   />
                <br   />
                <asp:Button   ID= "Button1 "   runat= "server "   Text= "确定 "   OnClick= "Button1_Click "   /> </div>
        </form>
</body>
</html>

---------------------

using   System;
using   System.Data;
using   System.Configuration;
using   System.Collections;
using   System.Web;
using   System.Web.Security;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.WebControls.WebParts;
using   System.Web.UI.HtmlControls;
using   System.IO;

public   partial   class   HtmlTo   :   System.Web.UI.Page
{
        protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                string   filename;
                filename   =   DropDownList1.SelectedValue;


                StreamReader   sr   =   new   StreamReader(Server.MapPath( " ")   +   "\\ "   +   filename,   System.Text.Encoding.GetEncoding( "GB2312 "));
                TextBox1.Text   =   sr.ReadToEnd();
                sr.Close();
                //释放资源
                sr.Dispose();

        }
        protected   void   Button1_Click(object   sender,   EventArgs   e)
        {
               
                File.WriteAllText(Server.MapPath( " ")   +   "\\ "   +   "1.html ",   TextBox2.Text,   System.Text.Encoding.GetEncoding( "GB2312 "));
             
        }
}


但是我每次都得点2下 "确定 ",才能修改成功,请问是什么原因

[解决办法]
其实你点击头一次的时候就已经修改成功了,只不过你看一下你的页面逻辑,点确定的时候会首先执行Page_Load,它会把还未修改的内容次读取到TextBox1里,然后再执行Button1_Click修改内容,当你再一次点击的时候,就会把修改后的内容读取到TextBox1里,你看着好像是点两次才能修改成功。

简单修改一下,没有测试不知道行不行,大概就是这个意思
protected void Page_Load(object sender, EventArgs e)
{
if(!isPostBack) FillText();
}
protected void FillText()
{
string filename;
filename = DropDownList1.SelectedValue;

StreamReader sr = new StreamReader(Server.MapPath( " ") + "\\ " + filename, System.Text.Encoding.GetEncoding( "GB2312 "));
TextBox1.Text = sr.ReadToEnd();
sr.Close();
//释放资源
sr.Dispose();
}
protected void Button1_Click(object sender, EventArgs e)
{

File.WriteAllText(Server.MapPath( " ") + "\\ " + "1.html ", TextBox2.Text, System.Text.Encoding.GetEncoding( "GB2312 "));
FillText();
}
[解决办法]
Button1_Click在Page_Load之后执行,第一次提交时其实1.html已经被修改了,只是TextBox1中读取的是未修改的内容,应该把设置TextBox1内容的代码放在Button1_Click之后,比如:OnPreRender中

热点排行