All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET技巧:在GridView中使用Cache_[Asp.Net教程]
asp.net页面导出为Word文档_[Asp.Net教程]
学习动态网页技术PHP中参数引用返回的实例_php资料_编程技术
javascript在asp.ne中的应用_[Asp.Net教程]
PHP将汉字转换拼音_[PHP教程]
.Net中使用GDI+提高gif图片画质的代码_[Asp.Net教程]
关于xml编码问题在VB,PHP,JAVA下的解决方案_[XML教程]
ASP.NET,2.0高级控件之FileUpload控件_[Asp.Net教程]
PHP实例:一个非常全面获取图象信息的PHP函数_php资料_编程技术
smarty技术学习,修改php模板的一点体会_php资料_编程技术
Reading number is top 10 pictures
Ashlynn Brooke photograph of a group3
移民小国也实惠1
美女挤公交
Rendez-vous Sleep with actress, three days to earn 600000
漂亮脸蛋魔鬼身材1
Small s breast enhancement demonstration
西方气质的东方美女3
2012 national geographic daily picture1
中国文革时期的色情图片2
随便发几张图
Download software ranking
Boxer Classic video3
打鸟视频
网页特效实例大全
c#程序设计案例教程
Unix video tutorial5
Adobe Flash Player(IE) 10.0.32.18 浏览器专用的FLASH插件
Unix video tutorial12
C#COM编程指南
Unix video tutorial20
Wild things 2
aaa published in(发表于) 2013/12/17 7:45:54 Edit(编辑)
ASP.NET十分有用的页面间传值方法_.net资料_编程技术

ASP.NET十分有用的页面间传值方法_.net资料_编程技术

ASP.NET十分有用的页面间传值方法_.net资料_编程技术-你的首页-uuhomepage.com

一、目前在ASP.NET中页面传值共有这么几种方式:


1、表单提交,




....
form1.submit();
....
此种方在ASP。NET中无效,因为ASP。NET的表单总是提交到自身页面,如果要提交到别一页面,需要特殊处理。
2、链接地址传送
接收页面: string str = Request["param1"]
3、Session共享
发送页面:Session("param1") = "1111";
按收页面 string str = Session("param1").ToString();
4、Application共享
发送页面: Application("param1") = "1111";
按收页面: string str = Application("param1").ToString();
此种方法不常使用,因为Application在一个应用程序域范围共享,所有用户可以改变及设置其值,故只应用计数器等需要全局变量的地方。
5、Cookie
6、Response.Redirect()方式
Response.Redirect("target.aspx?param1=1111&param2=2222")
接收页面: string str = Request["param1"]
7、Server.Transfer()方式。
Server.Transfer("target.aspx?param1=1111&param2=2222")
接收页面: string str = Request["param1"]


二、如果在两个页面间需要大量的参数要传传递,如数据查询等页面时,用1 - 6的方法传值及其不便,而第 7 种方法确有一独特的优势!但使用该方法时需要一定的设置,现简单介绍一下该方法的使用方式:


  以查询数据页面为例:


在查询页面中设置如下公有属性(QueryPage.aspx):


public class QueryPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtStaDate;
protected System.Web.UI.WebControls.TextBox txtEndDate;
...
///


/// 开始时间
///

public string StaDate
{
get{ return this.txtStaDate.Text;}
set{this.txtStaDate.Text = value;}
}
///
/// 结束时间
///

public string EndDate
{
get{ return this.txtEndDate.Text;}
set{this.txtEndDate.Text = value;}
}
....
private void btnEnter_Click(object sender, System.EventArgs e)
{
Server.Transfer("ResultPage.aspx");
}
}


在显示查询结果页面(ResultPage.aspx):


public class ResultPage : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
//转换一下即可获得前一页面中输入的数据
QueryPage queryPage = ( QueryPage )Context.Handler;
Response.Write( "StaDate:" );
Response.Write( queryPage.StaDate );
Response.Write( "
EndDate:" );
Response.Write( queryPage.EndDate );
}
}


三、如果有许多查询页面共用一个结果页面的设置方法:


在这种方式中关键在于“ QueryPage queryPage = ( QueryPage )Context.Handler; ”的转换,只有转换不依赖于特定的页面时即可实现。


如果让所有的查询页面都继承一个接口,在该接口中定义一个方法,该方法的唯一作用就是让结果页面获得构建结果时所需的参数,就可实现多页面共享一个结果页面操作!


1、先定义一个类,用该类放置所有查询参数:


///


/// 结果页面中要用到的值
///

public class QueryParams
{
private string staDate;
private string endDate;
///
/// 开始时间
///

public string StaDate
{
get{ return this.staDate;}
set{this.staDate = value;}
}
///
/// 结束时间
///

public string EndDate
{
get{ return this.endDate;}
set{this.endDate = value;}
}
}


2、接口定义:


///


/// 定义查询接口。
///

public interface IQueryParams
{
///
/// 参数
///

QueryParams Parameters{get;}
}


3、查询页面继承IQueryParams接口(QueryPage.aspx):


///


///查询页面,继承接口
///

public class QueryPage : System.Web.UI.Page, IQueryParams
{
protected System.Web.UI.WebControls.TextBox txtStaDate;
protected System.Web.UI.WebControls.TextBox txtEndDate;
private QueryParams queryParams;
...
///
/// 结果页面用到的参数
///

public QueryParams Parameters
{
get
{
return queryParams;
}
}
....
private void btnEnter_Click(object sender, System.EventArgs e)
{
//赋值
queryParams = new QueryParams();
queryParams.StaDate = this.txtStaDate.Text;
queryParams.EndDate = this.txtEndDate.Text
Server.Transfer("ResultPage.aspx");
}
}


4、别外的页面也如此设置


5、接收页面(ResultPage.aspx):


public class ResultPage : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
QueryParams queryParams = new QueryParams();
IQueryParams queryInterface;
//实现该接口的页面
if( Context.Handler is IQueryParams)
{
queryInterface = ( IQueryParams )Context.Handler;
queryParams = queryInterface.Parameters;
}
Response.Write( "StaDate:" );
Response.Write( queryParams.StaDate );
Response.Write( "
EndDate:" );
Response.Write( queryParams.EndDate );
}
}





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.