在asp.net中关闭子窗体并刷新父窗体数据
要实现的是点击A页面的某个按钮弹出一个模式对话框B,显示从A里取出的某条数据,在模式对话框里做完修改后点击 B里的修改按钮的同时关闭B,重新回到A并刷新页面 A中用GridView显示数据
A.aspx
protected void BlackListGView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row .DataItem!=null)
{
Random rd = new Random();
int suijiNum = rd.Next(1, 2000);
string ID = (e.Row.Cells[1].Controls[0].FindControl("lblID") as Label).Text;
string updateUrl = string.Format("javascript:window.open('B.aspx?ID={0}&sj={1}', '', 'height=400, width=520,top=240,left=100,toolbar=no, menubar=no, scrollbars=no, resizable=no');", ID, suijiNum);
string addUrl = string.Format("javascript:window.open('B.aspx?sj={0}','','height=400, width=520,top=240,left=100,toolbar=no, menubar=no, scrollbars=no, resizable=no');", suijiNum);
((Button)e.Row.Cells[6].Controls[0].FindControl("Btn_Update")).Attributes.Add("onclick",updateUrl);
((Button)e.Row.Cells[6].Controls[0].FindControl("Btn_Add")).Attributes.Add("onclick", addUrl);
}
}
URL后面带上一个随机数是为了防止IE缓存
以上代码是在GridView的RowDataBound事件里主要是弹出模式对话框,并传参,最后是B中做修改,在B的Update_Click事件里加上:
protected void Btn_Update_Click(object sender, EventArgs e)
{
if (_ID != "" && txtPhone.Text != "")
{
bool flag = objPDAL.IsUpdateBlackList(_ID, txtPhone.Text);
if (flag)
{
Response.Write("<Script>window.opener.opener= null;window.opener.location.replace(window.opener.location.href);window.close();</Script>");
}
else
ClientScript.RegisterStartupScript(Page.GetType(), "onClick", "<script>alert(更新失败!);</script>");
}
}
对数据进行修改的代码就不用写了,关键是关闭B的一段JS代码很重要:
Response.Write("<Script>window.opener.opener= null;window.opener.location.replace(window.opener.location.href);window.close();</Script>");
以上就是要实现功能的关键代码