單擊父窗口中的GridView彈出一個模態對話框,提交數據后,根據提交的數據更新父窗口頁面的實現

本文介绍了一个ASP.NET Web应用程序实例,展示了如何通过双击事件打开子窗口选择用户,并在选择后刷新父窗口的数据视图。具体涉及使用JavaScript进行页面间的参数传递,以及C#后端代码实现数据查询与绑定。

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

父窗體UserComputerLink.aspx

HTML代碼如下

----------------------------------------------------------------------------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserComputerLink.aspx.cs"
    Inherits
="ComputerInfo_UserComputerLink" 
%>

<!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>
    
<link href="../Css/Css.css" rel="Stylesheet" type="text/css" />
    
<script language="javascript" type="text/javascript">        
        
function DbClickEvent(EquipmentID)
        
{
              
var st=window.showModalDialog("DisplayLis.aspx?EquipmentID="+EquipmentID);
              
//方法一:子窗口刷新父頁面時的父頁面的調用的窗口
             //window.self.location="UserComputerLink.aspx?val="+st;
             //方法二:子窗口刷新父頁面的父頁面的調用窗口,重載本頁面
           if(st==1)
            
{
                window.location.reload();
            }

        }

//        function ClickEvent(EquipmentID)
//
        {
//
              window.alert("事件类型: OnClick  作用对象: " + EquipmentID);            
//
        }
//
        function GridViewItemKeyDownEvent(EquipmentID)
//
        {
//
              window.alert("事件类型: GridViewItemKeyDownEvent  作用对象: " + EquipmentID);       
//
        }
//
        function KeyDownEvent()
//
        {
//
               if( event.altKey && event.keyCode > 48 && event.keyCode < 54 )            
//
               {                
//
                      window.alert("事件类型: FormKeyDownEvent  选中记录数: " + ( parseInt(event.keyCode) - 48 )); 
//
               }                      
//
        }           
    
</script>

</head>
<body>
    
<form id="form1" runat="server">
        
<div>
            
<div id="div1">
                
<table id="table1" border="0" cellpadding="0" cellspacing="0" width="100%" class="td">
                    
<tr>
                        
<td style="width: 10%; text-align: center">
                            
<asp:ImageButton ID="ImgBtn" runat="server" ImageUrl="~/Image/add.gif" /></td>
                        
<td style="width: 10%; text-align: center">
                            
<asp:ImageButton ID="SaveBtn" runat="server" ImageUrl="~/Image/Saves.gif" /></td>
                        
<td style="width: 10%; text-align: center">
                            
<asp:ImageButton ID="ClearBtn" runat="server" ImageUrl="~/Image/Clear.gif" /></td>
                        
<td style="width: 10%; text-align: center">
                        
</td>
                        
<td style="width: 10%; text-align: center">
                        
</td>
                        
<td style="width: 10%; text-align: center">
                        
</td>
                        
<td style="width: 10%">
                        
</td>
                        
<td style="width: 10%">
                        
</td>
                        
<td style="width: 10%">
                        
</td>
                        
<td style="width: 10%">
                        
</td>
                    
</tr>
                
</table>
            
</div>
            
<asp:GridView ID="GridPCInfo" runat="server" BackColor="White" BorderColor="#CC9966"
                BorderStyle
="None" BorderWidth="1px" CellPadding="4" Width="100%" AutoGenerateColumns="False" OnRowDataBound="GridPCInfo_RowDataBound">
                
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                
<RowStyle BackColor="White" ForeColor="#330099" />
                
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                
<Columns>
                    
<asp:BoundField DataField="Equipmentid" HeaderText="AutoNum" />
                    
<asp:BoundField DataField="MAC" HeaderText="MAC地址" />
                    
<asp:BoundField DataField="IP" HeaderText="IP地址" />
                    
<asp:BoundField DataField="compuname" HeaderText="電腦名稱" />
                    
<asp:BoundField DataField="TradeMark" HeaderText="電腦品牌" />
                    
<asp:BoundField DataField="CPU" HeaderText="CPU" />
                    
<asp:BoundField DataField="RAMSize" HeaderText="內存大小" />
                    
<asp:BoundField DataField="HDSize" HeaderText="硬盤大小" />
                    
<asp:BoundField DataField="GraphCardSize" HeaderText="顯卡大小" />
                    
<asp:BoundField DataField="OS" HeaderText="OS" />
                    
<asp:BoundField DataField="Loacl" HeaderText="所在位置" />
                    
<asp:BoundField DataField="ManageLabel" HeaderText="管制標籤" />
                
</Columns>
            
</asp:GridView>
            
            
<asp:GridView ID="GridLink" runat="server" BackColor="White" BorderColor="#CC9966"
                BorderStyle
="None" BorderWidth="1px" CellPadding="4" Width="100%" AutoGenerateColumns="False">
                
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                
<RowStyle BackColor="White" ForeColor="#330099" />
                
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                
<Columns>
                    
<asp:BoundField DataField="Equipmentid" HeaderText="設備ID" />
                    
<asp:BoundField DataField="IP" HeaderText="IP地址" />
                    
<asp:BoundField DataField="mac" HeaderText="MAC地址" />
                    
<asp:BoundField DataField="computername" HeaderText="電腦名稱" />
                    
<asp:BoundField DataField="userid" HeaderText="用戶ID" />
                    
<asp:BoundField DataField="username" HeaderText="用戶名" />
                
</Columns>
            
</asp:GridView>
        
</div>
    
</form>
</body>
</html>

後臺程序如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class ComputerInfo_UserComputerLink : System.Web.UI.Page
{
    
//將類實例化
    ITInfoClass GetDB = new ITInfoClass();
    UserPCLinkClass GetDBData 
= new UserPCLinkClass();
    
//定義一個字符串對象
    StringBuilder SQL = new StringBuilder();
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            BindData();
            BindPCUserData();
            
//驗證子頁面的傳回的參數是否為空,若不為空則執行以下相關代碼
            
//單獨的子窗口關閉刷新父窗體用不著此程式。
            
//if (Request.QueryString["val"] != null)
            
//{
            
//    BindData();
            
//    BindPCUserData();
            
//}
        }

    }

    
/// <summary>
    
/// 查詢數據庫中的數據綁定到GridView
    
/// </summary>

    protected void BindData()
    
{
        SQL.Remove(
0, SQL.Length);
        SQL.Append(
"SELECT");
        SQL.Append(
"    * ");
        SQL.Append(
"    FROM");
        SQL.Append(
"    COMPUTERINFO");
        SQL.Append(
"    ORDER BY");
        SQL.Append(
"    EQUIPMENTID DESC");
        GridPCInfo.DataSource 
= GetDB.GetITInfo(SQL.ToString());
        GridPCInfo.DataBind();  
    }


    
/// <summary>
    
/// 查詢數據庫中的PCUser表中數據并返回給GridLink
    
/// </summary>

    public void BindPCUserData()
    
{
        GridLink.DataSource 
= GetDBData.GetUserPC("PS_GETUSERPC""DATASETS") ;
         GridLink.DataBind();
       
    }

    
/// <summary>
    
/// GridPCInfo 的雙擊事件
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>

    protected void GridPCInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    
{
        GetDB.ChangeRowColor(sender, e, GridPCInfo);
        
if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            e.Row.Attributes.Add(
"OnDblClick""DbClickEvent('" + e.Row.Cells[0].Text + "')");
            e.Row.Attributes[
"style"= "Cursor:hand";

            
//e.Row.Attributes.Add("OnClick"OnDbClick, "ClickEvent('" + e.Row.Cells[1].Text + "')");

            
//e.Row.Attributes.Add("OnKeyDown", "GridViewItemKeyDownEvent('" + e.Row.Cells[1].Text + "')");

            
//设置悬浮鼠标指针形状为"小手"

        }

    }

}

子窗體頁面 DisplayList.aspx

HTML代碼乳如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DisplayLis.aspx.cs" Inherits="ComputerInfo_DisplayLis" %>

<!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>
    
<link href="../Css/Css.css" type="text/css" rel="Stylesheet" />
    
<base target="_self" />
</head>
<body>
    
<form id="form1" runat="server">
        
<div>
            
<asp:GridView ID="GridUser" runat="server" BackColor="White" BorderColor="#CC9966"
                BorderStyle
="None" BorderWidth="1px" CellPadding="4" Width="100%" AutoGenerateColumns="False">
                
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                
<RowStyle BackColor="White" ForeColor="#330099" />
                
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                
<Columns>
                    
<asp:TemplateField HeaderText="選擇">
                        
<ItemTemplate>
                            
<asp:CheckBox ID="Select1" runat="server" />
                        
</ItemTemplate>
                    
</asp:TemplateField>
                    
<asp:BoundField DataField="userid" HeaderText="用戶ID" />
                    
<asp:BoundField DataField="username" HeaderText="用戶名" />
                    
<asp:BoundField DataField="sex" HeaderText="性別" />
                    
<asp:BoundField DataField="deptid" HeaderText="部門" />
                    
<asp:BoundField DataField="phone" HeaderText="電話" />
                
</Columns>
            
</asp:GridView>
            
<table cellpadding="0" cellspacing="0" width="100%" border="0">
                
<tr align="center">
                    
<td style="height: 24px; width:50%;">
                        
<asp:Button ID="ConfrimBtn"
                            runat
="server" Text="確定" OnClick="ConfrimBtn_Click" />
                        
</td> 
                    
<td style="height: 24px; width:50%;">
                    
<asp:Button ID="CancelBtn" runat="server" Text="取消" OnClick="CancelBtn_Click" />
                    
                    
</td>
                
</tr>
                
<tr>
                
<td colspan="2">
                 
<asp:HiddenField ID="HfEquipmentid" runat="server" />
                    
<asp:HiddenField ID="HfUserID" runat="server" />
                
</td>
                
</tr>
            
</table>
           
        
</div>
        
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></form>
</body>
</html>

後臺代碼如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ComputerInfo_DisplayLis : System.Web.UI.Page
{
    UserPCLinkClass GetUs 
= new UserPCLinkClass();
    
protected void Page_Load(object sender, EventArgs e)
    
{
        Response.Expires 
= -1;
        
if (!IsPostBack)
        
{
            
if (Request.QueryString["EquipmentID"!= null)
            
{
                
string Equipmentid = Request.QueryString["EquipmentID"].ToString().Replace("null""");
                HfEquipmentid.Value 
= Equipmentid.ToString();
            }

            UserDataBind(); 
        }

    }

    
/// <summary>
    
/// 獲取用戶并綁定到GridView
    
/// </summary>

    public void UserDataBind()
    
{
        DataSet ds 
= new DataSet();
        ds 
= GetUs.GetUser();
        GridUser.DataSource 
= ds;
        GridUser.DataBind();
    }

    
/// <summary>
    
/// 取消按鈕事件,關閉當前的模態頁面
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>

    protected void CancelBtn_Click(object sender, EventArgs e)
    
{
        
this.RegisterStartupScript("key""<script>window.opener=null;window.close(this);</script>");
    }

    
/// <summary>
    
/// 點擊“確定”按鈕,執行設備ID與人員的綁定
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>

    protected void ConfrimBtn_Click(object sender, EventArgs e)
    
{
        CheckBox FalseCheck 
= new CheckBox();
        
if (GetCheckValue().Value != null)
        
{
            
string[] stringUserID = GetCheckValue().Value.Split('|');
            
int IDCount = stringUserID.Length;
            
for (int j = 0; j < IDCount; j++)
            
{
                
//將Equipmentid與用戶關聯的數據插入到數據庫中
                GetUs.InsertCompuUser(HfEquipmentid.Value, stringUserID[j].ToString());
            }

            
//取消Grid中被選中的checkBox
            for (int i = 0; i < GridUser.Rows.Count; i++)
            
{
                FalseCheck 
= (CheckBox)GridUser.Rows[i].FindControl("Select1");
                
if (FalseCheck.Checked == true)
                
{
                    FalseCheck.Checked 
= false;
                }

            }

            
//方法一、返回父頁面值1,然後父頁面判斷若是1,則刷新也頁面,若不是則不做刷新
            this.ClientScript.RegisterStartupScript(this.GetType(), "Key""<script language='javascript'>window.returnValue=1;window.close(this);</script>");
            
//方法二、返回父頁面本一個參數值,供父頁面取得此參數,由此參數的值再調用其他程式,且刷新父頁面
            
//this.ClientScript.RegisterStartupScript(this.GetType(), "Key", "<script language='javascript'>window.returnValue='"+HfEquipmentid.Value+"';window.close(this);</script>");
        }

        
else
        
{
            
this.RegisterStartupScript("key""<script>window.opener=null;window.close(this);</script>");
        }

        }

        
    
/// <summary>
    
/// 獲取頁面的所有的checkBox被選中的用戶ID號碼
    
/// </summary>
    
/// <returns></returns>

    public HiddenField GetCheckValue()
    
{
        HfUserID.Value 
= null;
        GridView MyGrid 
= new GridView();
        
//獲取頁面的GridView控件
        MyGrid = (GridView)this.Form.FindControl("GridUser");
        
//MyGrid = (GridView)Parent.FindControl("GridUser");
        if (MyGrid != null)
        
{
            
int i, Row;
            Row 
= MyGrid.Rows.Count;
            CheckBox MyCheckBox 
= new CheckBox();
            
for (i = 0; i < Row; i++)
            
{
                MyCheckBox 
= (CheckBox)MyGrid.Rows[i].FindControl("Select1");
                
if (MyCheckBox != null)
                
{
                    
if (MyCheckBox.Checked)
                    
{
                        
string Tedt = MyGrid.Rows[i].Cells[1].Text.ToString();
                        HfUserID.Value 
= HfUserID.Value + MyGrid.Rows[i].Cells[1].Text.ToString() + "|";
                    }

                }

            }

        }

        
//if (HfUserID.Value.Length > 0)
        
//{
        
//    HfUserID.Value = HfUserID.Value.Remove(HfUserID.Value.Length - 1);
        
//}
        return HfUserID;
    }

   
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值