大家都知道aspx在同一个页面中传值是很容易的(如textBox1.Text等)但有时必须向另一个页面传值用以前asp的方法的确觉得不怎么样,还要用Request.Form等东西好象又回到了从前。我这里使用一个类的静态属性作为两个类的外的全局变量实现了两个页面间传值.举一个简单的例子如下:
首先定义一个包含静态属性的类:
using System;
namespace study
{
///
<summary>
/// CIndex 的摘要说明。
///
</summary>
public class
CIndex
{
public static string
name="";
public
CIndex()
{
}
}
}
然后在一个包含信息提交的页面中这样写:
<%@ Page language="c#"
Codebehind="submit.aspx.cs" AutoEventWireup="false"
Inherits="study.submit" %>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN"
>
<HTML>
<HEAD>
<title>submit</title>
<meta
name="GENERATOR" Content="Microsoft Visual Studio
7.0">
<meta name="CODE_LANGUAGE"
Content="C#">
<meta
name="vs_defaultClientScript"
content="JavaScript">
<meta
name="vs_targetSchema" content=" href="http://schemas.microsoft.com/intellisense/ie5">http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body
MS_POSITIONING="GridLayout">
<form
runat="server"
ID="Form1">
<asp:TextBox
id="textBox1" runat="server"
/>
<asp:Button text="提交"
runat="server" ID="Button1"
/>
</form>
</body>
</HTML>
Codebehind:
首先引入study命名空间
using
study;
再加入Button1的Click事件
private void
Button1_Click(object sender, System.EventArgs
e)
{
CIndex.name=textBox1.Text;//将要传到另一页的值赋给类的静态属性
Response.Redirect("getsubmit.aspx");
}
然后在另一个页面里面的codebehind里加入下面代码:
private void
Page_Load(object sender, System.EventArgs
e)
{
Response.Write("你输入的参数值是:
"+CIndex.name);
}
这样就完成了.简单吧.