您的位置:首页 > 编程语言 > ASP

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

2014-01-18 09:24 495 查看
假设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");
}

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

更多详细内容请查看:http://www.111cn.net/net/net/49465.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: