2月到新的公司上班,开始的任务是熟悉公司一直在做的项目。并且修改原项目中存在的一些不方便的操作,就是改善用户体验。
其中有个任务就是解决页面返回的问题,开始他们使用的全是goback();的方法。按照项目的设计,每一个功能都是跳转到一个新的页面(不是在新窗口中打开)。
这样最后使用起来就会有一个很不好的体验。比如,我进入到一个页面,选择了N多查询条件以后,会有个结果列表,点具体信息的时候,会跳转到一个详细信息页面(非新窗口),我在这个详细页面进行信息修改。这个时候操作完毕。需要返回到结果列表页面。就需要点2次按钮。这样操作者就会觉得很麻烦。所以我建议不使用我们项目现在用的这个方法,任何新功能都跳转到一个新的页面。
推荐几种方法
1:在同一个页面,工具功能,显示和隐藏不同的PANL。不涉及到页面之间传值
2:在新窗口中打开
3:使用弹出层
4:视图状态
5:post数据到详细页面,然后在详细页点返回的时候再post回来,不过这样就不太符合asp.net了
但是我的项目不可能大改,只能在原来的基础上改了。改的办法有很多种
1:SESSION,不推荐,会影响程序性能
2:URL参数:不安全,因为参数会暴露在外面
3:通过HttpContext.Current.Handler来获取
附上一小段代码,让大家理解
Code
public partial class Serach : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e )
{
if ( !IsPostBack )
{
Result result;
ifHttpContext.Current.Handleris Result )
{
result =HttpContext.Current.Handleras Result;
Response.Write( result.text );
}
}
}
public string datatime
{
get
{ return this.TextBox1.Text; }
set
{ this.TextBox1.Text = value; }
}
protected void Button1_Click( object sender, EventArgs e )
{
Server.Transfer( "Result.aspx" );
}
}
//查询页面
public partial class Result : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if ( !IsPostBack )
{
Serach serach;
if ( HttpContext.Current.Handler is Serach )
{
serach = HttpContext.Current.Handler as Serach;
this.HiddenField1.Value=serach.datatime ;
}
}
}
public string text
{
get
{ return this.HiddenField1.Value; }
set
{ this.HiddenField1.Value = value; }
}
protected void btnText_Click( object sender, EventArgs e )
{
Server.Transfer( "Serach.aspx" );
}
}
//结果页面
这样你可以保存你最初的查询SQL语句,返回的时候再重新加载SQL语句
4:Server.transfer配合previouspage属性
page类有个previouspage属性,这样你可以获取上个页面的信息了
if ( Page.PreviousPage !=
null
)
{
ContentPlaceHolder content = ( ContentPlaceHolder ) Page.PreviousPage.Master.FindControl(
"
UIContentAreaPlaceHolder
"
);
// Response.Write( hf.Value );
if
( content
!=
null
)
{
HiddenField hf = ( HiddenField ) content.FindControl(
"
hfsql
"
);
if ( hf
!=
null
)
{
this .hfSql.Value
=
hf.Value;
// 本人加载SQL语句的方法
LoadDataIntoGridView();
}
}
}
可以将你要保存的信息放在一个HiddenField中来保存。然后使用Server.transfer跳转到另外的页面时候可以通过previouspage属性找到上个页面保存信息的控件来获取
我这次使用的就是这个办法
在此非常感谢园子里的MVP lovecherry