阳漳一哥
希望将自己所看,所读,所想,所做,所悟进行摘取-汇总-记录-分享!和大家一起共同学习,谢谢大家,欢迎来逛!

WinForm传值方式汇总

1.通过静态变量传值

public  class TestA

{

   public static string value;

}

在Form1中跳转到Form2的时候,可以赋值

TestA.value="跳转到窗体2中的测试";

new Form2().Show();

在Form2中构造函数或Load中接收值:

this.Label1.Text = TestA.value;

TestA.value = "Form2中的值"; //给app.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中:

private void button1_Click(object sender, EventArgs e)
{
new Form2().Show();
}
public static void FF()
{
MessageBox.Show("Form1方法");
}

Form2中:

private void Form_Load(object sender, EventArgs e)
{
Form1.FF();
}
 
4.通过窗体的公有属性传值

在窗体Form2中定义一个公有属性Form2Value,获取和设置textBox1的文本值

public string Form2Value
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text = value;
}
}

在窗体Form1中这样调用
Form2 f2 = new Form2 ( );
f2.Form2Value = "Ok"; //给Form2的textBox1赋值Ok
f2.ShowDialog ( );
 
5.传参式传值

Form1:

private void button1_Click(object sender, EventArgs e)
{
new Form2("来自Form1").Show();
}

Form2:
public Form2(string value)
{
InitializeComponent();
MessageBox.Show(vaue);
}
 
6.通过构造函数传值
 

在窗体Form2中

int value1;
string value2;
public Form2 ( int value1 , string value2 )
{
InitializeComponent ( );
this.value1 = value1;
this.value2 = value2;
}

在窗体Form1中这样调用
new Form2 ( 111 , "222" ).Show ( ); //这样就把111,"222",这2个值传送给了Form2
 
7.用委托来实现传值
 

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);
            }
        }
    }
}

 
 
WebForm传值方式汇总
 
1.使用QueryString传值
 
源页面WebForm1.aspx.cs中的部分代码:
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"];
}
 
2.使用Session变量传值
源页面WebForm1.aspx.cs中的部分代码:
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");
}
 
3.使用Application 对象变量传值

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();
}

 
4.使用Cookie对象变量传值

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();
}

 
5.利用某些控件的PostBackUrl属性
 

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>

 
WebForm2.aspx.cs中的部分代码:

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");

    }

}

 6.使用Server.Transfer传值

源页面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;
}

 
 
 
 
 
 
 
 
 
 
posted on 2014-12-11 15:54  阳漳一哥  阅读(290)  评论(0)    收藏  举报