ASP.NET依赖SFI(Single Form Interface:单接口)而不是MFI(Multi Form Interface:多重接口),因为SFI在ASP.NET通常状态下是一个元素.因此,如果你有两个或两个以上的Form表单的话,在你提交的时候会出现”一个页面只能有一个表单”的错误,在ASP.NET中表单是由HtmlForm派生的,这个类不提供常见的行为属性,也不支持事物属性.
Using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace MultipleForm { /**////<summary> /// Summary description for Main. ///</summary> publicclass Main : System.Web.UI.Page { protected System.Web.UI.WebControls.Button btnSubmit; protected System.Web.UI.HtmlControls.HtmlInputText txtFName; protected System.Web.UI.HtmlControls.HtmlInputText txtLName; protected System.Web.UI.WebControls.TextBox txtfname; privatevoid Page_Load(object sender, System.EventArgs e) { // 获取数据并初始化 Response.Write("<font>First Name : "+ txtFName.Value +"<br>"); Response.Write("Last Name : "+ txtLName.Value +"</font><br>"); } Web Form Designer generated code#region Web Form Designer generated code overrideprotectedvoid OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } /**////<summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///</summary> privatevoid InitializeComponent() { this.Load +=new System.EventHandler(this.Page_Load); } #endregion } }
[A1.aspx.cs]
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Collections.Specialized; /**//* The System.Collections.Specialized namespace has * NameValueCollection class which will be used to * retrieve(检索) form fields data by defined name * * 在命名空间System.Collections.Specialized下的 * NameValueCollection类具有检索form表单字段功能 */ namespace MultipleForm { /**////<summary> /// Summary description for A1. ///</summary> publicclass A1 : System.Web.UI.Page { privatevoid Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here Response.Write("<font>Data posted to A1.apsx page <br>"); // 定义一个 NameValueCollection 类的对象 NameValueCollection userdata; //将form请求赋给userdata对象 //此时userdata就具有所有Form对象的控件值了 userdata = Request.Form; //现在我们就可以预览Form对象中的所有值了 Response.Write("First Name : "+ userdata["fname"].ToString() +"<br>"); Response.Write("Last Name : "+ userdata["lname"].ToString() +"</font><br>"); } Web Form Designer generated code#region Web Form Designer generated code overrideprotectedvoid OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /**////<summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///</summary> privatevoid InitializeComponent() { this.Load +=new System.EventHandler(this.Page_Load); } #endregion } }