ASP.NET页面传值
在实际项目中经常会遇到一些页面间传递参数的问题,我经常使用的是Cookie的方法,加密传递,但实际中根据不同的情况可以有不同方法,总结几个备用。
1.QueryString变量传递
QueryString是一种简单而且使用频率比较多的一种传值方式,它将传递的值显示在浏览器的地址栏中,如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法。
使用方法:
发送页面—send.aspx
接收页面—receive.aspx
Response.Redirect( "receive.aspx?param1=hello¶m2=hi ")
string str = Request.QueryString["param1"];
string str1 = Request.QueryString["param2];
2.Cookie对象变量(Cookie存放在客户端)
在同一域名下将值保存在Cookie中传递,建议将Cookie信息加密传递,以达到一定的安全性要求。
使用方法:
设置Cookie:
HttpCookie cookie_name = new HttpCookie("name");
cookie_name.Value = VALUE(建议使用加密值);
Reponse.AppendCookie(cookie_name);
获取Cookie:
string name= Request.Cookie["name"].Value.ToString();
3.Session变量(session存放在服务器端)
session值存放在服务器端,可能会存在一些服务器性能和安全问题,实际项目中不是太多使用这种方法。
使用方法:
设置Session: Session["name"] =”hello”;
获取Session:
string name = Session["name"].ToString();
4.Application 对象变量
Application对象的作用范围是整个全局,也就是说对所有用户都有效。此种方法不常使用,因为Application在一个应用程序域范围共享,所有用户可以改变及设置其值,故只应用计数器等需要全局变量的地方。
使用方法:
设置Application : Application["name"] = =”hello”;
获取Application :
string name = Application["name"].ToString();
5.PostBackUrl()方法
该方法应用于两个有直接联系的两个页面间传递,如上一步,下一步页面间传递参数值。
使用方法:
current.aspx
<asp:Button ID=“Button1“ Runat=“server“ Text=“PostToAnotherPage“ PostBackUrl=“~/next.aspx“ />
next.aspx
if (PreviousPage != null)
{
TextBox textBox1 = (TextBox)PreviousPage.FindControl(“TextBox1“);
Response.write(textBox1.Text );
}
6.Server.Transfer方法
该方法是面象对象开发所使用的方法,其使用Server.Transfer方法把流程从当前页面引导到另一个页面中,新的页面使用前一个页面的应答流,所以这个方法是完全面象对象的,简洁有效。
下面这段代码演示在需要很多个参数的时候使用,如果参数比较少就没必要使用这个方法了.
如果让所有的查询页面都继承一个接口,在该接口中定义一个方法,该方法的唯一作用就是让结果页面获得构建结果时所需的参数,就可实现多页面共享一个结果页面操作。
使用方法:
(1)定义一个类,用该类放置所有查询参数
public class QueryParams{
private string firstName;
private string lastname;
private int age;
public string Firstname{
get { return this.firstname; }
set { this.firstname = value; }
}
public string LastName
{
get { return this.lastname; }
set { this.lastname = value; }
}
public string Age
{
get { return this.age; }
set { this.age = value; }
}
}
(2)接口定义
public interface IQueryParams
{
QueryParams Parameters { get;}
}
(3)查询页面继承IQueryParams接口(QueryPage.aspx)
QueryPage.aspx
<form id=“form1“ runat=“server“>
<div>
<asp:TextBox ID=“txtFirstName“ runat=“server“></asp:TextBox>
<asp:TextBox ID=“txtLastName“ runat=“server“></asp:TextBox>
<asp:TextBox ID=“txtAge“ runat=“server“></asp:TextBox>
<asp:Button ID=“btnEnter“ runat=“server“ Text=“Button“ OnClick=“btnEnter_Click“ />
</div>
</form>
QueryPage.aspx.cs
public partial class QueryPage : System.Web.UI.Page, IQueryParams
{
private QueryParams queryParams;
public QueryParams Parameters
{
get
{ return queryParams; }
}
public void btnEnter_Click(object sender, System.EventArgs e)
{
//赋值
queryParams = new QueryParams();
queryParams.FirstnName = this.txtFirstName.Text;
queryParams.Lastname = this.txtLastName.Text;
queryParams.Age = this.txtAge.Text;
Server.Transfer( “ResultPage.aspx “);
}
protected void Page_Load(object sender, EventArgs e)
{ }
}
(4)接收页面(ResultPage.aspx)
public partial class ResultPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
QueryParams queryParams = new QueryParams();
IQueryParams queryInterface;
//实现该接口的页面
if (Context.Handler is IQueryParams)
{
queryInterface = (IQueryParams)Context.Handler;
queryParams = queryInterface.Parameters;
}
Response.Write(“FirstName: “);
Response.Write(queryParams.FirstName);
Response.Write(“ <br/ >Lastname: “);
Response.Write(queryParams.LastName);
Response.Write(“ <br/ >Age: “);
Response.Write(queryParams.Age);
}
}
参考资料:http://www.cnblogs.com/applelure/archive/2008/05/04/1182202.html
还没有评论