asp.net 父窗体获取子窗体的返回值,对父窗体局部更新(模式化窗口)

来源:互联网 发布:网络经纪人 编辑:程序博客网 时间:2024/05/22 14:23

从图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

 

 

其中  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     }

原创粉丝点击