设为首页收藏本站language 语言切换
查看: 1302|回复: 0
收起左侧

ASP.NET实现页面间值传递的几种方法介绍

[复制链接]
发表于 2010-2-25 10:47:37 | 显示全部楼层 |阅读模式
<p ><><FONT face="Times New Roman">  第一种方法:</FONT></P><><FONT face="Times New Roman" size=3>  通过URL链接地址传递</FONT></P><><FONT face="Times New Roman" size=3><CENTER><ccid_nobr><table width="400" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code" ><pre> send.aspx:<BR>  protected void Button1_Click(object sender, EventArgs e)<BR>    {<BR>        Request.Redirect("Default2.aspx?username=honge");<BR>    }<BR> receive.aspx:<BR> string username = Request.QueryString["username"];这样可以得到参数值。</pre></td></tr></table></ccid_nobr></CENTER> </FONT></P><><FONT face="Times New Roman" size=3>  第二种方法:</FONT></P><><FONT face="Times New Roman" size=3>  通过post方式。</FONT></P><><FONT face="Times New Roman" size=3><CENTER><ccid_nobr><table width="400" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code" ><pre>send.aspx<BR><form id="form1" runat="server" action="receive.aspx" method=post><BR>    <div><BR>        <BR><BR></div><BR>    </form><BR>receive.aspx<BR>string username = Ruquest.Form["receive"];</pre></td></tr></table></ccid_nobr></CENTER></FONT></P><><FONT face="Times New Roman" size=3>  第三种方法:</FONT></P><><FONT face="Times New Roman" size=3>  通过session</FONT></P><><FONT face="Times New Roman" size=3><CENTER><ccid_nobr><table width="400" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code" ><pre>send.aspx:<BR>  protected void Button1_Click(object sender, EventArgs e)<BR>    {<BR>        Session["username"] = "honge";<BR>        Request.Redirect("Default2.aspx");<BR>    }<BR> receive.aspx:<BR> string username = Session["username"];这样可以得到参数值。</pre></td></tr></table></ccid_nobr></CENTER></FONT></P><><FONT face="Times New Roman" size=3>  第四种方法:</FONT></P><><FONT face="Times New Roman" size=3>  通过Application</FONT></P><><FONT face="Times New Roman" size=3><CENTER><ccid_nobr><table width="400" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code" ><pre>send.aspx:<BR>  protected void Button1_Click(object sender, EventArgs e)<BR>    {<BR>        Application["username"] = "honge";<BR>        Request.Redirect("Default2.aspx");<BR>    }<BR> receive.aspx:<BR> string username = Application["username"];这样可以得到参数值。</pre></td></tr></table></ccid_nobr></CENTER></FONT></P><><FONT face="Times New Roman" size=3>  第五种方法:</FONT></P><><FONT face="Times New Roman" size=3>  通过Server.Transfer</FONT></P><><FONT face="Times New Roman" size=3><CENTER><ccid_nobr><table width="400" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code" ><pre>send.aspx:<BR>  public string Name<BR>    {<BR>        get {<BR>            return "honge";<BR>        }<BR>    }<BR>    protected void Button1_Click(object sender, EventArgs e)<BR>    {<BR>        Server.Transfer("Default2.aspx");<BR>    }<BR> receive.aspx:<BR>   send d = Context.Handler as send ;<BR>        if (d != null)<BR>        {<BR>            Response.Write(d.Name);这样可以得到参数值。<BR>        }</pre></td></tr></table></ccid_nobr></CENTER></FONT></P><><FONT face="Times New Roman" size=3>  如果在asp.net 2.0中还可以这样用:通过PreviousPage</FONT></P><><FONT face="Times New Roman" size=3>  PreviousPage d = Context.Handler as PreviousPage ;<BR>   if (d != null)<BR>  {<BR>   Response.Write(d.Name);这样可以得到参数值。<BR>  }</FONT></P><><FONT face="Times New Roman" size=3>  也可以这样用:</FONT></P><><FONT face="Times New Roman" size=3><CENTER><ccid_nobr><table width="400" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code" ><pre>send.aspx:<BR><BR>receive.aspx:<BR><%@ PreviousPageType VirtualPath="~/Default.aspx" %><BR>string name = PreviousPage.Name;这样可以得到参数值。</pre></td></tr></table></ccid_nobr></CENTER></FONT></P><><FONT face="Times New Roman" size=3>  如果你的页面中用到了MasterPage的话 Server.Transfer 传递的 PreviousPage就无效了,不知道这是什么原因.所以在用到MasterPage的话,最好用Session或是Context.Items["username"]来实现。</FONT></P><><p align="center"><font color="FF0000" >1</font>2<span class="content01">下一页&gt;&gt;</span></p></p>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-4-4 03:36 , Processed in 0.071063 second(s), 24 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

快速回复 返回顶部 返回列表