C#中的五种界面间传值方法

来源:互联网 发布:webkaka软件下载 编辑:程序博客网 时间:2024/05/14 17:05
C#中五种界面间传值方法:

 一、Session传值(保存在服务端)


login.aspx.cs页面中:
        新建一个login.aspx页面,添加用户名和密码,以及两个文本框,和一个Button安扭,在button按钮的单击事件中,填写如下代码:
<span style="font-family:SimHei;font-size:12px;"><span style="white-space:pre"></span>Session["name"] = TextBox1.Text.Trim();<span style="white-space:pre"></span>Session["password"] = TextBox2.Text.Trim();<span style="white-space:pre"></span>Response.Redirect("index.aspx");</span>

index.aspx.cs页面中

   新建一个index.aspx页面,在Page_load()方法中写入如下代码:
<span style="white-space:pre"></span> <span style="font-family:SimHei;">Response.Write("<br />你的用户名是:"+Session["name"].ToString());    Response.Write("<br />你的密码是:" + Session["password"].ToString());</span>

用完后消除Session的值,以免浪费系统资源 :Session.Remove("name"); Session.Remove("password");

            

二、通过URL传值(这种传值方法不安全,信息会显示在地址栏)


     login.aspx.cs页面中
<pre name="code" class="csharp"><span style="font-family:SimHei;"><span style="white-space: pre;"></span>string name = TextBox1.Text.Trim();     string password = TextBox2.Text.Trim();<span style="white-space: pre;"></span>Response.Redirect("index.aspx?name="+name+"&password="+password);</span>

    index.aspx.cs页面中
<span style="font-family:SimHei;">     <span style="white-space:pre"></span>Response.Write("<br />你的用户名是:"+Request.QueryString["name"]);    <span style="white-space:pre"></span>Response.Write("<br />你的密码是:"+Request.QueryString["password"</span>]);

三、Cookie传值(保存在客户端)


         login.aspx.cs页面中
<span style="font-family:SimHei;">        string name = TextBox1.Text.Trim();        string password = TextBox2.Text.Trim();        //实例化cookie类        HttpCookie cookie_name = new HttpCookie("name");        HttpCookie cookie_password = new HttpCookie("password");        cookie_name.Value = name ;        cookie_password.Value = password;        Response.AppendCookie(cookie_name );        Response.AppendCookie(cookie_password);        Server.Transfer("index.aspx");//跳转到页面index.aspx</span>
         index.aspx.cs页面中
<span style="white-space:pre"></span><span style="font-family:SimHei;">Response.Write("<br/>你的用户名是:" +Request.Cookies["name"].Value.ToString());       Response.Write("<br/>你的密码是:" + Request.Cookies["password"].Value.ToString());</span>
    注意:执行后浏览器的临时文件夹中会生成一个txt的文本文件,该文件包含cookie所传的值。

四、使用Application 对象变量

  a.aspx的C#代码
<span style="white-space:pre"></span><span style="font-family:SimHei;">private void Button1_Click(object sender, System.EventArgs e)        {<span style="white-space:pre"></span>Application["name"] = Label1.Text;<span style="white-space:pre"></span>Server.Transfer("b.aspx");        }</span>
b.aspx中C#代码
<span style="white-space:pre"></span><span style="font-family:SimHei;">private void Page_Load(object sender, EventArgs e)        {<span style="white-space:pre"></span>string name;<span style="white-space:pre"></span>Application.Lock();<span style="white-space:pre"></span>name = Application["name"].ToString();<span style="white-space:pre"></span>Application.UnLock();        } </span>

五、使用Server.Transfer方法
   
a.aspx的C#代码
<span style="white-space:pre"></span><span style="font-family:SimHei;">public string Name        {<span style="white-space:pre"></span>get{ return Label1.Text;}        }<span style="white-space:pre"></span>private void Button1_Click(object sender, System.EventArgs e)        {<span style="white-space:pre"></span>Server.Transfer("b.aspx");        }</span>

b.aspx中C#代码

<span style="font-family:SimHei;"><span style="white-space:pre"></span>private void Page_Load(object sender, EventArgs e)        {<span style="white-space:pre"></span>a newWeb;//实例a窗体<span style="white-space:pre"></span>newWeb = (source)Context.Handler;<span style="white-space:pre"></span>string name;<span style="white-space:pre"></span>name = newWeb.Name;        }</span>