返回创远网络首页 | 台州创远网络 | 列表 >> 网络编程 >> 浏览主题 版主:
互联资讯 | 站长在线 | SEO搜索 | 网站运营 | 网络编程 | 全部分类
ASP.NET跨页面传值技巧
已阅:2368 / 回复:1(楼主)
1. 使用QueryString变量

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

private void Button1_Click(object sender, System.EventArgs e)
{
    string s_url;
    s_url = "b.aspx?name=" + Label1.Text;
    Response.Redirect(s_url);
}

  b.aspx中C#代码

private void Page_Load(object sender, EventArgs e)
{
    Label2.Text = Request.QueryString["name"];
}

2. 使用Application 对象变量

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

  a.aspx的C#代码

private void Button1_Click(object sender, System.EventArgs e)
{
    Application["name"] = Label1.Text;
    Server.Transfer("b.aspx");
}

  b.aspx中C#代码

private void Page_Load(object sender, EventArgs e)
{
    string name;
    Application.Lock();
    name = Application["name"].ToString();
    Application.UnLock();
}

3. 使用Session变量

  想必这个肯定是大家使用中最常见的用法了,其操作与Application类似,作用于用户个人,所以,过量的存储会导致服务器内存资源的耗尽。

  a.aspx的C#代码

private void Button1_Click(object sender, System.EventArgs e)
{
    Session["name"] = Label.Text;
}

  b.aspx中C#代码

private void Page_Load(object sender, EventArgs e)
{
    string name;
    name = Session["name"].ToString();
}

4. 使用Cookie对象变量

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

  a.aspx的C#代码

private void Button1_Click(object sender, System.EventArgs e)
{
    HttpCookie cookie_name = new HttpCookie("name");
    cookie_name.value = Label1.Text;
    Reponse.AppendCookie(cookie_name);
    Server.Transfer("b.aspx");
}

  b.aspx中C#代码

private void Page_Load(object sender, EventArgs e)
{
    string name;
    name = Request.Cookie["name"].value.ToString();
}

5. 使用Server.Transfer方法

  这个才可以说是面象对象开发所使用的方法,其使用Server.Transfer方法把流程从当前页面引导到另一个页面中,新的页面使用前一个页面的应答流,所以这个方法是完全面象对象的,简洁有效。

  a.aspx的C#代码

public string Name
{
    get{ return Label1.Text;}
}
private void Button1_Click(object sender, System.EventArgs e)
{
    Server.Transfer("b.aspx");
}

  b.aspx中C#代码

private void Page_Load(object sender, EventArgs e)
{
    a newWeb;   //实例a窗体
    newWeb = (source)Context.Handler;
    string name;
    name = newWeb.Name;
}


作者:admin (2009-10-18 18:20:20)
回复:ASP.NET跨页面传值技巧
第 1 楼
stone processing equipment polishing machines bridge saw Profile machine cutting machinery stylaobte cutting machine stone machine automatic bridge cutter stone processing machine shaped piece cutter stone machinery stone machinery  automatic edge cutter cutting machine working center edge cutting machine drilling machine bridge cutting machine portal edge cutter single jib disk stone cutter oil-sealed cutter single pillar stone cutter two-way stone cutter  marble cutting machine square stone cutter infrared cutting machine double blade cutter gantry block cutter marble processing machine marble processing machine wire cutting machine granite processing machine multi-blade cutting machine stone cutter marble cutter sawing machine diamond wheel profiling wheel  saw blade external grinding internal grinding plane grinding planing block cutting machine cutting basin machine grinding and polishing machine Auto Cutting Machine Auto Cutting Machinery Edge cutting machinery Quarrying machine Quarrying machinery stone manufacturing equipment cutting equipment Stone Processing Machinery Equipment Stone Processing Machine Equipment Stone for industrial equipment cutter machine Stone cutting machine Stone Cutting Machinery Granite cutting Granite cutting machinery Granite cutting machine Marble cutting Marble cutting machinery stone equipment supplies automation stone manufacturing equipment polishing tools stone polishing tools Granite polishing tools Marble polishing tools stone polishing equipment stone industrial machines granite polishing equipment granite polishing machines bridge saws bridge saw granite diamond tools diamond tool diamond saws bridge saw machine bridge saw machinery saws stone cutting stone cutting saw bridge saws equipment profile cutting machine profile grinding machine profile wrapping machine Stripe Paving Stone Ceramics machine diamond saw bit experimental machine ceramic tile cutting machine stone engraving machine computer numerial controlling cutter stone worksite cutter
作者:stonemachi (2010-6-17 17:07:45)
当前总数:1 每页5条 当前1/1页 [1 

目前不允许游客回复,请 登录 注册 发贴.