熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

.NET 2.0得到本頁生成的HTML代碼

2013-11-13 10:17:31  來源: .NET編程 

  在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


From:http://tw.wingwit.com/Article/program/net/201311/13173.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.