码迷,mamicode.com
首页 > Windows程序 > 详细

ASP.NET 窗体间传值实现方法详解

时间:2014-12-02 14:47:25      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:http   io   ar   os   使用   sp   java   for   on   

假设ParentForm.aspx 页面上有TextBox1文本框和Open按钮
点击Open按钮弹出SubForm.aspx,SubForm.aspx页面上有TextBox1文本框和Close按钮
点击Close按钮关闭SubForm.aspx页面,并把子页面SubForm.aspx文本框的值显示到父页面ParentForm.aspx 的文本框上。

父窗体前台代码:

 代码如下 复制代码
      <script type="text/javascript">
        function OpenSubForm(ret) {
            var strPath = "http://www.111Cn.NeT /subForm.aspx"
            var nHeight = 500
            var nWidth = 500
            var feature
            feature = "Height= " + nHeight + ",Width=" + nWidth + ",top=30,Left=30";
            feature += ",dependent=yes,location=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no;";
            window.open(strPath+"?Ret_Form=Form1&Ret_Value="+ret,‘subForm‘,feature).focus();
            return false;
        }
    </script>

父窗体后台代码:
 

 代码如下
private void Page_Load(object sender, System.EventArgs e)
        {
            // ペ?ジを初期化するユ?ザ? コ?ドをここに?啡毪筏蓼?br />             this.Button1.Attributes.Add("onClick","return OpenSubForm(‘TextBox1‘);");
        }

子窗体后台代码:
 

 代码如下

        private void Button1_Click(object sender, System.EventArgs e)
        {
            string strScript =string.Empty;
            string strRetForm = String.Empty;
            string strRetValue=String.Empty;
            strRetForm=Request.Params["Ret_Form"];
            strRetValue=Request.Params["Ret_Value"];
            if (strRetForm == string.Empty)
            {
                strRetForm= "document.forms[0]";
            }
            strScript = "<script language=javascript>";
            strScript += "window.opener." + strRetForm;
            strScript += "." + strRetValue + ".value=‘" + this.TextBox1.Text.Trim() + "‘;";
            strScript += "window.close();";
            strScript += "</script>";
            Response.Write(strScript);
        }


 
 

上面是js其实也就是页面传值了,下面我把一些页面传值的代码发给大家参考。

页面间传值的几种方式 .

下面的代码片断演示了如何实现这个方法:
  源页面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"];
}

使用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");
}

上面两种是常用的其它的就不介绍了,大家可自行去参考

 

ASP.NET 窗体间传值实现方法详解

标签:http   io   ar   os   使用   sp   java   for   on   

原文地址:http://www.cnblogs.com/taofx/p/4137202.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!