您的位置:首页 > 编程语言 > ASP

Asp.net页面传值的几种方式

2010-05-24 14:48 435 查看
一、使用QueryString参数

QueryString将传递的值显示在浏览器的地址栏中,是一种非常简单也使用比较多的传值方式。

如果传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法

/// <summary>
/// 使用QueryString变量
/// </summary>
/// <param name="param1"></param>
/// <param name="param2"></param>

//sourcePage
Response.Redirect("target.aspx?param1 = hello¶m2 = hi");

//targetPage

string str1 = Request.QueryString["param1"];
string str2 = Request.QueryString["param2"];


二、使用cookie对象变量

cookie存放在客户端中

//setting cookie
HttpCookie cookie_name =  new HttpCookie("name");
cookie_name.value = txtName.Text.Trim();
Response.AppendCookie (cookie_name);

//get cookie
string name = Request.Cookie["name"].Value.ToString();


三、使用session变量

session存放在服务器中,如果连接超时等情况,有可能会发生数据丢失
//setting session
Session["name"] = "hello";

//get session
string name = Session["name"].ToString();


四、使用Application变量

Application变量的作用范围是整个全局,也就是说对所有的用户都有效,因此此种方法不常用。

因为Application在一个应用程序域范围内共享,所有用户都可以改变或者设置其值,故只应用计数器等需要全局变量的地方

]//setting Application
Application["name"] == "hello";

//get Application
string name = Application["name"].ToString();


五、使用PostBackUrl()

//sourcePage default1.aspx
<asp:Button ID="Button1" Runat="server" Text="PostToAnotherPage" PostBackUrl="~/default2.aspx">
</asp>

//targetPage default2.cs
if (PreviousPage != null)
{
TextBox tb = (TextBox)Previous.FindControl("TextBox1");
string str = tb.Text;
}


六、使用Server.Transfer方法

这个才可以说是面向对象开发所使用的方法
使用Server.Transfer方法把流程从当前页面引导到另一个页面中,新的页面使用前一个页面的应答流,所以这个方法完全面向对象的,简洁有效。
下面这个代码是展示在需要很多个参数的时候使用的方法,如果参数比较少就没有必要使用这个方法了
如果让所有的查询页面都继承一个接口,在该接口中定义一个方法,该方法的唯一作用就是让结果页面获得构建结果时所需的参数,就可实现多页面共享一个结果页面操作

///<summary>
///region query class QueryParams contains all the query params
///</summary>
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;}
}
}

///<summary>
///region query interface IQueryParams
///</summary>
public interface IQueryParams
{
QueryParams Parameters{get;}
}

//sourcePage:IQueryParams
//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" OnClick = "btnEnter_Click"/>
</div>
</form>

//QueryPage.aspx.cs
Public partial class QueryPage: System.Web.UI.Page,IQueryParams
{
private QueryParams queryParams;
public QueryParams Parameters
{
set
{
queryParams = value;
}
get
{
return queryParams;
}
}
public void btnEnter_Click(object sender, System.EventArgs e)
{
//赋值
queryParams = new QueryParams();
queryParams.Firstname = 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)
{}
}

//targetPage
//ResultPage.aspx.cs
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: