在ASPNET 中有时候需要对ASPNET生成的HTML代码进行处理或者是保存成静态文件ASPNET 提供了直接将请求保存成文件的方法HttpRequestSaveAs方法下面这个方法就是在ASPNET 中得到ASPNET生成的HTML代码同时生成一个静态文件的方法
C#
<%@ Page Language=C# AutoEventWireup=true %>
<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN
transitionaldtd>
<script runat=server>
// 计算数据完全可以从数据看取得
ICollection CreateDataSource( )
{
SystemDataDataTable dt = new SystemDataDataTable();
SystemDataDataRow dr;
dtColumnsAdd(new SystemDataDataColumn(学生班级 typeof(SystemString)));
dtColumnsAdd(new SystemDataDataColumn(学生姓名 typeof(SystemString)));
dtColumnsAdd(new SystemDataDataColumn(语文 typeof(SystemDecimal)));
dtColumnsAdd(new SystemDataDataColumn(数学 typeof(SystemDecimal)));
dtColumnsAdd(new SystemDataDataColumn(英语 typeof(SystemDecimal)));
dtColumnsAdd(new SystemDataDataColumn(计算机 typeof(SystemDecimal)));
for (int i = ; i < ; i++)
{
SystemRandom rd = new SystemRandom(EnvironmentTickCount * i); ;
dr = dtNewRow();
dr[] = 班级 + iToString();
dr[] = 【孟子E章】 + iToString();
dr[] = SystemMathRound(rdNextDouble() * );
dr[] = SystemMathRound(rdNextDouble() * );
dr[] = SystemMathRound(rdNextDouble() * );
dr[] = SystemMathRound(rdNextDouble() * );
dtRowsAdd(dr);
}
SystemDataDataView dv = new SystemDataDataView(dt);
return dv;
}
protected void Page_Load( object sender EventArgs e )
{
if (!IsPostBack)
{
GridViewDataSource = CreateDataSource();
GridViewDataBind();
}
}
protected override void Render( HtmlTextWriter writer )
{
SystemIOStringWriter html = new SystemIOStringWriter();
SystemWebUIHtmlTextWriter tw = new SystemWebUIHtmlTextWriter(html);
baseRender(tw);
SystemIOStreamWriter sw;
sw = new SystemIOStreamWriter(ServerMapPath() false SystemTextEncodingDefault);
swWrite(htmlToString());
swClose();
twClose();
ResponseWrite(htmlToString());
}
</script>
<html xmlns=>
<head>
<title>得到ASPNET生成的HTML</title>
</head>
<body>
<form id=Form runat=server>
<asp:GridView ID=GridView runat=server FontSize=px BackColor=#FFFFFF
GridLines=Both CellPadding= Width=>
<HeaderStyle BackColor=#EDEDED Height=px />
</asp:GridView>
</form>
</body>
</html>
VBNET
<%@ Page Language=VB %>
<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN
transitionaldtd>
<script runat=server>
Function CreateDataSource() As ICollection
Dim dt As SystemDataDataTable = New SystemDataDataTable
Dim dr As SystemDataDataRow
dtColumnsAdd(New SystemDataDataColumn(学生班级 GetType(SystemString)))
dtColumnsAdd(New SystemDataDataColumn(学生姓名 GetType(SystemString)))
dtColumnsAdd(New SystemDataDataColumn(语文 GetType(SystemDecimal)))
dtColumnsAdd(New SystemDataDataColumn(数学 GetType(SystemDecimal)))
dtColumnsAdd(New SystemDataDataColumn(英语 GetType(SystemDecimal)))
dtColumnsAdd(New SystemDataDataColumn(计算机 GetType(SystemDecimal)))
Dim i As Integer =
For i = To
Dim rd As SystemRandom = New SystemRandom(EnvironmentTickCount * i)
dr = dtNewRow
dr() = 班级 + iToString
dr() = 【孟子E章】 + iToString
dr() = SystemMathRound(rdNextDouble * )
dr() = SystemMathRound(rdNextDouble * )
dr() = SystemMathRound(rdNextDouble * )
dr() = SystemMathRound(rdNextDouble * )
dtRowsAdd(dr)
Next
Dim dv As SystemDataDataView = New SystemDataDataView(dt)
Return dv
End Function
Protected Sub Page_Load(ByVal sender As Object ByVal e As EventArgs)
If Not IsPostBack Then
GridViewDataSource = CreateDataSource()
GridViewDataBind()
End If
End Sub
Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
Dim html As SystemIOStringWriter = New SystemIOStringWriter
Dim tw As SystemWebUIHtmlTextWriter = New SystemWebUIHtmlTextWriter(html)
MyBaseRender(tw)
Dim sw As SystemIOStreamWriter
sw = New SystemIOStreamWriter(ServerMapPath() False SystemTextEncodingDefault)
swWrite(htmlToString)
swClose()
twClose()
ResponseWrite(htmlToString)
End sub
</script>
<html xmlns=>
<head>
<title>得到ASPNET生成的HTML</title>
</head>
<body>
<form id=Form runat=server>
<asp:GridView ID=GridView runat=server FontSize=px BackColor=#FFFFFF
GridLines=Both CellPadding= Width=>
<HeaderStyle BackColor=#EDEDED Height=px />
</asp:GridView>
</form>
</body>
</html