ASP.NET 2.0 中的跨页提交

分类: asp.net技巧   出处:iocblog整理  更新时间:2009-02-24   添加到收藏  

  在asp.net 1.x的时候,很多朋友可能需要进行跨页提交的处理,也就是从页面a能够提交到页面b,甚至不同的control其目标处理页面也各不相同。尤其是从asp/jsp/php转过来的开发人员,可能更有这种需求。但很不幸,在asp.net 1.x的时候,处理这种跨页请求是十分丑陋的,需要非常多的“技巧化”处理。 
   
  在asp.net 2.0的时候,对于跨页提交已经有了非常合理的解决方案,以下就是一个示例。 
   
  sourcepage.aspx: 请注意button1的postbackurl属性设置
  
  <%...@ page language="c#" %>
  
  <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
  
  <script runat="server">...
   public string yourname
   ...{
   get
   ...{
   return this.textbox1.text;
   }
   }
  </script>
  
  <html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
   <title>untitled page</title>
  </head>
  <body> [iocblog.net 来源]
   <form id="form1" runat="server">
   <div>
   <asp:label id="label1" runat="server" text="请输入您的姓名" width="183px"></asp:label>
   <asp:textbox id="textbox1" runat="server"></asp:textbox>
   <asp:button id="button1" runat="server" text="提交" postbackurl="~/targetpage.aspx" /></div>
   </form>
  </body>
  </html> 
   
  targetpage.aspx:请注意previouspagetype的属性设置
  
  <%...@ page language="c#" %>
  <%...@ previouspagetype virtualpath="~/sourcepage.aspx" %>
  
  <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
  
  <script runat="server">...
  
   protected void page_load(object sender, eventargs e)
   ...{
   this.label1.text = previouspage.yourname;
   }
  </script>
  
  <html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
   <title>untitled page</title>
  </head>
  <body>
   <form id="form1" runat="server">
   <div>
   <asp:label id="label1" runat="server" ></asp:label>
  
   </div>
   </form>
  </body>
  </html> 
  ok,就通过这么简单的两个属性设置,就可以非常方便的得到跨页提交的特性。当然,您也可以根据您自己的需求,比如每个control需要提交到不同的页面来进行更加复杂的设置。 
   
  [iocblog.net 来源]



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。