在两个页面之间传递信息

在asp.net不同页面之间传值的方法主要有三种:
(1)用querystring
方法:在vs2005中新建asp.net网站(发送端),在页面上添加button,两个TextBox,代码如下:
None.gif      protected   void  Button1_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
string url;
InBlock.gif        url 
= "http://localhost/look1/Default.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text;
InBlock.gif        Response.Redirect(url);
ExpandedBlockEnd.gif     }
再建接收端页面,在页面上添加两个TextBox,代码如下:
None.gif         TextBox1.Text  =  Request.QueryString[ " name " ];
None.gif        TextBox2.Text 
=  Request.QueryString[ " email " ];

(2)使用session变量
None.gif private   void  Button1_Click
None.gif(
object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifSession[
"name"]=TextBox1.Text;
InBlock.gifSession[
"email"]=TextBox2.Text;
InBlock.gifServer.Transfer(
"http://localhost/look1/Default.aspx");
ExpandedBlockEnd.gif}

None.gif目标页面代码:
None.gif
private   void  Page_Load
None.gif(
object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifTextBox1.Text
=Session["name"].ToString();
InBlock.gifTextBox2.Text
=Session["email"].ToString();
InBlock.gifSession.Remove(
"name");
InBlock.gifSession.Remove(
"email");
ExpandedBlockEnd.gif}

None.gif

(3)使用server.transfer
None.gif 源页面代码:
None.gif把以下的代码添加到页面中
None.gif
public   string  Name
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
get
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return TextBox1.Text;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
public   string  EMail
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
get
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return TextBox2.Text;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif然后调用Server.Transfer方法
None.gif
private   void  Button1_Click
None.gif(
object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifServer.Transfer(
"anotherwebform.aspx");
ExpandedBlockEnd.gif}

None.gif目标页面代码:
None.gif
private   void  Page_Load
None.gif(
object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifWebForm1 wf1;
InBlock.gifwf1
=(WebForm1)Context.Handler;
InBlock.gifTextBox1.Text
=wf1.Name;
InBlock.gifTextBox2.Text
=wf1.EMail;
ExpandedBlockEnd.gif}

None.gif
None.gif
当然,在同一个解决方案里的不同页面之间传值就更简单了,只需将url改为页面名(如:Default2.aspx)就OK了!试试看

转载于:https://www.cnblogs.com/support/archive/2006/10/10/524949.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值