WinForm传值方式汇总
1.通过静态变量传值
public class TestA
{
public static string value;
}
在Form1中跳转到Form2的时候,可以赋值
TestA.value="跳转到窗体2中的测试";
new Form2().Show();
在Form2中构造函数或Load中接收值:
this.Label1.Text = TestA.value;
2.通过公共变量传值
Form1中
public static string TestValueA;
private void button1_Click(object sender, EventArgs e)
{
TestValueA= "Form1中的测试值";
new Form2().Show();
}
在Form2中
private void Form2_Load(object sender, EventArgs e)
{
MessageBox.Show(Form1.TestValueA);
}
3.静态方法方式访问
Form1中:
{
new Form2().Show();
}
public static void FF()
{
MessageBox.Show("Form1方法");
}
Form2中:
{
Form1.FF();
}
在窗体Form2中定义一个公有属性Form2Value,获取和设置textBox1的文本值
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text = value;
}
}
在窗体Form1中这样调用
f2.Form2Value = "Ok"; //给Form2的textBox1赋值Ok
f2.ShowDialog ( );
Form1:
{
new Form2("来自Form1").Show();
}
Form2:
{
InitializeComponent();
MessageBox.Show(vaue);
}
在窗体Form2中
string value2;
public Form2 ( int value1 , string value2 )
{
InitializeComponent ( );
this.value1 = value1;
this.value2 = value2;
}
在窗体Form1中这样调用
Form1中代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WinTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//单击该按钮时SHOW出第二个窗体
Form2 fm2 = new Form2();
fm2.myevent += new Form2.mydelegate(givevalue);//在SHOW出窗体的同时订阅FORM2的事件,调用givevalue()方法.
fm2.ShowDialog();
}
public void givevalue(string text) //用于修改label的方法
{
this.label1.Text = text;
}
}
}
Form2代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WinTest
{
public partial class Form2 : Form
{
public delegate void mydelegate(string text);//定义一个委托
public event mydelegate myevent;//定义上诉委托类型的事件
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//在单击该窗体上的按钮时触发事件
if (myevent != null)
{
myevent(textBox1.Text);
}
}
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
string url;
url="WebForm2.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text;
Response.Redirect(url);
}
目标页面WebForm2.aspx.cs中的部分代码:
private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text=Request.QueryString["name"];
Label2.Text=Request.QueryString["email"];
}
private void Button1_Click(object sender, System.EventArgs e)
{
Session["name"]=TextBox1.Text;
Session["email"]=TextBox2.Text;
Server.Transfer("WebForm2.aspx");
}
目标页面WebForm2.aspx.cs中的部分代码:
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");
}
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();
}
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();
}
WebForm1.aspx中的部分代码:
<asp:Button ID="btnPostBack" Runat="server" Text="PBButton"></asp:Button>
<asp:TextBox ID="txtName" Runat="server" ></asp:TextBox>
<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
protected void Page_Load(object Sender,System.EventArgs e)
{
if(PreviousPage.IsCrossPagePostBack)
{
TextBox txtName;
Calendar calendar1;
txtName=(TextBox)PreviousPage.FindControl("txtName");
calendar1=(Calendar)PreviousPage.FindControl("Calendar1");
Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();
}
else
{
Response.Redirect("WebForm1.aspx");
}
}
源页面WebForm1.aspx.cs中的部分代码:
把以下的代码添加到页面中
public string Name
{
get
{
return TextBox1.Text;
}
}
public string EMail
{
get
{
return TextBox2.Text;
}
}
然后调用Server.Transfer方法
private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer("WebForm2.aspx");
}
目标页面代码:
在WebForm2.aspx中务必在第一句话添加
<%@ Reference Page="~/WebForm1.aspx" %>或
<%@ PreviousPageType VirtualPath="~/WebForm1.aspx" %>
然后在WebForm2.aspx.cs中添加如下。
private void Page_Load(object sender, System.EventArgs e)
{
//create instance of source web form
WebForm1 wf1;
//get reference to current handler instance
wf1=(WebForm1)Context.Handler;
Label1.Text=wf1.Name;
Label2.Text=wf1.EMail;
}
浙公网安备 33010602011771号