如何在asp.net把表单提交到另一页

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>
/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值