Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server" action="Default.aspx">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="txtName" runat="server" ></asp:TextBox>
<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox>
<asp:TextBox ID="txts" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Labelxu" runat="server" Text="Label"></asp:Label><br />
<!--labelxu被skinfile.skin在Html中翻译成了span id=="Labelxu" -->
</div>
</form>
<%=formStr %>
<!--若这里是首次运行为null,则不会触发preinit,若有submit会触发preinit -->
</body>
</html>
Default.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected string formStr;
protected void Page_PreInit(object sender, EventArgs e)
{
if (Request.Form["s"] != null)
{
Page.Theme = Request.Form["s"].ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("<form id='tempForm' name='tempForm' action='Default.aspx' method='post'>");
//sb.Append("<form id='tempForm' name='tempForm' action='Default.aspx' method='post' target='_blank' >");
sb.Append("<input type='hidden' name='userName' value='" + txtName.Text.ToString() + "'>");
sb.Append("<input type='hidden' name='userPwd' value='" + txtPwd.Text.ToString() + "'>");
sb.Append("<input type='hidden' name='s' value='" + txts.Text.ToString() + "'>");
sb.Append("<input type='text' name='texttemp' value='我是来自tempform'>");
sb.Append("</form>");
//sb.Append("</form><script>document.getElementById('tempForm').submit();</script>");
formStr = sb.ToString();
//Response.Write(formStr);
//这里构造的form是临时生成的,因为有submit,所以页面提交后,接受页面又是其本身,而本身接受后,只能按照固定的form来构造html。所以submit后,看不见构造的tempform.
//可以尝试删去submit,看看是否会生成,结果的确是会生成html。
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
}
}