GridView的父子窗口的值传递(弹出层方式)

该博客介绍了如何在ASP.NET中通过弹出层(使用JavaScript和CSS)实现GridView的父子窗口值传递。用户点击“添加”按钮后,弹出一个包含TextBox的窗口,输入值后点击保存,将数据保存到数据库并刷新GridView展示。示例代码展示了添加事件处理和数据绑定的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4.     <title></title>
  5.     <style>
  6.         .black_overlay
  7.         {
  8.             display: none;
  9.             position: absolute;
  10.             top: 0%;
  11.             left: 0%;
  12.             width: 100%;
  13.             height: 100%;
  14.             background-color: black;
  15.             z-index: 1001;
  16.             -moz-opacity: 0.8;
  17.             opacity: .80;
  18.             filter: alpha(opacity=80);
  19.         }
  20.         .white_content
  21.         {
  22.             display: none;
  23.             position: absolute;
  24.             top: 25%;
  25.             left: 25%;
  26.             width: 50%;
  27.             height: 50%;
  28.             padding: 16px;
  29.             border: 1px solid orange;
  30.             background-color: white;
  31.             z-index: 1002;
  32.             overflow: auto;
  33.         }
  34.     </style>
  35. </head>
  36. <body>
  37.     <form id="form1" runat="server">
  38.     <div>
  39.     </div>
  40.     <asp:GridView ID="GridView1" runat="server" Height="101px" Width="257px" CellPadding="4"
  41.         ForeColor="#333333" GridLines="None">
  42.         <RowStyle BackColor="#EFF3FB" />
  43.         <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  44.         <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  45.         <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
  46.         <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  47.         <EditRowStyle BackColor="#2461BF" />
  48.         <AlternatingRowStyle BackColor="White" />
  49.     </asp:GridView>
  50.     <input id="Button3" type="button" value="添加" onclick="document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'" />
  51.     
  52.     <div id="light" class="white_content">
  53.         添加记录<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  54.         <br />
  55.         <asp:Button ID="Button2" runat="server" Text="保存" OnClick="Button2_Click" />
  56.         <a href="javascript:void(0)" onclick="document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'">
  57.             关闭</a></div>
  58.     <div id="fade" class="black_overlay">
  59.     </div>
  60.     </form>
  61. </body>
  62. </html>
    1.         protected void Page_Load(object sender, EventArgs e)
    2.         {
    3.             if (!IsPostBack)
    4.                 BindGrid();
    5.         }
    6.         protected void Button2_Click(object sender, EventArgs e)
    7.         {
    8.             if (TextBox1.Text.Trim() != "")
    9.             {
    10.                 string sql = string.Format("insert into table_2(name) values('{0}')", TextBox1.Text.Trim());//添加记录
    11.                 SqlHelper.ExecuteNonQuery(SqlHelper.conn, CommandType.Text, sql);
    12.                 BindGrid();
    13.             }
    14.         }
    15.         //绑定
    16.         private void BindGrid()
    17.         {
    18.             string sql = "select name from table_2";
    19.             DataSet ds = SqlHelper.ExecuteDataset(SqlHelper.conn, CommandType.Text, sql);//返回数据
    20.             GridView1.DataSource = ds;
    21.             GridView1.DataBind();
    22.         }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值