关于asp.net生成静态页循环数据的问题
<div class="j_rt2">
<b>{position}</b>
<div class="j_rt2_txt">岗位职责:{duties}
</div>
</div>
我想循环生成这段代码怎么弄啊!现在生成的是数据库中最后一条
cs文件如下:
//这个是一个按钮事件——生成静态页
protected void html_button_Click(object sender, EventArgs e)
{
string selete = "select position,duties from news where title='招聘信息'";
SqlDataAdapter da = new SqlDataAdapter(selete, sqlconn);
DataSet ds = new DataSet();
da.Fill(ds);
string position = ds.Tables[0].Rows[0][0].ToString();
string duties = ds.Tables[0].Rows[0][1].ToString();
StringBuilder strHtml = new StringBuilder();
for (int i = 0; i <= ds.Tables[0].Rows.Count; i++)
{
OutputHtml(position, duties);
}
}
private void OutputHtml(string position, string duties)
{
string FName = "job.htm";
string HtmlPath = String.Format(@"/HtmlLink/{0}", FName);
string HtmlTemp = File.ReadAllText(Server.MapPath("/HtmlTemp/job.htm"), Encoding.UTF8);
StringBuilder SBuilder = new StringBuilder(HtmlTemp);
SBuilder.Replace("{position}", position);
SBuilder.Replace("{duties}", duties);
StreamWriter SWriter = File.CreateText(Server.MapPath("/HtmlLink/") + FName);
SWriter.WriteLine(SBuilder.ToString());
SWriter.Flush();
SWriter.Close();
}
------解决方案--------------------把这段代码封装成一个方法,在button_Click事件中循环调用该方法!
------解决方案--------------------
你的stream打开关闭位置不要放到循环体内
代码修改了下,如下:
C# code
protected void html_button_Click(object sender, EventArgs e)
{
string selete = "select position,duties from news where title='招聘信息'";
SqlDataAdapter da = new SqlDataAdapter(selete, sqlconn);
DataSet ds = new DataSet();
da.Fill(ds);
string position = ds.Tables[0].Rows[0][0].ToString();
string duties = ds.Tables[0].Rows[0][1].ToString();
StringBuilder strHtml = new StringBuilder();
StreamWriter SWriter = File.CreateText(Server.MapPath("/HtmlLink/") + FName);//在写到stream前就打开
for (int i = 0; i <= ds.Tables[0].Rows.Count; i++)
{
OutputHtml(position, duties);
}
//全部写完之后再关闭stream
SWriter.Flush();
SWriter.Close();
}
private void OutputHtml(string position, string duties)
{
string FName = "job.htm";
string HtmlPath = String.Format(@"/HtmlLink/{0}", FName);
string HtmlTemp = File.ReadAllText(Server.MapPath("/HtmlTemp/job.htm"), Encoding.UTF8);
StringBuilder SBuilder = new StringBuilder(HtmlTemp);
SBuilder.Replace("{position}", position);
SBuilder.Replace("{duties}", duties);
SWriter.WriteLine(SBuilder.ToString());
}