页面间传值的方法

来源:互联网 发布:javascript运行机制 编辑:程序博客网 时间:2024/06/05 22:56

页面间传值的方法:
1 使用QueryString
2 使用Session变量
3 使用Server.Transfer
4 使用类的静态属性
5 使用Application全局变量
6 使用隐藏域

1 使用QueryString:
   描述使用URL传值
   优点:占用资源低
   缺点:传递类型单一;安全性差
   例子:


Private void Button1_ Click(object sender.System.EventArgs e)
...{
//定义一个字符串,此字符串中包含源页面向目标页面传送的数据
     string strUrl;
//从源页面中获得传送的数据
     strUrl=”WebForm2.aspx? userid = ” + TextBox1.Text+ ”&username=”+TextBox2.Text;
//向目标页面传送数据
     Response.Redirect(strUr1);
)

private void Page_ Ioad(object sender,System.EventArgs e)
...{
//使用QueryString从源页面中接受userid、username变量数据,
    并通过labell、label2显示
    Label1.Text  = Request.QueryString["userid"];
    Label2.Text  =  Request.QueryString["username"];
)
2  使用Session变量
描述:
服务器端控件可以保存各种类型变量
优点:可以传递任何类型的变量,安全性高
缺点:占用系统资源多

private void Button1_Click(object sender,System.EventArgs c)
...{
//创建Session变量,用以存放TextBoxl、TextBox2组件中的数据
    Session[“userid”] =  TextBox1.Text;
    Session[“username”] = TextBox2.Text;
//向目标页面传送数据
    Server.Transfer(“WebForm2.aspx”);
}

private void Page_ Ioad(object sender,System.EventArgs e)
...{
//从源页面中接受数据并显示出来
    Labe1.Text=SessionE“userid”].ToString();
    Labe2.Text-Session[“usernflme”].ToString();
//清除创建的Session变量
   Session.Remove(“userid”);
   Session.Remove(“username”);
)
3 使用Server.Transfer
描述:以页面对象属性的方式传递信息到另一个页面。
优点:不占用内存,适合传递大容量的数据。
缺点:只能在一个服务器传递数据。

public class WehForml:System.Web.UI.Page
...{
      protected System.Web.UI.WebControls.TextBox TextBoxl;
      protected System.Web.UI.WebControls.TextBox FextBox2;
      protected System.Web.UI.WebControls.Button Button1;

private void Page_Ioad(object sender,System.EventArgs e)
...{
)
public string userid
(
    get
     ...{
        return TextBox1.Text;
     }
}
public string username
...{
     get
    ...{
      return TextBox2.Text;
    }
}
private void Button1_Click(object sender,System.EventArgs e)
...{
    Server.Transfer(”WebForm2.aspx”,true);
}
}


public class WebForm2 :System.Web.UI.Page
...{
private void Page_Ioad(object sender,System.EventArgs e)
...{
   WebForml webform1;
   webform1= (W ebForm1)Context.Handler:
   Iabel1.Text  = webform1.userid;
   label2.Text = wehform1.username;
}
}
4 使用类的静态属性
描述:
1 在页面里添加必要的控件。
2 定义一个包含静态属性的类.
3 创建可以返回表单的按钮和链接按钮.
4 在按钮单击事件中将要传送的值赋给静态属性并用Response.Redirect方法重定向到目标页面.
5 目标页面中可以通过静态属性获得源页面中要传送过来的值 .

//源页面
public class SProperty
...{
   public static string userid=””;
   pubic static string username=’”’:
   private void Buttonl_Click(object sender。System.EventArgs e)
   ...{
   //将要传送到目标页面的值赋给类的静态属性
    SProperty.userid = TextBox1Text;
    SProperty.username = TextBox2. Text;
    Response.Redirect(”W ebForm2.aspx”);
    }
}

//目标页面
private void Page_ I oad(object sender,System.EventArgs e)
...{
    Iabel1.Text = SProperty.userid;
    Iabel2.Text=  SProperty.username;
}
5 使用Application全局变量
描述:
Application变量是全局性的,一旦定义,它将影响到程序的所有部分。

//源页面WebForml中代码:
Application[“userid”] = TextBox1.Text;
Application[“username”] = TextBox2.Text...{
Server.Transfer[WebForm2.aspx];
//目标页面WebForm2中代码:
Label1.Text=Application[“userid”].ToString();
Label2.Text=Application[“username”].ToString();
6 使用隐藏域
描述:当一个网页提交给服务器时,隐藏域的内容和其它控件的值一块儿被送到HTTP Form集合中;,而且必须被显式地添加在网页上.使用隐藏域,必须使用HTTP—Post方法提交网页

//ASP.NET中的HtmlInputHidden组件提供
//了隐藏域的功能.该组件用来设置隐藏字段的值,其
//语法格式是:
<input type=”hidden”id=”组件名称”runat=”server”>

//利用隐藏域传值的程序语句如下:
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;

原创粉丝点击