在asp.net 2.0中,有时候需要对asp.net生成的html代码进行处理,或者是保存成静态文件。asp.net 提供了直接将请求保存成文件的方法:httprequest.saveas方法。下面这个方法就是在asp.net 2.0中得到asp.net生成的html代码,同时,生成一个静态文件的方法。
c#
<%@ page language=“c#“ autoeventwireup=“true“ %>
<!doctype html public “-//w3c//dtd xhtml 1.0 transitional//en”
“http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd”> <script runat=”server”>
// 计算数据,完全可以从数据看取得
icollection createdatasource( )
{
system.data.datatable dt = new system.data.datatable();
system.data.datarow dr;
dt.columns.add(new system.data.datacolumn(“学生班级“, typeof(system.string)));
dt.columns.add(new system.data.datacolumn(“学生姓名“, typeof(system.string)));
dt.columns.add(new system.data.datacolumn(“语文“, typeof(system.decimal)));
dt.columns.add(new system.data.datacolumn(“数学“, typeof(system.decimal)));
dt.columns.add(new system.data.datacolumn(“英语“, typeof(system.decimal)));
dt.columns.add(new system.data.datacolumn(“计算机“, typeof(system.decimal)));
{
system.random rd = new system.random(environment.tickcount * i); ;
dr = dt.newrow();
dr[0] = “班级“ + i.tostring();
dr[1] = “【孟子e章】“ + i.tostring();
dr[2] = system.math.round(rd.nextdouble() * 100, 2);
dr[3] = system.math.round(rd.nextdouble() * 100, 2);
dr[4] = system.math.round(rd.nextdouble() * 100, 2);
dr[5] = system.math.round(rd.nextdouble() * 100, 2);
dt.rows.add(dr);
}
system.data.dataview dv = new system.data.dataview(dt);
return dv;
}
{
if (!ispostback)
{
gridview1.datasource = createdatasource();
gridview1.databind();
}
}
{
system.io.stringwriter html = new system.io.stringwriter();
system.web.ui.htmltextwriter tw = new system.web.ui.htmltextwriter(html);
base.render(tw);
system.io.streamwriter sw;
sw = new system.io.streamwriter(server.mappath(“a.htm“), false, system.text.encoding.default);
sw.write(html.tostring());
sw.close();
tw.close();
response.write(html.tostring());
}
</script> <html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>得到asp.net生成的html</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<asp:gridview id=”gridview1″ runat=”server” font-size=”12px” backcolor=”#ffffff”
gridlines=”both” cellpadding=”4″ width=”560″>
<headerstyle backcolor=”#ededed” height=”26px” />
</asp:gridview>
</form>
</body>
</html>
“http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd”> <script runat=”server”>
// 计算数据,完全可以从数据看取得
icollection createdatasource( )
{
system.data.datatable dt = new system.data.datatable();
system.data.datarow dr;
dt.columns.add(new system.data.datacolumn(“学生班级“, typeof(system.string)));
dt.columns.add(new system.data.datacolumn(“学生姓名“, typeof(system.string)));
dt.columns.add(new system.data.datacolumn(“语文“, typeof(system.decimal)));
dt.columns.add(new system.data.datacolumn(“数学“, typeof(system.decimal)));
dt.columns.add(new system.data.datacolumn(“英语“, typeof(system.decimal)));
dt.columns.add(new system.data.datacolumn(“计算机“, typeof(system.decimal)));
for (int i = 0 ; i < 50 ; i++)
{
system.random rd = new system.random(environment.tickcount * i); ;
dr = dt.newrow();
dr[0] = “班级“ + i.tostring();
dr[1] = “【孟子e章】“ + i.tostring();
dr[2] = system.math.round(rd.nextdouble() * 100, 2);
dr[3] = system.math.round(rd.nextdouble() * 100, 2);
dr[4] = system.math.round(rd.nextdouble() * 100, 2);
dr[5] = system.math.round(rd.nextdouble() * 100, 2);
dt.rows.add(dr);
}
system.data.dataview dv = new system.data.dataview(dt);
return dv;
}
protected
void page_load( object sender, eventargs e ){
if (!ispostback)
{
gridview1.datasource = createdatasource();
gridview1.databind();
}
}
protected override
void render( htmltextwriter writer ){
system.io.stringwriter html = new system.io.stringwriter();
system.web.ui.htmltextwriter tw = new system.web.ui.htmltextwriter(html);
base.render(tw);
system.io.streamwriter sw;
sw = new system.io.streamwriter(server.mappath(“a.htm“), false, system.text.encoding.default);
sw.write(html.tostring());
sw.close();
tw.close();
response.write(html.tostring());
}
</script> <html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>得到asp.net生成的html</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<asp:gridview id=”gridview1″ runat=”server” font-size=”12px” backcolor=”#ffffff”
gridlines=”both” cellpadding=”4″ width=”560″>
<headerstyle backcolor=”#ededed” height=”26px” />
</asp:gridview>
</form>
</body>
</html>
vb.net
<%@ page language=“vb“ %>
<!doctype html public “-//w3c//dtd xhtml 1.0 transitional//en”
“http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd”> <script runat=”server”>
function createdatasource() as icollection
dim dt as system.data.datatable = new system.data.datatable
dim dr as system.data.datarow
dt.columns.add(new system.data.datacolumn(“学生班级“, gettype(system.string)))
dt.columns.add(new system.data.datacolumn(“学生姓名“, gettype(system.string)))
dt.columns.add(new system.data.datacolumn(“语文“, gettype(system.decimal)))
dt.columns.add(new system.data.datacolumn(“数学“, gettype(system.decimal)))
dt.columns.add(new system.data.datacolumn(“英语“, gettype(system.decimal)))
dt.columns.add(new system.data.datacolumn(“计算机“, gettype(system.decimal)))
dim i as integer = 0
for i = 0 to 50 – 1
dim rd as system.random = new system.random(environment.tickcount * i)
dr = dt.newrow
dr(0) = “班级“ + i.tostring
dr(1) = “【孟子e章】“ + i.tostring
dr(2) = system.math.round(rd.nextdouble * 100, 2)
dr(3) = system.math.round(rd.nextdouble * 100, 2)
dr(4) = system.math.round(rd.nextdouble * 100, 2)
dr(5) = system.math.round(rd.nextdouble * 100, 2)
dt.rows.add(dr)
next
dim dv as system.data.dataview = new system.data.dataview(dt)
return dv
end function
gridview1.databind()
end if
end sub
dim tw as system.web.ui.htmltextwriter = new system.web.ui.htmltextwriter(html)
mybase.render(tw)
dim sw as system.io.streamwriter
sw = new system.io.streamwriter(server.mappath(“a.htm“), false, system.text.encoding.default)
sw.write(html.tostring)
sw.close()
tw.close()
response.write(html.tostring)
end sub
</script>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>得到asp.net生成的html</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<asp:gridview id=”gridview1″ runat=”server” font-size=”12px” backcolor=”#ffffff”
gridlines=”both” cellpadding=”4″ width=”560″>
<headerstyle backcolor=”#ededed” height=”26px” />
</asp:gridview>
</form>
</body>
</html
<!doctype html public “-//w3c//dtd xhtml 1.0 transitional//en”
“http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd”> <script runat=”server”>
function createdatasource() as icollection
dim dt as system.data.datatable = new system.data.datatable
dim dr as system.data.datarow
dt.columns.add(new system.data.datacolumn(“学生班级“, gettype(system.string)))
dt.columns.add(new system.data.datacolumn(“学生姓名“, gettype(system.string)))
dt.columns.add(new system.data.datacolumn(“语文“, gettype(system.decimal)))
dt.columns.add(new system.data.datacolumn(“数学“, gettype(system.decimal)))
dt.columns.add(new system.data.datacolumn(“英语“, gettype(system.decimal)))
dt.columns.add(new system.data.datacolumn(“计算机“, gettype(system.decimal)))
dim i as integer = 0
for i = 0 to 50 – 1
dim rd as system.random = new system.random(environment.tickcount * i)
dr = dt.newrow
dr(0) = “班级“ + i.tostring
dr(1) = “【孟子e章】“ + i.tostring
dr(2) = system.math.round(rd.nextdouble * 100, 2)
dr(3) = system.math.round(rd.nextdouble * 100, 2)
dr(4) = system.math.round(rd.nextdouble * 100, 2)
dr(5) = system.math.round(rd.nextdouble * 100, 2)
dt.rows.add(dr)
next
dim dv as system.data.dataview = new system.data.dataview(dt)
return dv
end function
protected sub page_load(byval sender as object, byval e as eventargs)
if not ispostback then
gridview1.datasource
gridview1.databind()
end if
end sub
protected overloads overrides sub render(byval writer as htmltextwriter)
dim html as system.io.stringwriter
dim tw as system.web.ui.htmltextwriter = new system.web.ui.htmltextwriter(html)
mybase.render(tw)
dim sw as system.io.streamwriter
sw = new system.io.streamwriter(server.mappath(“a.htm“), false, system.text.encoding.default)
sw.write(html.tostring)
sw.close()
tw.close()
response.write(html.tostring)
end sub
</script>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>得到asp.net生成的html</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<asp:gridview id=”gridview1″ runat=”server” font-size=”12px” backcolor=”#ffffff”
gridlines=”both” cellpadding=”4″ width=”560″>
<headerstyle backcolor=”#ededed” height=”26px” />
</asp:gridview>
</form>
</body>
</html