ASP.NET实现页面间值传递的几种方法介绍
<p ><P><FONT face="Times New Roman"> 第一种方法:</FONT></P><P><FONT face="Times New Roman" size=3> 通过URL链接地址传递</FONT></P><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><P><FONT face="Times New Roman" size=3> 第二种方法:</FONT></P><P><FONT face="Times New Roman" size=3> 通过post方式。</FONT></P><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><P><FONT face="Times New Roman" size=3> 第三种方法:</FONT></P><P><FONT face="Times New Roman" size=3> 通过session</FONT></P><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><P><FONT face="Times New Roman" size=3> 第四种方法:</FONT></P><P><FONT face="Times New Roman" size=3> 通过Application</FONT></P><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><P><FONT face="Times New Roman" size=3> 第五种方法:</FONT></P><P><FONT face="Times New Roman" size=3> 通过Server.Transfer</FONT></P><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><P><FONT face="Times New Roman" size=3> 如果在asp.net 2.0中还可以这样用:通过PreviousPage</FONT></P><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><P><FONT face="Times New Roman" size=3> 也可以这样用:</FONT></P><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><P><FONT face="Times New Roman" size=3> 如果你的页面中用到了MasterPage的话 Server.Transfer 传递的 PreviousPage就无效了,不知道这是什么原因.所以在用到MasterPage的话,最好用Session或是Context.Items["username"]来实现。</FONT></P><P><p align="center"><font color="FF0000" >1</font>2<span class="content01">下一页>></span></p></p>
页:
[1]