当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net子窗体与父窗体交互实战分享

asp.net子窗体与父窗体交互实战分享

2017年12月12日  | 移动技术网IT编程  | 我要评论
今天在项目上遇到了这个问题,其实只是window.returnvalue的简单应用,不是asp.net的专属内容。作为积累,记录一个简单的实现模型。 图1 

今天在项目上遇到了这个问题,其实只是window.returnvalue的简单应用,不是asp.net的专属内容。作为积累,记录一个简单的实现模型。

图1  用到的文件

   从图1中我们可以看到,只用到了两个页面,其中default.aspx作为父页面,default2.aspx作为子页面被弹出。default.aspx页面上有两个textbox一个button,代码如下:

复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default"%>

<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox runat="server" id="a1">
</asp:textbox>
<asp:textbox id="textbox1" runat="server" ontextchanged="textbox1_textchanged"></asp:textbox>
<asp:button id="button1" runat="server" text="button" onclick="button1_click"/>
</div>
</form>
</body>
</html>

在button1的click事件中,我们注册弹窗脚本,代码如下:
复制代码 代码如下:

protectedvoid button1_click(object sender, eventargs e)
{
stringbuilder s =new stringbuilder();
s.append("<script language=javascript>");
s.append("var a=window.showmodaldialog('default2.aspx');");
s.append("if(a!=null)");
s.append("document.all('textbox1').value=a;");
s.append("</script>");
type cstype =this.gettype();
clientscriptmanager cs = page.clientscript;
string sname ="lt";
if (!cs.isstartupscriptregistered(cstype, sname))
cs.registerstartupscript(cstype, sname, s.tostring());
}

其中  s.append("var a=window.showmodaldialog('default2.aspx');");一句用来弹窗default2.aspx页面并接收它的返回值。

接收了返回值之后我们把它赋值给textbox1.

 default2.aspx页面有一个textbox和一个button,代码如下:

 (这里需要注意的是在head里的<base target="_self"/>标记十分重要。

复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codefile="default2.aspx.cs" inherits="default2"%> <!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></title>
<base target="_self"/>
</head>

<body>
<form id="form1" runat="server">
<div>
<asp:textbox runat="server" id="t1"></asp:textbox>
<asp:button id="button1" runat="server" text="button" onclick="button1_click"/>

</div>
</form>
</body>
</html>

我们在default2.aspx页面的button_click事件中使用脚本返回一个值给父页面。代码如下:
复制代码 代码如下:

protectedvoid button1_click(object sender, eventargs e)
{
stringbuilder s =new stringbuilder();
s.append("<script language=javascript>"+"\n");
s.append("window.returnvalue='"+this.getselectvalue() +"';"+"\n");
s.append("window.close();"+"\n");
s.append("</script>");
type cstype =this.gettype();
clientscriptmanager cs = page.clientscript;
string csname ="ltype";
if (!cs.isstartupscriptregistered(cstype, csname))
cs.registerstartupscript(cstype, csname, s.tostring());

}

脚本注册成功之后,我们可以做如下的实验:

1)打开default1.aspx页面在id为a1的textbox中输入数字55,然后点击button

2)在弹窗中输入数字66再点子窗体的按钮关闭子窗体。

3)查看结果

从结果中,我们可以看出我们保留了先输入到父窗体中的值,又接收了从子窗体传递过来的值。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网