黑马程序员--页面、窗体传值

-------------------------------------------- ASP.Net+Android+IO开发、.Net培训、期待与您交流! -----------------------------------

                                                       (一)窗体间传值

WinForm中窗体显示有两种方法:

1:(模式窗体)Form.ShowDialog //只有关闭对话框后,才执行此方法后面的代码,单击“确定”按钮时,DialogResult属性设置为DialogResult.OK 注意:当单击“取消”时,并不是将此窗体Close而只是隐藏此窗体(Visible)

 

2:Form.Show方法    //Show方法后面的代码会立即执行

代码示例:

(Login窗体向Worker窗体传值)

Login窗体中的主要代码:

                                  string a = txt_name.Text;
                            string tg = string.Format("工作人员" + name + "于" + date + "登录了");
                            worker w = new worker();
                            w.Tag = tg;
                            w.Show();
                            this.Hide();

常用的方法总结 一下:
1:Form1中的代码:
  
form2 f2 = new form2();
f2.ShowDialog(this);
       Form2中的代码:
   
form1 f1 = (form1)this.owner;
f1.textBox1.text =this.textBox2.text;
2:Form1中的代码:
form2 f2 = new form2(this);
f2.ShowDialog();
       Form2中的代码:
form1 form1;
public form2(form1 f1)
     InitializeComponent();
     form1 = f1;
}
                                                                                                                (二)页面传值
 

1。使用QueryString
     优点是实现起来非常简单,缺点是传递的值是会显示在浏览器的地址栏上的(不安全),而且不能传递对象使用这种方法的步骤如下:
1
,使用控件创建web表单(form
2,创建可以返回表单的按钮和链接按钮
3,在按钮或链接按钮的单击事件里创建一个保存URL的字符变量
4,在保存的URL里添加QueryString参数
5,使用Response.Redirect重定向到上面保存的
URL
下面的代码片断演示了如何实现这个方法:
  源页面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变量
     首先存储在服务器端,Session变量存储过多的数据会消耗比较多的服务器资源,在使用session时应该慎重,当然了,我们也应该使用一些清理动作来去除一些不需要的session来降低资源的无谓消耗。使用Session变量传递值的一般步骤如下:
1,在页面里添加必要的控件
2,创建可以返回表单的按钮和链接按钮
3,在按钮或链接按钮的单击事件里,把控件的值添加到session变量里
4,使用Response.Redirect(或Server.Transfer)方法重定向到另一个页面
5,在另一个页面提取session的值,在确定不需要使用该session时,要显式清除它
下面的代码片断演示了如何实现这个方法:
   源页面WebForm1.aspx.cs中的部分代码:
private void Button1_Click(object sender, System.EventArgs e)
{
     //textbox1 and textbox2 are webform
     //controls
     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.利用某些控件的PostBackUrl属性

示例:仍然是源页面WebForm1.aspx和目标页面WebForm2.aspx.

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)

{

    TextBox txtName;

    Calendar calendar1;

    txtName=(TextBox)PreviousPage.FindControl("txtName");

    calendar1=(Calendar)PreviousPage.FindControl("Calendar1");

    Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();

}

使用这种方法存在一个问题:如果在没有单击那个按钮之前,也就是未处理WebForm1.aspx之前,有人请求了WebForm2.aspx,该怎么办?这就需要在WebForm2.aspx中的代码处理之前加一个判断.使用IsCrossPagePostBack属性,这与IsPostBack属性很相似,它允许检查请求是否来自WebForm1.aspx.如下:

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

    }

}

4.  使用Cookie对象变量
  这个也是大家常使用的方法,与Session一样,是对每一个用户而言的,但是有个本质的区别,即Cookie是存放在客户端的,而session是存放在服务器端的。而且Cookie的使用要配合ASP.NET内置对象Request来使用。

a.aspxC#代码
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.aspxC#代码
private void Page_Load(object sender, EventArgs e)
{
    string name;
    name = Request.Cookie["name"].Value.ToString();
}

5.  使用Application 对象变量
  Application对象的作用范围是整个全局,也就是说对所有用户都有效。其常用的方法用LockUnLock
a.aspxC#代码
private void Button1_Click(object sender, System.EventArgs e)
{
    Application["name"] = Label1.Text;
    Server.Transfer("b.aspx");
}

b.aspxC#代码
private void Page_Load(object sender, EventArgs e)
{
    string name;
    Application.Lock();
    name = Application["name"].ToString();
    Application.UnLock();
}

 

 -------------------------------------------- ASP.Net+Android+IO开发、.Net培训、期待与您交流! -----------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值