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

ASP.NET页面传值的几种方式

2012-02-23 14:28 543 查看
新建两个Web窗体,a.aspx为登陆页面,b.aspx页面把用户在a.aspx页面中输入的用户名、密码显示出来。

a.aspx.cs文件的代码:

protected void Button1_Click(object sender, EventArgs e)
{
//使用querystring传值
//Response.Redirect("b.aspx?username=" + TextBox1.Text.Trim() + "&&pwd=" + TextBox2.Text.Trim());

//使用Session传值
//Session["username"] = TextBox1.Text.Trim();
//Session["pwd"] = TextBox2.Text.Trim();
//Response.Redirect("b.aspx");

//使用Application传值
//Application["username"] = TextBox1.Text.Trim();
//Application["pwd"] = TextBox2.Text.Trim();
//Response.Redirect("b.aspx");

//使用Cookie传值
//HttpCookie hc = new HttpCookie("username",TextBox1.Text.Trim());
//HttpCookie hc2 = new HttpCookie("pwd",TextBox2.Text.Trim());
//Response.AppendCookie(hc);//将创建的Cookie添加到内部Cookie集合中
//Response.AppendCookie(hc2);
//Server.Transfer("b.aspx"); //Cookie需要使用Server.Transfer跳转

//使用Server.Transfer传值
//Server.Transfer方法把流程从当前页面引导到另一个页面中,新的页面使用前一个页面的应答流,所以这个方法是完全面象对象的。
//Server.Transfer("b.aspx");
}
//使用Server.Transfer传值
//public string GetUserName
//{
//    get { return TextBox1.Text.Trim(); }
//}
//public string GetPwd
//{
//    get { return TextBox2.Text.Trim(); }
//}


b.aspx.cs文件的代码:

          //使用querystring取值
//Label1.Text = Request.QueryString["username"].ToString();
//Label2.Text = Request.QueryString["pwd"].ToString();

//使用Session取值
//Label1.Text = Session["username"].ToString();
//Label2.Text = Session["pwd"].ToString();
//Session.Clear();

//使用Application取值
//Label1.Text = Application["username"].ToString();
//Label2.Text = Application["pwd"].ToString();
//Application.Clear();

//使用Cookie取值
//Label1.Text = Response.Cookies["username"].Value.ToString();
//Label2.Text = Response.Cookies["pwd"].Value.ToString();
//Response.Cookies.Clear();

//使用Server.Transfer取值
//a newWeb; //实例a窗口
//newWeb = (a)Context.Handler;
//Label1.Text = newWeb.GetUserName;
//Label2.Text = newWeb.GetPwd;


主要使用QueryString、Session、Application、Cookie、Server.Transfer五种方式传值
需要注意的是,使用Cookie传值跳转时需要用Server.Transfer的方式跳转
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: