Asp.net页面传值的常用方法总结,页面传值的几种基本方法,当然是在网上前辈们的基础上编写的,在经过自己调试。以下是具体的方法,给着我一步步来,掌握基本知识很重要哟!先建立两个页面:a.aspx,b.aspx,a页面放两个TextBox,一个Button,b页面,放一个label,一个TextBox。
第一种:使用QueryString,当然我们有时候也叫url传值,传递的参数通过url实现,安全性不是很高。
a.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("b.aspx?name=" + this.TextBox1.Text + "&id=" + this.TextBox2.Text);
}
b.aspx.cs
private void TheFirst()
{
this.TextBox1.Text = Request.QueryString["name"];
this.Label1.Text = Request.QueryString["id"];
}
第二种:使用Application对象,作用范围对每个用户都有效
a.aspx.cs
protected void Button2_Click(object sender, EventArgs e)
{
Application["name"] = this.TextBox1.Text;
Application["id"] = this.TextBox2.Text;
Server.Transfer("b.aspx");
}
b.aspx.cs
private void TheSecond()
{
Application.Lock();
this.TextBox1.Text = Application["name"].ToString();
this.Label1.Text = Application["id"].ToString();
Application.UnLock();
}
第三种:使用Asp.net的Cookie对象变量,这个使用的也比较多,针对客户端的,而我们用的比较多的Session是针对服务器端。
a.aspx.cs
protected void Button3_Click(object sender, EventArgs e)
{
HttpCookie cookie_name = new HttpCookie("test");
cookie_name.Values["name"] = this.TextBox1.Text;
cookie_name.Values["id"] = this.TextBox2.Text;
Response.AppendCookie(cookie_name);
Server.Transfer("b.aspx");
}
b.aspx.cs
private void TheThird()
{
HttpCookie cookie_name = Request.Cookies["test"];
this.TextBox1.Text = cookie_name.Values["name"];
this.Label1.Text = cookie_name.Values["id"].ToString();
}
本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/net/aspnet/20100810/8913.html
本文介绍了ASP.NET中三种常见的页面传值方法:使用QueryString、Application对象和Cookie对象。每种方法都给出了具体的代码示例,并说明了它们的特点。
895

被折叠的 条评论
为什么被折叠?



