[上]另类页面间传值方法。除Session,Application,Cookie,Get,Post,Server.Transfer()

本文介绍了ASP.NET中页面间传值的多种方法,包括表单提交、链接地址传送、Session共享、Application共享、Cookie、Response.Redirect()方式、Server.Transfer()方式等,并通过示例详细解释了每种方法的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、目前在ASP.NET中页面传值共有这么几种方式:
1、表单提交, <form action= "target.aspx" method = "post" name = "form1"> // method = "post" /"get"
2、链接地址传送
接收页面: string str = Request["param1"]
3、Session共享
发送页面:Session("param1") = "1111";  
按收页面   string str = Session("param1").ToString();  
4、Application共享
发送页面: Application("param1") = "1111";   
按收页面: string str = Application("param1").ToString();  
此种方法不常使用,因为Application在一个应用程序域范围共享,所有用户可以改变及设置其值,故只应用计数器等需要全局变量的地方。
5、Cookie
6、Response.Redirect()方式
    Response.Redirect("target.aspx?param1=1111&m2=2222")
    接收页面: string str = Request["param1"]
7、Server.Transfer()方式。
    Server.Transfer("target.aspx?param1=1111&m2=2222")
    接收页面: string str = Request["param1"]

    Server.Transfer()方式。以查询数据页面为例:
    在查询页面中设置如下公有属性(QueryPage.aspx):

ContractedBlock.gifExpandedBlockStart.gifCode
public class QueryPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtStaDate;
protected System.Web.UI.WebControls.TextBox txtEndDate;
    
/// <summary>
/// 开始时间
/// </summary>
public string StaDate
{
getreturn this.txtStaDate.Text;}
set{this.txtStaDate.Text = value;}
}
/// <summary>
/// 结束时间
/// </summary>
public string EndDate
{
getreturn this.txtEndDate.Text;}
set{this.txtEndDate.Text = value;}
}
.
private void btnEnter_Click(object sender, System.EventArgs e)
{
Server.Transfer(
"ResultPage.aspx");
}

 

在显示查询结果页面(ResultPage.aspx):

 

ContractedBlock.gifExpandedBlockStart.gifCode
public class ResultPage : System.Web.UI.Page
{
    
private void Page_Load(object sender, System.EventArgs e)
    { 
//转换一下即可获得前一页面中输入的数据
QueryPage queryPage = ( QueryPage )Context.Handler;

Response.Write( 
"StaDate:" );
Response.Write( queryPage.StaDate );
Response.Write( 
"<br/>EndDate:" );
Response.Write( queryPage.EndDate );
   }

 

 

转载于:https://www.cnblogs.com/voswin/articles/1271664.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值