、接口定义:
/// <summary> /// 定义查询接口。 /// </summary> public interface IQueryParams { /// <summary> /// 参数 /// </summary> QueryParams Parameters{get;} } |
3、查询页面继承IQueryParams接口(QueryPage.aspx):
/// <summary> ///查询页面,继承接口 /// </summary> 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; /// <summary> /// 结果页面用到的参数 /// </summary> 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( "<br/>EndDate:" ); Response.Write( queryParams.EndDate ); } } |
三、本文起因: 因在工作中要作一个数据查询,参数烦多,原先是用Session传递,用完该Session传来的参数后,还需清理Session,在用Session之前还得判断该Session是否存在,极其烦琐,我想应该还有更简便的方法来实现页面间的参数传递,故上网查找,终于找到这样一种方式来实现页面间的参数传递。