Asp.net页面之间传“大量的参数”

本文介绍了ASP.NET中页面间参数传递的多种方法,包括利用Server.Transfer和Response.Redirect的区别,并提出了一种通过自定义参数类和接口的方式进行跨页面参数传递的方案。

参考:   http://tech.ddvip.com/2009-04/1240487173116514.html

除了常用的Get,Post,Session,Application等页面间可传递参数的方法,还有新的方法,这应该是Asp.net独有的吧
B页面取A页面的值
页面A代码
ContractedBlock.gif ExpandedBlockStart.gif Code
public partial class Default : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
{
    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

    }


    
public string F
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return this.TextBox1.Text.ToString(); }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set this.TextBox1.Text = value; }
    }


    
public string M
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return this.TextBox2.Text.ToString(); }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set this.TextBox2.Text = value; }
    }

    
protected void Button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        Server.Transfer(
"Default2.aspx");
        
// 注意下,地址栏没变
        
//用Response.Redirect不行……
        
//Response.Redirect("Default2.aspx");
        
//这的解释http://topic.youkuaiyun.com/t/20051227/21/4484983.html       
    }

}

页面B的代码
ContractedBlock.gif ExpandedBlockStart.gif Code
public partial class Default2 : System.Web.UI.Page 
ExpandedBlockStart.gifContractedBlock.gif
{
    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{    
        Default s;
        
if (Context.Handler is Default)
ExpandedSubBlockStart.gifContractedSubBlock.gif        

            s 
= (Default)Context.Handler;
            Label1.Text 
= s.F + "---"+ s.M;
        }
       
    }

}

  应该注意到,页面Default.aspx.cs本身就是一个类,这样的话可以在页面二中直接使用这个类,将页面一需要传递的参数
封装一下,供外界访问

一个页面接受多个页面传递的参数,对结果统一处理
1.新建参数类及接
ContractedBlock.gif ExpandedBlockStart.gif Code
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
///QueryParams 的摘要说明
/// </summary>

public class QueryParams
ExpandedBlockStart.gifContractedBlock.gif
{
    
private string staDate;
    
private string endDate;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 开始时间
    
/// </summary>

    public string StaDate
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return this.staDate; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set this.staDate = value; }
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 结束时间
    
/// </summary>

    public string EndDate
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return this.endDate; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set this.endDate = value; }
    }

}

ExpandedBlockStart.gifContractedBlock.gif    
/**//// <summary>
/// 定义查询接口。
/// </summary>

public interface IQueryParams
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary>
/// 参数
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gifQueryParams Parameters{get;}
}

2.多个页面中要继承该接口
ContractedBlock.gif ExpandedBlockStart.gif Code
public partial class Default3 : System.Web.UI.Page,IQueryParams
ExpandedBlockStart.gifContractedBlock.gif
{
    
private QueryParams param;
    
public QueryParams Parameters
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return param;
        }

    }

    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
       
       
    }

    
protected void Button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        param 
= new QueryParams();
        param.StaDate 
= this.TextBox1.Text.ToString();
        param.EndDate 
= this.TextBox2.Text.ToString();
         Server.Transfer(
"Default4.aspx");
    }

}


3.别的页面同样处理
4.统一处理页面

ContractedBlock.gif ExpandedBlockStart.gif Code
public partial class Default4 : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
{
    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        QueryParams queryParams 
= new QueryParams();
        IQueryParams queryInterface;
        
//实现该接口的页面
        
//与Default2.asp相比,主要是Context.Handler的类型不同
        if (Context.Handler is IQueryParams)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            queryInterface 
= (IQueryParams)Context.Handler;
            queryParams 
= queryInterface.Parameters;
        }

        Label1.Text 
= "StaDate:" + queryParams.StaDate + "<br/>EndDate:" + queryParams.EndDate;
       
    }

}


注意下“Response.Redirect(url)和Server.Transfer(url)的用法”是不同的……
还有“Context.Handler”

源代码下载 /Files/hsrzyn/MyWebSite.rar

转载于:https://www.cnblogs.com/hsrzyn/archive/2009/10/06/1578300.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值