表單提交
傳送頁面代碼
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>表單提交</title>
<script type="text/javascript" language="javascript">
function post()
{
forPost.action="DestinationPage.aspx";
forPost.submit();
}
</script>
</head>
<body>
<form id="forPost" method="post">
方式一:表單提交<br />
<input type="text" id="SourceData2" runat="server"/>
<input type="button" id="btnTransfer1" value="提交" onclick="post();" />
</form>
</body>
</html>
接收頁面代碼
protected void Page_Load(object sender, EventArgs e)
{
string a = Request.Form["SourceData2"].ToString();
txt1.Value = a;
}
QueryString傳值
傳送頁面代碼
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>QueryString</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="txt1" runat="server" />
<input type="text" id="txt2" runat="server" />
<input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
</div>
</form>
</body>
</html>
protected void btn1_ServerClick(object sender, EventArgs e)
{
string aa = txt1.Value;
string bb = txt2.Value;
string url = "DestinationPage5.aspx?parameter1=" + aa + "¶meter2=" + bb;
Response.Redirect(url, false);
}
接收頁面代碼
protected void Page_Load(object sender, EventArgs e)
{
txt1.Value = Request.QueryString["parameter1"].ToString();
txt2.Value = Request.QueryString["parameter2"].ToString();
}
鏈接地址傳值
傳送頁面代碼
<a href="DestinationPage6.aspx?param1=1111¶m2=2222">跳轉</a>
接收頁面代碼
protected void Page_Load(object sender, EventArgs e)
{
txt1.Value = Request["param1"];
txt2.Value = Request["param2"];
}
Context傳值
通過Context傳值,在傳送頁面之前,將需要傳遞到其他頁面的值存在Context中。
傳送頁面代碼
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Context</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="txt1" runat="server" />
<input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
</div>
</form>
</body>
</html>
protected void btn1_ServerClick(object sender, EventArgs e)
{
Context.Items["value"] = txt1.Value;
Server.Transfer("DestinationPage3.aspx");
}
接收頁面代碼
protected void Page_Load(object sender, EventArgs e)
{
string a = Context.Items["value"].ToString();
txt1.Value = a;
}
Server.Transfer傳值
傳送頁面代碼
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Server.Transfer</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="txt1" runat="server" />
<input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
</div>
</form>
</body>
</html>

protected void btn1_ServerClick(object sender, EventArgs e)
{
Server.Transfer("DestinationPage4.aspx");
}
public string Value
{
get { return txt1.Value; }
}
接收頁面代碼

public partial class Transfer_DestinationPage4 : System.Web.UI.Page
{
private Transfer_SourcePage4 sourcePage;
protected void Page_Load(object sender, EventArgs e)
{
sourcePage = (Transfer_SourcePage4)Context.Handler;
string aa = sourcePage.Value;
txt1.Value = aa;
}
}
Cookie傳值
傳送頁面代碼

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Cookie</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="txt1" runat="server" />
<input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
</div>
</form>
</body>
</html>
protected void btn1_ServerClick(object sender, EventArgs e)
{
string aa = txt1.Value;
HttpCookie cookie = new HttpCookie("MyCookie", aa);
Response.Cookies.Add(cookie);
string url = "DestinationPage8.aspx";
Response.Redirect(url, false);
}
接收頁面代碼

protected void Page_Load(object sender, EventArgs e)
{
HttpCookie myCookie = Request.Cookies["MyCookie"];
txt1.Value = myCookie.Value;
}
Session傳值
通過Session取值,在一個頁面中賦值,在其他頁面中共享。為避免造成Session值的混亂無序,應盡量少用Session傳非公共的變量。Session比較適合用來保存一些公共變量。
傳送頁面代碼

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Session傳值</title>
</head>
<body>
<form runat="server">
<input type="text" id="txt1" runat="server" />
<input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
</form>
</body>
</html>
protected void btn1_ServerClick(object sender, EventArgs e)
{
Session["value"] = txt1.Value;
Response.Redirect("DestinationPage2.aspx");
}
接收頁面代碼

protected void Page_Load(object sender, EventArgs e)
{
txt1.Value = Session["value"].ToString();
}
Application傳值
此種方法不常使用,因為Application在一個應用程序域范圍共享,所有用戶可以改變及設置其值,故只應用於計數器等需要全局變量的地方。
傳送頁面代碼

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Application</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="txt1" runat="server" />
<input type="button" id="btn1" value="提交" runat="server" onserverclick="btn1_ServerClick" />
</div>
</form>
</body>
</html>
protected void btn1_ServerClick(object sender, EventArgs e)
{
string aa = txt1.Value;
Application["param1"] = aa;
string url = "DestinationPage7.aspx";
Response.Redirect(url, false);
}
接收頁面代碼

protected void Page_Load(object sender, EventArgs e)
{
txt1.Value = Application["param1"].ToString();
}