Posts Tagged ‘传值’

ASP.NET页面间的传值的几种方法

星期二, 06月 3rd, 2008

ASP.NET WEB FORMS 给开发者提供了极好de事件驱动开发模式.然而这种简单de应用程序开发模式却给我带来了一些小问题,举个例子,在传统deASP应用程序中,您能够通过POST方法很容易de把一个值或多个值从一个页面传送到另一个页面,用同样de方法在ASP.NET中实现有点麻烦.在这里,我可以通过其他方式来解决这种情形.ASP.NET为我提供了三种方式,一种是可以通过用QueryString来传送相应de值,再一种是通过session变量来传送相应de值,还有就是通过Server.Transfer方法来实现.下面分别一一介绍:
一、使用Querystring
Querystring是一种非常简单de传值方式,其缺点就是会把要传送de值显示在浏览器de地址栏中,并且在此方法中不能够传递对象.如果您想传递一个安全性不是那么太重要或者是一个简单de数值时,用此方法最好不过了.下面通过一个小例子来完成传值工作,步骤如下:
1、创建一个web form
2、在新建deweb form中放置一个button1,在放置两个TextBox1,TextBox2
3、为button按钮创建click事件
相关代码如下:
private void Button1_Click
(object sender, System.EventArgs e)
{
string url;
url=”webform2.aspx?name=”
TextBox1.Text “&email=”
TextBox2.Text;
Response.Redirect(url);
}
4、新建一个目标页面命名为webform2
5、在webform2中放置两个Label1,Label2
在webform2dePage_Load中添加如下相关代码:
private void Page_Load
(object sender, System.EventArgs e)
{
Label1.Text=Request.QueryString["name"];
Label2.Text=Request.QueryString["email"];
}
运行,即可看到传递后de结果了.
二、使用Session变量
使用Session变量传值是一种最常见de方式了,此中方式不仅可以把值传递到下一个页面,还可以交叉传递到多个页面,直至把Session变量de值removed后,变量才会消失.举个例子看看:
1、创建一个web form
2、在新建deweb form中放置一个button1,在放置两个TextBox1,TextBox2
3、为button按钮创建click事件
相关代码如下:
private void Button1_Click
(object sender, System.EventArgs e)
{
Session["name"]=TextBox1.Text;
Session["email"]=TextBox2.Text;
Response.Redirect(”webform2.aspx”);
}
4、新建一个目标页面命名为webform2
5、在webform2中放置两个Label1,Label2
在webform2dePage_Load中添加如下相关代码:
private void Page_Load
(object sender, System.EventArgs e)
{
Label1.Text=Session["name"].ToString();
Label2.Text=Session["email"].ToString();
Session.Remove(”name”);
Session.Remove(”email”);
}
运行,即可看到传递后de结果了.
三、使用Server.Transfer
虽然这种方法有点复杂,但也不失为一种在页面传值de方式.
举个例子看看:
1、创建一个web form
2、在新建deweb form中放置一个button1,在放置两个TextBox1,TextBox2
3、为button按钮创建click事件
相关代码如下:
private void Button1_Click
(object sender, System.EventArgs e)
{
Server.Transfer(”webform2.aspx”);
}
4、创建过程来返回TextBox1,TextBox2控件de值相关代码如下:
public string Name
{
get
{
return TextBox1.Text;
}
}
public string EMail
{
get
{
return TextBox2.Text;
}
}
5、新建一个目标页面命名为webform2
6、在webform2中放置两个Label1,Label2
在webform2dePage_Load中添加如下相关代码:
private void Page_Load
(object sender, System.EventArgs e)
{
//创建原始窗体de实例
WebForm1 wf1;
//获得实例化de句柄
wf1=(WebForm1)Context.Handler;
Label1.Text=wf1.Name;
Label2.Text=wf1.EMail;
}
运行,即可看到传递后de结果了.

2个页面间不通过Session与url的传值方式

星期二, 06月 3rd, 2008

下面是全部相关代码,已经编译通过.
Chuandi(传递)是名字空间

WebForm1:
<%@ Page language=”c#” Codebehind=”WebForm1.aspx.cs” Inherits=”chuandi.WebForm1″ %>
<HTML>
<HEAD>
<title>WebForm1</title>
</HEAD>
<body>
<form id=”Form1″ method=”post” runat=”server”>
<asp:TextBox id=”TextBox1″ runat=”server”></asp:TextBox>
<asp:Button id=”Button1″ runat=”server” Text=”传”></asp:Button>
</form>
</body>
</HTML>
using System;
namespace chuandi
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
public string Text1
{
get
{
return this.TextBox1.Text;
}
}
private void Page_Load(object sender, System.EventArgs e)
{}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click = new System.EventHandler(this.Button1_Click);
this.Load = new System.EventHandler(this.Page_Load);
}
private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer(”WebForm2.aspx”);
}
}
}


WebForm2:
<%@ Page language=”c#” Codebehind=”WebForm2.aspx.cs” Inherits=”chuandi.WebForm2″ %>
<%@ Reference Page=”WebForm1.aspx” %>
<HTML>
<HEAD>
<title>WebForm2</title>
</HEAD>
<body>
<form id=”Form1″ method=”post” runat=”server”>
<asp:Label id=”Label1″ runat=”server”>Label</asp:Label>
<asp:Button id=”Button1″ runat=”server” Text=”返回”></asp:Button>
</form>
</body>
</HTML>
using System;
namespace chuandi
{
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
public chuandi.WebForm1 wf1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
wf1=(chuandi.WebForm1)Context.Handler;
Label1.Text=”上页传来de是:” wf1.Text1;
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click = new System.EventHandler(this.Button1_Click);
this.Load = new System.EventHandler(this.Page_Load);
}
private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer(”WebForm1.aspx”);
}
}