ASP.NET不用页面跳转方式的区别及页面间传值方法

来源:互联网 发布:win10 手写笔软件 编辑:程序博客网 时间:2024/05/22 20:26


本文转自:http://www.cnblogs.com/csharp4/archive/2010/05/14/1735745.html

1 response.redirect 这个跳转页面的方法跳转的速度不快,因为它要走2个来回(2次postback),但他可以跳 转到任何页面,没有站点页面限制(即可以由雅虎跳到新浪),同时不能跳过登录保护。但速度慢是其最大缺陷!redirect跳转机制:首先是发送一个http请求到客户端,通知需要跳转到新页面,然后客户端在发送跳转请求到服务器端。需要注意的是跳转后内部空间保存的所有数据信息将会丢失,所以需要用到session。
2 server.transfer 速度快,只需要一次postback ,但是。。。。他必须是在同一个站点下,因为它是server的一个方法。另外,他能跳过登录保护。你可以写个小程序试试:设计一个由页面一到页面二的跳转,但要进入到页面二需要登录,form认证,但如果跳转语句使用transfer的话,那就不会弹出登录页面了。这个方法的重定向请求是发生在服务器端,所以浏览器的url地址仍然保留的是原页面的地址!
3 sever.execute 这个方法主要是用在页面设计上面,而且他必须是跳转同一站点下的页面。这个方法是需要将一个页面的输出结果插入到另一个aspx页面的时候使用,大部分是在表格中,将某一个页面类似于嵌套的方式存在于另一页面。
总结:
当需要把用户跳转到另一台服务器上的页面的时候 使用redirect
当需要把用户跳转到非aspx页面时候,如html 使用redirect
需要把查询字符串作为url一部分的时候保留传给服务器的时候,因为其他2种方法不能做到2次postback,把数据先带回服务器 使用redirect
需要aspx页面间的转换(不涉及登录) 使用transfer
当需要把aspx页面的输出结果插入到另一个aspx页面的时候使用 execute方法。
当然,忘记了还有一个超链接!当然这个就不需要讲太多了。他是在党需要用户来决定什么时候跳转页面,就使用超链接。

 

顺便提一下,如何使用redirect方法在查询字符串中使用汉字,因为经常的情况是出现乱码,原因是url不支持汉字。这个时候需要转换:
string message =server.urlencode("欢迎来到赛跑专栏");
先转换,在使用查询字符串
response.redirect("webform2.aspx?msg="+message);

 

页面间传值方式:

两个页面:Default.aspx,WebForm1.aspx

代码:

Default.aspx.cs:        public string GetId        {            get            {                return "567";            }        }        protected void Page_Load(object sender, EventArgs e)        {        }        protected void btnTransfer_Click(object sender, EventArgs e)        {            Response.Redirect("webForm1.aspx?id=123");        }        protected void Button2_Click(object sender, EventArgs e)        {            //Session创建            Session["id"] = "345";            Response.Redirect("webForm1.aspx",false);        }        protected void Button3_Click(object sender, EventArgs e)        {            //注:一些浏览器是不支持Cookies的            HttpCookie cId = new HttpCookie("id");            cId.Value = "234";            Response.Cookies.Add(cId);            Response.Redirect("webForm1.aspx", false);        }        protected void Button4_Click(object sender, EventArgs e)        {            Application["id"] = "456";            Response.Redirect("webForm1.aspx", false);        }        protected void Button5_Click(object sender, EventArgs e)        {            Server.Transfer("webForm1.aspx", false);        }webForm1.aspx.cs:        protected void Page_Load(object sender, EventArgs e)        {            try            {                if (Request.QueryString["id"] != null)                    Label1.Text = "Response.Redirect方式:" + Request.QueryString["id"];                if (Request.Cookies["id"] != null)                    Label1.Text = "Cookies方式:" + Request.Cookies["id"].Value;                //从Session中取值                if (Session["id"] != null)                    Label1.Text = "Session方式:" + Session["id"].ToString();                if (Application["id"] != null)                    Label1.Text = "Application方式:" + Application["id"].ToString();                _Default d;                //获取该页面的句柄                d = (_Default)Context.Handler;                if (d.GetId != null)                    Label1.Text = "Application方式:" + d.GetId;            }            catch (Exception ex)            {                Console.WriteLine("------" + ex.Message);            }                    }

原创粉丝点击