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

点击按钮弹出新窗口,输入数据后返回并刷新页面(C#代码) 窗口互传值

2014-01-04 11:15 751 查看
Webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Webform1.aspx.cs" Inherits="pchgoTest_Webform1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
>

<head>

<title>WebForm1</title>

</head>

<body >

<div align="center">

<form id="Form1" method="post" runat="server">

<asp:label id="Label1" runat="server" Font-Bold="true">

从当前页面打开新窗口,并把变量传递到新窗口的例子,可以多次打开提交。

</asp:label><br/>

<br/>

<asp:textbox id="TextBox1" runat="server" Width="600px">这是初始值,将被传递到新窗口。</asp:textbox><br/>

<br/>

<asp:button id="Button1" runat="server" Width="96px" Text="打开窗口2"></asp:button>

<asp:button id="Button2" runat="server" Width="96px" Text="打开窗口4"></asp:button></form>

</div>

</body>

</HTML>

Webform1.aspx.cs

public partial class pchgoTest_Webform1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

GetScript();

}

protected void GetScript()

{

// 在此处放置用户代码以初始化页面

string strScript = "\n";

if (!IsClientScriptBlockRegistered("clientScript"))

{

strScript = "<script>\n";

strScript += "function OpenWin(){\n";

strScript += "var str=window.showModalDialog('WebForm2.aspx',document.getElementById(\"TextBox1\").value,'help:no')\n";

strScript += "if(str!=null) document.getElementById(\"TextBox1\").value=str\n";

strScript += "}\n";

strScript += "</script>\n";

RegisterClientScriptBlock("clientScript", strScript);

}

if (!IsClientScriptBlockRegistered("clientScript2"))

{

strScript = "<script>\n";

strScript += "function OpenWin2(){\n";

strScript += "var str=window.showModalDialog('WebForm4.aspx',document.getElementById(\"TextBox1\").value,'help:no')\n";

strScript += "if(str!=null) document.getElementById(\"TextBox1\").value=str\n";

strScript += "}\n";

strScript += "</script>\n";

RegisterClientScriptBlock("clientScript2", strScript);

}

Button1.Attributes.Add("onclick", "OpenWin()");

Button2.Attributes.Add("onclick", "OpenWin2()");

}

}

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

Webform2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Webform2.aspx.cs" Inherits="pchgoTest_Webform2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<title>WebForm2</title>

</head>

<frameset rows="0,*">

<frame src="about:blank">

<frame src="WebForm3.aspx">

</frameset>

</html>

WebForm3.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm3.aspx.cs" Inherits="pchgoTest_WebForm3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<title>WebForm3</title>

</head>

<body ms_positioning="GridLayout" id="MyBody" runat="server">

<form id="Form1" method="post" runat="server">

<asp:Label ID="Label1" runat="server">请输入您的大名:</asp:Label><br>

<br>

<asp:TextBox ID="TextBox1" runat="server" Width="320px"></asp:TextBox><br>

<br>

<asp:Button ID="Button1" runat="server" Text=" 提 交 "></asp:Button>

</form>

</body>

</html>

WebForm3.aspx.cs

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class pchgoTest_WebForm3 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (IsPostBack)

{

string strScript = "<script>\n";

strScript += "window.parent.returnValue='" + TextBox1.Text.Replace("'", "\'") + "'\n";

strScript += "window.parent.close()\n";

strScript += "</script>\n";

if (!IsClientScriptBlockRegistered("clientScript"))

RegisterClientScriptBlock("clientScript", strScript);

}

else

{

MyBody.Attributes.Add("onload", "document.getElementById(\"TextBox1\").value=window.parent.dialogArguments");

}

}

}

Webform4.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Webform4.aspx.cs" Inherits="pchgoTest_Webform4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<title>WebForm4</title>

</head>

<body ms_positioning="GridLayout">

<form id="Form1" method="post" runat="server">

<iframe frameborder="no" src='WebForm3.aspx' style="width: 368px; height: 192px"></iframe>

</form>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐