页面之间传值方法.doc_第1页
页面之间传值方法.doc_第2页
页面之间传值方法.doc_第3页
页面之间传值方法.doc_第4页
页面之间传值方法.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

3.4.1 页面之间传值方法页面间传值的方法:1 使用QueryString2 使用Session变量3 使用ServerTransfer4 使用类的静态属性5 使用Application全局变量6 使用隐藏域1 使用QueryString:描述使用URL传值优点:占用资源低缺点:传递类型单一;安全性差 例子:protected void btn_Submit_Click(object sender, EventArgs e) /定义一个字符串,此字符串中包含源页面向目标页面传送的数据 string strUrl = string.Empty; /从源页面中获得传送的数据 strUrl=./Label.aspx?username= + this.txt_UserName.Text.Trim() + &userpwd= + this.txt_PWD.Text; /向目标页面传送数据 Response.Redirect(strUrl);/protected void Page_Load(object sender, EventArgs e) /使用QueryString从源页面中接受、username userpwd变量数据, /并通过labell、label2显示 this.txtName.Text = Request.QueryStringusername; this.txtPassword.Text = Request.QueryStringuserpwd;2 使用Session变量描述:服务器端控件可以保存各种类型变量优点:可以传递任何类型的变量,安全性高缺点:占用系统资源多protected void btn_Submit_Click(object sender, EventArgs e) /创建Session变量,用以存放TextBoxl、TextBox2组件中的数据 Sessionusername = this.txt_UserName.Text; Sessionuserpwd = this.txt_PWD.Text; /向目标页面传送数据 Server.Transfer(SessionReceive.aspx);protected void Page_Load(object sender, EventArgs e) /从源页面中接受数据并显示出来 this.Label1.Text = Sessionusername.ToString(); this.Label2.Text = Sessionuserpwd.ToString(); /清除创建的Session变量 Session.Remove(username); Session.Remove(userpwd); 3 使用ServerTransfer描述:以页面对象属性的方式传递信息到另一个页面。优点:不占用内存,适合传递大容量的数据。缺点:只能在一个服务器传递数据。注意:这个例子在 webApplication中执行public class WehForml:System.Web.UI.Pageprivate void Page_Ioad(object sender,System.EventArgs e)public string userid get return TextBox1.Text; public string username get return TextBox2.Text; protected void Button1_Click1(object sender, EventArgs e) Server.Transfer(WebForm2.aspx, true); public class WebForm2 :SystemWebUIPageprotected void Page_Load(object sender, EventArgs e) WebForm1 webform1; webform1= (WebForm1)Context.Handler; Label1.Text = webform1.userid; Label2.Text = webform1.username; 4 使用类的静态属性描述:1 在页面里添加必要的控件。2 定义一个包含静态属性的类3 创建可以返回表单的按钮和链接按钮4 在按钮单击事件中将要传送的值赋给静态属性并用ResponseRedirect方法重定向到目标页面5 目标页面中可以通过静态属性获得源页面中要传送过来的值 /源页面public partial class WebForm1 : System.Web.UI.Page public static string userid = ; public static string username = ; protected void Page_Load(object sender, EventArgs e) protected void Button1_Click(object sender, EventArgs e) /将要传送到目标页面的值赋给类的静态属性 WebForm1.userid = this.TextBox1.Text; WebForm1.username = this.TextBox2.Text; Response.Redirect(WebForm2.aspx); /目标页面public partial class WebForm2 : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) this.Label1.Text = WebForm1.userid; this.Label2.Text = WebForm1.username; 5 使用Application全局变量描述:Application变量是全局性的,一旦定义,它将影响到程序的所有部分。/源页面WebForml中代码:protected void Button2_Click(object sender, EventArgs e) Applicationuserid = TextBox1.Text; Applicationusername = TextBox2.Text; Server.Transfer(WebForm3.aspx); /目标页面WebForm2中代码:protected void Page_Load(object sender, EventArgs e) Label1.Text=Applicationuserid.ToString(); Label2.Text = Applicationusername.ToString(); 6 使用隐藏域描述:当一个网页提交给服务器时,隐藏域的内容和其它控件的值一块儿被送到HTTP Form集合中;,而且必须被显式地添加在网页上使用隐藏域,必须使用HTTPPost方法提交网页/ASPNET中的HtmlInputHidden组件提供/了隐藏域的功能该组件用来设置隐藏字段的值,其/语法格式是:/利用隐藏域传值的程序语句如下:protected System.Web.UI.HtmlControls.HtmllnputHidden Hidden1;protected System.Web.UI.HtmlControls.HtmllnputHidden Hidden2;/给隐藏域赋值Hidden1.Value = Text1.Text;Hidden2.Value = Text2.Text;/获得隐藏域的值Iabel1.Text = Hidden1.Value;Iabe2.Text = Hidden2.Va1ue;post和get的区别?1. get是从服务器上获取数据,post是向服务器传送数据。2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。post是通过HTTP post机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。3. 对于get方式,服务器端用Request.QueryString获取变量的值,对于post方式,服务器端用Request.Form获取提交的数据。4. get传送的数据量较小,不能大于2KB。p

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论