一、目前在ASP.NET中页面传值共有这么几种方式:
1、表单提交
<formaction="target.ASPx"method="post"name="form1">
<inputname="param1"value="1111"/>
<inputname="param2"value="2222"/>
</form>
....
form1.submit();
....
此种方在ASP。NET中无效,因为ASP。NET的表单总是提交到自身页面,如果要提交到别一页面,需要特殊处理。
2、链接地址传送
接收页面:stringstr=Request["param1"]
3、Session共享
发送页面:Session("param1")="1111";
按收页面 stringstr=Session("param1").ToString();
4、Application共享
发送页面:Application("param1")="1111";
按收页面:stringstr=Application("param1").ToString();
此种方法不常使用,因为Application在一个应用程序域范围共享,所有用户可以改变及设置其值,故只应用计数器等需要全局变量的地方。
5、Cookie
6、Response.Redirect()方式
Response.Redirect("target.ASPx?param1=1111¶m2=2222")
接收页面:stringstr=Request["param1"]
7、Server.Transfer()方式。
Server.Transfer("target.ASPx?param1=1111¶m2=2222")
接收页面:stringstr=Request["param1"]
二、如果在两个页面间需要大量的参数要传传递,如数据查询等页面时,用1-6的方法传值及其不便,而第7种方法确有一独特的优势!但使用该方法时需要一定的设置,现简单介绍一下该方法的使用方式:
以查询数据页面为例:
在查询页面中设置如下公有属性(QueryPage.ASPx):
Www.
publicclassQueryPage:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.TextBoxtxtStaDate;
protectedSystem.Web.UI.WebControls.TextBoxtxtEndDate;
...
///<summary>
///开始时间
///</summary>
publicstringStaDate
{
get{returnthis.txtStaDate.Text;}
set{this.txtStaDate.Text=value;}
}
///<summary>
///结束时间
///</summary>
publicstringEndDate
{
get{returnthis.txtEndDate.Text;}
set{this.txtEndDate.Text=value;}
}
....
privatevoidbtnEnter_Click(objectsender,System.EventArgse)
{
Server.Transfer("ResultPage.ASPx");
}
}
_com
在显示查询结果页面(ResultPage.ASPx):
publicclassResultPage:System.Web.UI.Page
{
privatevoidPage_Load(objectsender,System.EventArgse)
{
//转换一下即可获得前一页面中输入的数据
QueryPagequeryPage=(QueryPage)Context.Handler;
Response.Write("StaDate:");
Response.Write(queryPage.StaDate);
Response.Write("<br/>EndDate:");
Response.Write(queryPage.EndDate);
}
}
^com
共3页:上一页1
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>