Server.Transer()方法的使用及HttpContext类

首先来看HttpContext类:
System.Web.HttpContext类继承自System.Object,按类名来理解,即是Http上下文类.

此类封装了有关单个HTTP 请求的所有HTTP 特定的信息。此类为继承 IHttpModuleIHttpHandler 接口的类提供了对当前 HTTP 请求的 HttpContext 对象的引用。该对象提供对请求的内部 RequestResponseServer 对象的访问。

HttpContext类的常用公共属性有:
Application,为当前 HTTP 请求获取 HttpApplicationState 对象。
Current, 为当前 HTTP 请求获取 HttpContext 对象。
Handler, 为当前 HTTP 请求获取或设置 IHttpHandler 对象。
Items,获取可用于在 HTTP 请求过程中在 IHttpModuleIHttpHandler 之间组织和共享数据的键值集合。
Request,为当前 HTTP 请求获取 HttpRequest 对象。
Response,为当前 HTTP 响应获取 HttpResponse 对象。
Server, 获取提供用于处理 Web 请求的方法的 HttpServerUtility 对象。
Session, 为当前 HTTP 请求获取 HttpSessionState 实例。

通过Page类的Context属性可以获得当前的System.Web.HttpContext对象


接着来看Server.Transer()方法:
通过Page类的Server属性类可以Transfer到另一个页面,如Server.Transfer("NewPage.aspx"),可以跳转到新页面中,
使用Server.Transfer()跳转页面,客户端的URL并不会改变,只是在服务器端执行新页并输出,因此可以在新页面中通过获取来获得请求页面传递的 对象表单数据查询字符串.

假定当前页面为FormerPage.aspx(类名为FormerPage), 跳转到的新页面为NewPage.aspx
从FormerPage.aspx跳转的代码如下:
private   void   btnToNewPage_Click( object  sender, System.EventArgs e)
{
   ArrayList list 
= new ArrayList(3);
   list.Add(
"This list ");
   list.Add(
"is for ");
   list.Add(
"FormerPage to see.");

   Context.Items[
"FormerPageList1"= list;

   Server.Transfer(
"NewPage.aspx");
}


在新页面(NewPage.aspx)Page_Load()事件中通过如下代码获得传递的数据:
         
            
if ( ! IsPostBack)
            
{
                
try
                
{
                    FormerPage former 
= (FormerPage)Context.Handler;                
                    txtFromParentPage.Text 
= former.ClassName; //获取FormerPage中定义的ClassName公共属性
                   
                    //获取在FormerPage中的上下文字典中添加的ArrayList: Context.Items["FormerPageList1"]
                    //获取Contex字典项并强制转换类型:
                    ArrayList list = Context.Items["FormerPageList1"] as ArrayList;
 
                    DataSet ds = former.GetDataSet();  //调用FormerPage中定义的GetDataSet()公共方法
                    DataGrid1.DataSource 
= ds;
                    DataGrid1.DataBind();
                }

                
catch
                
{
                    Response.Write(
"Error get data from parent transfer page!");
                }

                
            }
 
注意上面通过Context.Hander属性来获取当前Http请求的IHttpHander对象,并强制转换成FormerPage对象:
FormerPage former  =  (FormerPage)Context.Handler;
后面可以直接调用这个类的公共属性,方法.同时可以调用在FormerPage中添加的Context字典项(Dictionary Item).

值得注意的是,使用Server.Transer传递页面数据,并使用Context.Handler来接收数据,只有在页面首次加载时,才可以正确获取上一页面的实例,而在postback时,获取的就会是当前页面的实例.

比如在NewPage.aspx中,首次加载时可以获取FomerPage对象,而在回发时尝试获取FormerPage会抛出异常,因为回发时,请求页面已经发生改变,不再是FormerPage发出的请求,而是自身NewPage发出的请求.我们可以在Page_Load()中加入如下代码判断Http请求是由哪个页面发出的:
             string  path  =  Context.Request.Path; 
            Response.Write(
" <script>alert('Request from: "   +  path  +   " ');</script> " );

另外,Server.Transer()有一个重载的方法Server.Trasfer(string newpage,bool preserveForm), 第二个参数用来指定是否保留HttpRequest.Form和HttpRequest.QueryString集合,若为true, 则原页面的Form和QueryString在新的页面中依然有效,可以被调用. 如:
string  str  =   " Value of Textbox: " + Request.Form[ " TextBox1 " + " <br> " ;


About Context:
Context is an object of type System.Web.HttpContext. It is exposed as a property of the ASP.NET Page class. It's also available from user controls and your business objects (more on that later). Here's a partial list of the objects rolled up by HttpContext:
ObjectDescription
ApplicationA key/value pair collection of values that is accessible by every user of the application. Application is of type System.Web.HttpApplicationState.
ApplicationInstanceThe actual running application, which exposes some request processing events. These events are handled in Global.asax, or an HttpHandler or HttpModule.
CacheThe ASP.NET Cache object, which provides programmatic access to the cache. Rob Howard's ASP.NET Caching column provides a good introduction to caching.
ErrorThe first error (if any) encountered while processing the page. See Rob's Exception to the Rule, Part1 for more information.
ItemsA key-value pair collection that you can use to pass information between all of the components that participate in the processing of a single request. Items is of type System.Collections.IDictionary.
RequestInformation about the HTTP request, including browser information, cookies, and values passed in a form or on the query string. Request is of type System.Web.HttpRequest.
ResponseSettings and content for creating the HTTP response. Request is of type System.Web.HttpResponse.
ServerServer is a utility class with several useful helper methods, including Server.Execute(), Server.MapPath(), and Server.HtmlEncode(). Server is an object of type System.Web.HttpServerUtility.
SessionA key/value pair collection of values that are accessible by a single user of the application. Application is of type System.Web.HttpSessionState.
TraceThe ASP.NET Trace object, which provides access to tracing functionality. See Rob's Tracing article for more information.
UserThe security context of the current user, if authenticated. Context.User.Identity is the user's name. User is an object of type System.Security.Principal.IPrincipal.


Reference Articles:
1.MSDN, members of HttpContext, A Matter of Context
2.http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21015699.html
3.http://www.dotnet247.com/247reference/msgs/7/36862.aspx
4.http://www.jaron.cn/chs_scripts/9/2004-09/20040927002606-101590.html  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值