用Session可将值从一个窗体传到另一个窗体
第一个窗体代码(Page_Load 或 Click 中):
string a = "1";
string b = "2";
Session.Add("A",a);
Session.Add("B",b);
第二个窗体代码(Page_Load中):
string a = (string)(Session["A"]);
string b = (string)(Session["B"]);
第一个窗体代码(Page_Load 或 Click 中):
string a = "1";
string b = "2";
Session.Add("A",a);
Session.Add("B",b);
第二个窗体代码(Page_Load中):
string a = (string)(Session["A"]);
string b = (string)(Session["B"]);
传值??
在ASP.NET中有几种基于页面间的传值方法:常见的有QueryString.这种方法最简单不过了,但缺点就是它把要传送的值显示在地址栏中,如果对于在安全性的信息来说这并不是一种好的解决方案。还有一个缺点就是它不能传对象。这种方法适合于传送一个简单的值及安全性不大重要的信息。例:
有两个页面:WebForm1.aspx,WebForm2.aspx。
在WebForm1.aspx.cs的某些事件中放置如下代码:
string url="WebForm2.aspx?name="+this.txtname.Text;
Response.Redirect(url);
接着关键时刻出现了:在WebForm2.aspx.cs中某些事件中放置如下代码:
lblname.Text=Request.QueryString["name"];
OK! 整个传值过程完成! 还有一种是利用Session变量来传值,这种也比较常用。利用Session比较灵活,可以
在多个页面间进行传值。当调用其remove后,Session即失效。同样用两张页面举个例:
WebForm1.aspx.cs中写入如下代码:
Session["name"]=txtname.Text;
Response.Redirect("WebForm2.aspx");
下面在WebForm1.aspx.cs中取出Session的值:
lblname.Text=Session["name"].ToString(); //因为取出来的是一个对象,所以必须转换类型
Session.Remove("name");? //使Session失效。 第三种方式是利用Request对象来取值
第四种方法是利用Server对象的Transfer()方法来传值,它接收一个页面对象。下面用代码来讲解一下这种实现方式:在WebForm1.aspx 的某个事件中写入如下代码:Server.Transfer("WebForm2.aspx"); //传递页面对象
接着就可以在WebForm2.aspx.cs中获取WebForm1的页面对象。
if(Context.Handler is WebForm1) //判断传递过来的是否WebForm1,因为可能会有多个传递对象。
{
WebForm1 f1=(WebForm1)Context.Handler; //通过Context.Handler来创建一个WebForm1的页面对象,返 回一个object
Response.Write("Hey,I get it with Context Handler "+((TextBox)f1.FindControl("TextBox1")).Text+" ");??
}
//最后通过调用WebForm1对象的FinControl()方法来查找WebForm1中名为"TextBox1"的一个文本控件,在此也可以换成其它的控件,不管查找的是哪一种控件,都必须将其强制转换为其类型,本示例中为TextBox,最后调用其属性Text,获取在TextBox1中的值,达到传值的效果。
这种方法同样很灵活,可以传递多个页面对象,而不是传递值,因此当获取到页面对象的时候就可以获取此页面对象中的某些控件的值或其它的数据。
{
public void MyMethod(int i) {i = 10;}
public void MyMethod(ref int i) {i = 10;}
}
但以下重载声明是无效的:class MyClass
{
public void MyMethod(out int i) {i = 10;}
public void MyMethod(ref int i) {i = 10;}
}
有关传递数组的信息,请参见使用 ref 和 out 传递数组。示例
// cs_ref.cs
using System;
public class MyClass
{
public static void TestRef(ref char i)
{
// The value of i will be changed in the calling method
i = 'b';
} public static void TestNoRef(char i)
{
// The value of i will be unchanged in the calling method
i = 'c';
} // This method passes a variable as a ref parameter; the value of the
// variable is changed after control passes back to this method.
// The same variable is passed as a value parameter; the value of the
// variable is unchanged after control is passed back to this method.
public static void Main()
{
char i = 'a'; // variable must be initialized
TestRef(ref i); // the arg must be passed as ref
Console.WriteLine(i);
TestNoRef(i);
Console.WriteLine(i);
}
}
输出
b
b
using System;
class RefOut
{
public static void Main(String [] args)
{
int a = 0,b,c=0,d=0;
string s="000";
Console.WriteLine("a is normal parameter will not affect the changes after the function call");
Console.WriteLine("b is out parameter will affect the changes after the function call but not necessary to initialize the variable b but should be initialized in the function ParamTest ");
Console.WriteLine("c is ref parameter will affect the changes after the function call and is compulsory to initialize the variable c before calling the function ParamTest");
Console.WriteLine("d is used to store the return value");
Console.WriteLine("a = {0}", a);
//Console.WriteLine("b = {0}", b);
Console.WriteLine("c = {0}", c);
Console.WriteLine("s = {0}", s);
d=ParamTest(a,out b,ref c,out s);
Console.WriteLine("a = {0}", a);
Console.WriteLine("b = {0}", b);
Console.WriteLine("c = {0}", c);
Console.WriteLine("d = {0}", d);
Console.WriteLine("s = {0}", s);
}
public static int ParamTest(int a, out int b, ref int c,out string s)
{
a=10;
b=20;
c=30;
s="abc";
Console.WriteLine("a = {0}", a);
Console.WriteLine("b = {0}", b);
Console.WriteLine("c = {0}", c);
Console.WriteLine("s = {0}", s);
return 40;
} }