asp.net中form 提交到另一页去 解决办法。[转载]

感谢您使用微软产品。

在ASP.NET中,每一个aspx页面在server端都对应一个System.Web.UI.Page实例,所以把一个页面Form中Server Controls的内容(server端对应于page类实例的数据)提交给另一个page类,跟asp中的实现方法有所不同。

在asp.net中,Form提交的工作原理是:

比如说在您的一个aspx文件中,您使用了一个TextBox Server Control. 在您的Page class中, 有这么一个实例:

TextBox TextBox1 = new TextBox();

您可以使用TextBox1在服务器端来引用该对象。当ASP.NET执行完该页面之后,客户端(浏览器)得到的纯HTML/DHTML中,会产生下面的代码,对应于服务器端的逻辑:

<input name="TextBox1" type="text" id="TextBox1" />

注意:上边的“name”属性,和服务器端代码中TextBox1对象的UniqueID Property是一致的。

此时的客户端跟您的程序交互的唯一方式就是HTTP中的POST. POST提交之后,ASP.NET检查“name"是否和其所提交页面对应得Page类中的某一Control的UniqueID一致,如果有,并且该Server Control实现了IPostBackDataHandler借口,则调用LoadPostData函数,您可以重载这个函数。如果实现了IPostBackEventHanlder, ASP.NET调用RaisePostBackEvent().

在ASP.NET中传输Form到另外的页面,Inline Code(代码和html在同一页面)和Code-Behind(代码和html在不同的页面)地实现方式有所不同。下面是Inline Code的一个例子:

在WebForm1.aspx中:
1。为该页面声明类的名称;<%@ Page Language="C#" ClassName="FirstPageClass" %>
2。为每一个要传递到另外页面的元素,定义带Get accessor的Property:
3。使用Server.Transfer("Webform2.aspx")把控制权提交给另外一个WebForm class.

////////////////////////  WebForm1.aspx    ////////////////////////////////////////

<%@ Page Language="C#" ClassName="FirstPageClass" %>

<html>
<head>
   <script runat="server">
      public string FirstName
      {
         get
         {
            return first.Text;
         }
      }

      public string LastName
      {
         get
         {
            return last.Text;
         }
      }

      void ButtonClicked(object sender, EventArgs e)
      {
         Server.Transfer("secondpage.aspx");
      }

   </script>

</head>

<body>

   <form runat="server">
      First Name: <asp:TextBox id="first" runat="server"/>
      <br>
      Last Name: <asp:TextBox id="last" runat="server"/>
      <br>
      <asp:Button OnClick="ButtonClicked" Text="Go to second page" runat=server />
   </form>
</body>
</html>
//////////////////////////////////////////////////////////////////////

在目的Webform2.aspx中:

1。添加Reference指令;<%@ Reference Page="firstpage.aspx" %>
2。声明一个WebForm1.aspx对应的class的实例:FirstPageClass fp;
3。利用HttpContext class, 获得第一个得到 HTTP Request 的页面的实例(Webform1.aspx):   fp = (FirstPageClass)Context.Handler;

////////////////////  WebForm2.aspx     ////////////////////////////////////////////////////////////////////
<%@ Page Language="C#" %>
<%@ Reference Page="firstpage.aspx" %>
<html>
<head>
   <script runat="server">

      FirstPageClass fp;

      void Page_Load()
      {
         if (!IsPostBack)
         {
            fp = (FirstPageClass)Context.Handler;
         }
      }
   </script>
</head>
<body>

   <form runat="server">
     Hello <%=fp.FirstName%> <%=fp.LastName%>
   </form>

</body>
</html>
/////////////////////////////////////////////////////////////////////////////////////////////////////////

关于Code-Behinde方式中的详细信息,请您参阅下面的文章:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconpassingservercontrolvaluesbetweenpages.asp

希望上面的信息对您有所帮助。

-微软全球技术中心

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
原文地址:https://www.cnblogs.com/Neil/p/623192.html