.net笔试常见题(二) 页面传值常见方法总结

 1.Request 对象传值(Request.QueryString[])

QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中。如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法。但是对于传递数组或对象的话,就不能用这个方法了。

 

实例:创建QueryString.aspx和AcceptValue.aspx页面

 

传值端代码:

 

public partial class QueryString : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string heloo= this.TextBox1.Text;      

        Response.Redirect("AcceptValue.aspx?name="+heloo );
    }
}

 

 

接值端代码:

 

public partial class AcceptValue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        this .TextBox1 .Text =Request .QueryString ["name"];

      
    }
  
}

说明:传值时需定义一个变量(这里暂时定义是name)注意Response.Redirect()中的字符串写法(跳转页面+?+变量+变量值)

 

2.session对象传值

此方法比较常用,过量的存储会导致服务器内存资源的耗尽。

 

具体用法:传值端先定义session变量

 

string stt="你好";

Session["name"]=stt;

 

 

接值端页面:

 this.TextBox1.text=Session["name"].ToString();

 

3.application对象传值

Application对象的作用范围是整个全局,也就是说对所有用户都有效。其常用的方法用Lock和UnLock。

 

传值端代码:

public partial class application : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Application["name"] = this.TextBox1.Text;

        Server.Transfer("AcceptValue.aspx");
    }
}

 

接值端代码:

public partial class AcceptValue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    

        Application.Lock();

        this.TextBox1.Text = Application["name"].ToString();

        Application.UnLock();

      
    }
  
}

 

4.使用cookie对象传值

 

这个也是大家常使用的方法,与Session一样,其是什对每一个用户而言的,但是有个本质的区别,即Cookie是存放在客户端的,而session是存放在服务器端的。而且Cookie的使用要配合ASP.NET内置对象Request来使用。

 

传值端代码:

public partial class cookie : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //定义cookie对象

        HttpCookie hc = new HttpCookie("name");
        //赋值过程
        hc.Value = this.TextBox1.Text;

        //Reponse对象装载
        Response.AppendCookie(hc);

        Server.Transfer("AcceptValue.aspx");


    }
}

接值端代码:

public partial class AcceptValue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    

 

        this.TextBox1.Text = Request.Cookies["name"].Value.ToString();

      
    }
  
}

 

 

5.server对象传值

 

经常使用Server.Tranfer()方法在页面间传值,首先要传递的数据定义为页面的属性,然后再目的页面中通过源页面的实例来访问。

 

传值端代码:

public partial class server : System.Web.UI.Page
{

    //首先创建传递变量的属性
    public string name
    {
        get { return this.TextBox1.Text; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //传递方法定义
        Server.Transfer("AcceptValue.aspx",true );
    }
}

接值端代码:

public partial class AcceptValue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //在接值页面首先利用Context.Hander创建对源页面实例的引用,然后用这个页面源页面的属性,从而获得要传送的数据

        server oform = (server)this.Context.Handler;

        this.TextBox1.Text = oform.name;       
    }
  
}

 

 此外还有ViewState对象传值,它一般用于同一页面内保存和还原状态信息。

 

实例代码:(首先使用ViewState对象保存变量值)

ViewState["Usrname"]="mr";

 

然后,将保存在ViewState的值可以赋值给控件

 

this.label1.Text =ViewState["Username"].ToString();

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值