asp.net 父窗体获取子窗体的返回值,可用来对父窗体局部更新
今天在项目上遇到了这个问题,其实只是window.returnValue的简单应用,不是asp.net的专属内容。作为积累,记录一个简单的实现模型。
图1 用到的文件
从图1中我们可以看到,只用到了两个页面,其中Default.aspx作为父页面,Default2.aspx作为子页面被弹出。Default.aspx页面上有两个TextBox一个Button,代码如下:
1 <% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default.aspx.cs " Inherits = " _Default " %>
2
3 <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
4
5 < html xmlns = " http://www.w3.org/1999/xhtml " >
6 < head runat = " server " >
7 < title ></ title >
8
9 </ head >
10 < body >
11 < form id = " form1 " runat = " server " >
12 < div >
13 < asp:TextBox runat = " server " ID = " a1 " >
14 </ asp:TextBox >
15 < asp:TextBox ID = " TextBox1 " runat = " server " ontextchanged = " TextBox1_TextChanged " ></ asp:TextBox >
16 < asp:Button ID = " Button1 " runat = " server " Text = " Button " onclick = " Button1_Click " />
17
18
19 </ div >
20
21 </ form >
22 </ body >
23 </ html >
24
在Button1的Click事件中,我们注册弹窗脚本,代码如下:
1 protected void Button1_Click( object sender, EventArgs e)
2 {
3 StringBuilder s = new StringBuilder();
4 s.Append( " <script language=javascript> " );
5 s.Append( " var a=window.showModalDialog('Default2.aspx'); " );
6 s.Append( " if(a!=null) " );
7 s.Append( " document.all('TextBox1').value=a; " );
8 s.Append( " </script> " );
9 Type cstype = this .GetType();
10 ClientScriptManager cs = Page.ClientScript;
11 string sname = " lt " ;
12 if ( ! cs.IsStartupScriptRegistered(cstype, sname))
13 cs.RegisterStartupScript(cstype, sname, s.ToString());
14 }
其中 s.Append("var a=window.showModalDialog('Default2.aspx');");一句用来弹窗Default2.aspx页面并接收它的返回值。
接收了返回值之后我们把它赋值给TextBox1.
Default2.aspx页面有一个TextBox和一个Button,代码如下:
(这里需要注意的是在head里的<base target="_self" />标记十分重要。
1 <% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default2.aspx.cs " Inherits = " Default2 " %>
2
3 <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
4
5 < html xmlns = " http://www.w3.org/1999/xhtml " >
6 < head >
7 < title ></ title >
8 < base target = " _self " />
9 </ head >
10
11 < body >
12 < form id = " form1 " runat = " server " >
13 < div >
14 < asp:textbox runat = " server " ID = " t1 " ></ asp:textbox >
15 < asp:Button ID = " Button1 " runat = " server " Text = " Button " onclick = " Button1_Click " />
16
17 </ div >
18 </ form >
19 </ body >
20 </ html >
21
我们在Default2.aspx页面的Button_Click事件中使用脚本返回一个值给父页面。代码如下:
1 protected void Button1_Click( object sender, EventArgs e)
2 {
3 StringBuilder s = new StringBuilder();
4 s.Append( " <script language=javascript> " + " \n " );
5 s.Append( " window.returnValue=' " + this .GetSelectValue() + " '; " + " \n " );
6 s.Append( " window.close(); " + " \n " );
7 s.Append( " </script> " );
8 Type cstype = this .GetType();
9 ClientScriptManager cs = Page.ClientScript;
10 string csname = " ltype " ;
11 if ( ! cs.IsStartupScriptRegistered(cstype, csname))
12 cs.RegisterStartupScript(cstype, csname, s.ToString());
13
14 }
脚本注册成功之后,我们可以做如下的实验:
1)打开Default1.aspx页面在id为a1的TextBox中输入数字55,然后点击Button
2)在弹窗中输入数字66再点子窗体的按钮关闭子窗体。
3)查看结果
从结果中,我们可以看出我们保留了先输入到父窗体中的值,又接收了从子窗体传递过来的值。
本文转自悬魂博客园博客,原文链接:http://www.cnblogs.com/xuanhun/archive/2010/03/08/1681089.html,如需转载请自行联系原作者