• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

三种asp.net页面跳转的方法

asp 搞代码 4年前 (2022-01-03) 36次浏览 已收录 0个评论

跳转页面是大部编辑语言中都会有的,下面我们来分别介绍一下关于.net中response.redirect sever.execute server.transfer三种页面跳转的方法,,需要的朋

来源gaodai.ma#com搞#代!码网

友可以参考下

本文实例为大家分享了asp.net页面跳转的三种方法,供大家参考,具体内容如下

第一种方法:response.redirect

这个跳转页面的方法跳转的速度不快,因为它要走2个来回(2次postback),但它可以跳转到任何页面,没有站点页面限制(即可以由雅虎跳到新浪),同时不能跳过登录保护。但速度慢是其最大缺陷!redirect跳转机制:首先是发送一个http请求到客户端,通知需要跳转到新页面,然后客户端在发送跳转请求到服务器端。需要注意的是跳转后内部空间保存的所有数据信息将会丢失,所以需要用到session。

代码如下 

 using System; using System.Web.UI; namespace WebApplication1 { public partial class List : Page { protected void Page_Load(object sender, EventArgs e) { // Get response. var response = base.Response; // Redirect temporarily. // ... Don't throw an HttpException to terminate. response.Redirect("//www.gaodaima.com", false); } } } 

代码如下 

 HTTP/1.1 302 Found Content-Type: text/html; charset=utf-8 Location: //www.gaodaima.com Server: Microsoft-IIS/7.0 Date: Fri, 13 Aug 2010 21:18:34 GMT Content-Length: 144   <title>Object moved</title> <h2>Object moved to here.</h2>

第二种方法sever.execute

这个方法主要是用在页面设计上面,而且他必须是跳转同一站点下的页面。这个方法是需要将一个页面的输出结果插入到另一个aspx页面的时候使用,大部分是在表格中,将某一个页面类似于嵌套的方式存在于另一页面。

举个例子看看:

1、创建一个web form
2、在新建的web form中放置一个button1,在放置两个TextBox1,TextBox2
3、为button按钮创建click事件

代码如下

 private void Button1_Click (object sender, System.EventArgs e) { Server.Transfer("webform2.aspx"); }

4、创建过程来返回TextBox1,TextBox2控件的值代码如下:

代码如下

 public string Name { get { return TextBox1.Text; } } public string EMail { get { return TextBox2.Text; } } 

5、新建一个目标页面命名为webform2
6、在webform2中放置两个Label1,Label2

在webform2的Page_Load中添加如下代码:

代码如下

 private void Page_Load (object sender, System.EventArgs e) { //创建原始窗体的实例 WebForm1 wf1; //获得实例化的句柄 wf1=(WebForm1)Context.Handler; Label1.Text=wf1.Name; Label2.Text=wf1.EMail; } 

第三种方法:server.transfer

速度快,只需要一次postback ,但是它必须是在同一个站点下,因为它是server的一个方法。另外,他能跳过登录保护。你可以写个小程序试试:设计一个由页面一到页面二的跳转,但要进入到页面二需要登录,form认证,但如果跳转语句使用transfer的话,那就不会弹出登录页面了。这个方法的重定向请求是发生在服务器端,所以浏览器的url地址仍然保留的是原页面的地址!

代码如下

   <title></title>  <div> <br /></div> .net代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class WebForm1 : System.Web.UI.Page { public string Time { get { return DateTime.Now.ToString(); } } public string TestFun() { return "Function of WebForm1 Called"; } protected void Page_Load(object sender, EventArgs e) { Context.Items.Add("Context", "Context from Form1"); } protected void Button1_Click(object sender, EventArgs e) { //this.TextBox2.Text =Request ["TextBox1"].ToString (); Server.Transfer("WebForm2.aspx", true);//第二个参数为false时,WebForm2.aspx中不能获得TextBox1的内容 } }

如果要捕获一个ASPX页面的输出结果,然后将结果插入另一个ASPX页面的特定位置,则使用Server.Execute。
・如果要确保HTML输出合法,请使用Response.Redirect,因为Server.Execute 或者Server.Transfer方法返回给客户端的页面包含多个标记,不是合法的HTML页面,在非IE浏览器中可能会发生错误。

以上就三种asp.net页面跳转的方法,希望对大家的学习有所帮助。

以上就是三种asp.net页面跳转的方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:三种asp.net页面跳转的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址