ASP.NET中的多重表单[翻译]

作者:Sanjay Kumar 2006-6-5

译者:SPARON2006-6-7

这篇文章将教我们怎么使用ASP.NET中的多重表单

URL:

http://www.c-sharpcorner.com/UploadFile/avi_sanjay/MultiForms.htm06042006062957AM/MultiForms.htm.aspx

ASP.NET依赖SFI(Single Form Interface:单接口)而不是MFI(Multi Form Interface:多重接口),因为SFI在ASP.NET通常状态下是一个元素. 因此,如果你有两个或两个以上的Form表单的话,在你提交的时候会出现”一个页面只能有一个表单”的错误,在ASP.NET中表单是由HtmlForm派生的,这个类不提供常见的行为属性,也不支持事物属性.

当页面提交时,页面对象被处理并呈递第一个服务器端运行的Form,同时将其内置的flag标志写入日志,页面中其它的服务器端运行的Form将会被抛出一个HttpException异常。

不过你可以插入多个没有"runat =server"属性的Form标记.示范代码如下:

1. 如何在你的ASPX文件中插入多个Form并且提交到另一页面.

2. 如何在提交页面检索从Form控件传来的值.

[Main.aspx]: 在这个页面里有两个Form表单,一个是服务器端运行的表单,它传递的数据直接提交到本页面,另外一个表单是普通的HTML表单,它没有'runat=server'属性,它会将表单内的数据传递到另一个页面。嵌入该表单内的HTML控件需指定唯一地名称。

<%@ Page language="c#" Codebehind="Main.aspx.cs" AutoEventWireup="false" Inherits="MultipleForm.Main" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

<HEAD>

<title>Main</title>

<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">

<meta content="C#" name="CODE_LANGUAGE">

<meta content="JavaScript" name="vs_defaultClientScript">

<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

<link type="text/css" href="MyStyle.css" rel="stylesheet">

</HEAD>

<body MS_POSITIONING="GridLayout">

<form method="post" runat="server">

<table cellSpacing="0" cellPadding="0" width="95%" >

<caption class="tblCaption">Post data to same page [ Main.aspx ]</caption>

<tr class="rowStyle">

<td width="25%">First Name</td>

<td width="25%"><input class="inputformat" type="text" runat="server" NAME="txtFName"></td>

<td width="25%">Last Name</td>

<td width="25%"><input class="inputformat" type="text" runat="server" NAME="txtLName"></td>

</tr>

<tr class="rowStyle">

<td></td>

<td colspan="2" align="center"><asp:Button CssClass="buttonStyle" runat="server" Text="Post to Main"></asp:Button></td>

<td></td>

</tr>

</table>

</form>

<br>

<!------

此处是HTML表单,它将提交到另一个页面处理表单中的数据

//---------->

<form action="A1.aspx" method="post">

<table cellSpacing="0" cellPadding="0" width="95%" >

<caption class="tblCaption">Post data to another page [ A1.aspx ]</caption>

<tr class="rowStyle">

<td width="25%">First Name</td>

<td width="25%"><input class="inputformat" type="text" name="fname"></td>

<td width="25%">Last Name</td>

<td width="25%"><input class="inputformat" type="text" name="lname"></td>

</tr>

<tr class="rowStyle">

<td></td>

<td colspan="2" align="center"><input class="buttonStyle" type="submit" value="Post to A1"></td>

<td></td>

</tr>

</table>

</form>

</body>

</HTML>

[Main.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;

namespace MultipleForm

{

/// <summary>

/// Summary description for Main.

/// </summary>

public class 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;

private void Page_Load(object sender, System.EventArgs e)

{

// 获取数据并初始化

Response.Write("<font>First Name : " + txtFName.Value + "<br>");

Response.Write("Last Name : " + txtLName.Value + "</font><br>");

}

#region Web Form Designer generated code

override protected void 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>

private void 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>

public class A1 : System.Web.UI.Page

{

private void 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>");

}

#region Web Form Designer generated code

override protected void 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>

private void InitializeComponent()

{

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

}

}

ASP.NET中实现多表单提交.rar