asp.net 父窗体获取子窗体的返回值,可用来对父窗体局部更新

http://www.cnblogs.com/xuanhun/archive/2010/03/08/1681089.html

今天在项目上遇到了这个问题,其实只是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)查看结果

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的ASP.NET Web Forms学生管理系统的示例,不需要登录: 1. 创建一个新的ASP.NET Web Forms项目。 2. 在默认的Web窗体"Default.aspx"中添加一个GridView控件,它将显示学生的信息。在GridView的属性中,设置AutoGenerateColumns为"true"。 3. 在页面上添加一个TextBox和一个Button控件,用于添加新的学生信息。在Button的Click事件中,将TextBox中的数据添加到GridView中。 4. 在页面上添加一个DeleteButton控件,用于删除选定的学生信息。在DeleteButton的Click事件中,从GridView中删除选定的行。 5. 编写代码以连接到数据库,并在页面加载时从数据库中加载学生信息,并将其添加到GridView中。 下面是示例代码: Default.aspx: ```asp <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Add Student" OnClick="Button1_Click" /> <asp:Button ID="DeleteButton" runat="server" Text="Delete Selected Student" OnClick="DeleteButton_Click" /> ``` Default.aspx.cs: ```csharp protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { LoadStudents(); } } private void LoadStudents() { string connectionString = "YourConnectionStringHere"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand("SELECT * FROM Students", connection)) { using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { GridView1.DataSource = reader; GridView1.DataBind(); } } } } } protected void Button1_Click(object sender, EventArgs e) { string studentName = TextBox1.Text; string connectionString = "YourConnectionStringHere"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand("INSERT INTO Students (Name) VALUES (@Name)", connection)) { command.Parameters.AddWithValue("@Name", studentName); command.ExecuteNonQuery(); } } LoadStudents(); } protected void DeleteButton_Click(object sender, EventArgs e) { foreach (GridViewRow row in GridView1.Rows) { CheckBox checkBox = (CheckBox)row.FindControl("CheckBox1"); if (checkBox.Checked) { int studentId = Convert.ToInt32(row.Cells[1].Text); string connectionString = "YourConnectionStringHere"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand("DELETE FROM Students WHERE Id = @Id", connection)) { command.Parameters.AddWithValue("@Id", studentId); command.ExecuteNonQuery(); } } } } LoadStudents(); } ``` 这只是一个简单的示例,您可以根据需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值