-------------------------------------------- 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();
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.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. 使用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();
}
-------------------------------------------- ASP.Net+Android+IO开发、.Net培训、期待与您交流! -----------------------------------