Asp.net页面传值的常用方法总结

本文介绍了ASP.NET中三种常见的页面传值方法:使用QueryString、Application对象和Cookie对象。每种方法都给出了具体的代码示例,并说明了它们的特点。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值