ASP.NET页面之间传递参数方法汇总

在实际项目开发过程中经常需要在两个页面之间传递一些参数,本文在此总结出以下几个传递参数的方法

在撰写之前假设第一个页面为send.aspx,第二个页面为receive.aspx

1、通过URL链接地址传递

(1) send.asp代码

[c-sharp]  view plain copy
  1. protected void Button1_Click(object sender, EventArgs e)   
  2.     {   
  3.         Request.Redirect("Default2.aspx?username=honge");   
  4.     }   

(2) receive.aspx代码

[c-sharp]  view plain copy
  1. string username = Request.QueryString["username"];//这样可以得到参数值。   

2、POST方式传递

(1) send.asp代码

[xhtml]  view plain copy
  1. <form id="form1" runat="server" action="receive.aspx" method=post>   
  2.     <div>   
  3.         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />   
  4. <asp:TextBox ID="username" runat="server"></asp:TextBox>   
  5. </div>   
  6.     </form>   

(2) receive.aspx代码

[c-sharp]  view plain copy
  1. string username = Ruquest.Form["receive"];   
  

3、Session方式传递

(1) send.asp代码

[c-sharp]  view plain copy
  1. protected void Button1_Click(object sender, EventArgs e)   
  2.     {   
  3.         Session["username"] = "honge";   
  4.         Request.Redirect("Default2.aspx");   
  5.     }   

(2) receive.aspx代码

[c-sharp]  view plain copy
  1. string username = Session["username"];//这样可以得到参数值。  

4、Application方式传递

(1) send.asp代码

[c-sharp]  view plain copy
  1. protected void Button1_Click(object sender, EventArgs e)   
  2.     {   
  3.         Application["username"] = "honge";   
  4.         Request.Redirect("Default2.aspx");   
  5.     }   

(2) receive.aspx代码

[c-sharp]  view plain copy
  1. string username = Application["username"];这样可以得到参数值。  

5、使用Server.Transfer进行传递

(1) send.asp代码

[c-sharp]  view plain copy
  1. public string Name   
  2.     {   
  3.         get {   
  4.             return "honge";   
  5.         }   
  6.     }   
  7.     protected void Button1_Click(object sender, EventArgs e)   
  8.     {   
  9.         Server.Transfer("Default2.aspx");   
  10.     }   

(2) receive.aspx代码

[vb]  view plain copy
  1. send d = Context.Handler as send ;   
  2.         if (d != null)   
  3.         {   
  4.             Response.Write(d.Name);//这样可以得到参数值。   
  5.         }   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值