在Asp.net中,从A页面中弹出B页面,在B页面中选择数据后,关闭并将数据更新到A页面,是一种常用 的方式。只是我对Javascript不熟悉,所以捣鼓了一下午,终于有了一点成绩:
测试项目有两个页面:Default.aspx及Default2.aspx,在Default.aspx页面上有一个TextBox1及一个Button1,Button1用于触发Default2.aspx,TextBox1用于接收从子页面传回的值。
Button1的代码如下:

StringBuilder s = new StringBuilder();
s.Append("<script language=javascript>");
s.Append("var a=window.showModalDialog('Default2.aspx');");
s.Append("if(a!=null)");
s.Append("document.all('TextBox1').value=a;");
s.Append("</script>");
Type cstype = this.GetType();
ClientScriptManager cs = Page.ClientScript;
string sname = "lt";
if (!cs.IsStartupScriptRegistered(cstype, sname))
cs.RegisterStartupScript(cstype, sname, s.ToString());

 

Default2.aspx页面内有一个CheckBoxList1及一个Button1,Button1执行返回选择的CheckBoxList1的值,并将当前页面关闭。
代码如下:

 

protected void Button1_Click(object sender, EventArgs e)
{
StringBuilder s = new StringBuilder();
s.Append("<script language=javascript>" + "\n");
s.Append("window.returnValue='" + this.GetSelectValue() + "';" + "\n");
s.Append("window.close();"+"\n");
s.Append("</script>");
Type cstype = this.GetType();
ClientScriptManager cs = Page.ClientScript;
string csname = "ltype";
if (!cs.IsStartupScriptRegistered(cstype, csname))
cs.RegisterStartupScript(cstype, csname, s.ToString());
}
private string GetSelectValue()
{
string rvalue = "";
for (int i = 0; i < this.CheckBoxList1.Items.Count; i++)
{
if (this.CheckBoxList1.Items[i].Selected)
{
if (rvalue == "")
rvalue += this.CheckBoxList1.Items[i].Text;
else
rvalue += "," + this.CheckBoxList1.Items[i].Text;
}
}
return rvalue;
}

 

此时执行程序,在Default2.aspx中不会关闭且不能传回值,很重要的一点:
在head中,加入这一行:



<base target="_self" />

 

asp.net window.showModalDialog 后台用法(兼容谷歌浏览器)

 

  1.  StringBuilder s = new StringBuilder();  
  2.             s.Append("<script language=javascript>");  
  3.             s.Append("var a=window.showModalDialog('selectnh.aspx?zhen=" + this.sxdw1.zj + "&cun=" + this.sxdw1.cj + "&zu=" + this.sxdw1.zu + "');");  
  4.             s.Append(@"//for chrome   
  5. if(typeof(a) == 'undefined') {   
  6. a = window.returnValue;   
  7. }");  
  8.             s.Append("document.getElementById('txtcbfdb').value=a[0];");  
  9.             s.Append("document.getElementById('hdnhid').value=a[1];");  
  10.             s.Append("</script>");  
  11.             Type cstype = this.GetType();  
  12.             ClientScriptManager cs =this.ClientScript;  
  13.             string sname = "lt";  
  14.             if (!cs.IsStartupScriptRegistered(cstype, sname))  
  15.                 cs.RegisterStartupScript(cstype, sname, s.ToString());   


父页面

 

按钮事件代码

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. StringBuilder s = new StringBuilder();  
  2.             s.Append("<script language=javascript>" + "\n");  
  3.             s.Append("var result=new Array;  ");  
  4.             s.Append("result[0]="+this.ddlnh.SelectedItem.Text+";");  
  5.             s.Append("result[1]="+this.ddlnh.SelectedValue+";");  
  6.             s.Append(@"//for chrome   
  7. if(window.opener != undefined) {window.opener.returnValue = result;window.opener.close();} else{");  
  8.             s.Append("window.returnValue=result;}" + "\n");  
  9.              
  10.             s.Append("window.close();" + "\n");  
  11.             s.Append("</script>");  
  12.             Type cstype = this.GetType();  
  13.             ClientScriptManager cs = this.ClientScript;  
  14.             string csname = "ltype";  
  15.             if (!cs.IsStartupScriptRegistered(cstype, csname))  
  16.                 cs.RegisterStartupScript(cstype, csname, s.ToString());  

子页面 按钮事件代码

下面的传多个值的时候记得加‘’号

 

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1.  StringBuilder s = new StringBuilder();  
  2.                 s.Append("<script language=javascript>" + "\n");  
  3.                 s.Append("var result=new Array(10);  ");  
  4.                 if (!string.IsNullOrEmpty(model.YWCBHT))  
  5.                 {  
  6.                     s.Append("result[0]=" + model.CBHTBM.Trim() + ";");//合同编码  
  7.                 }  
  8.                 else  
  9.                 {  
  10.                     s.Append("result[0]=" + model.JYQZBM.Trim() + ";");//经营权证编码  
  11.                 }  
  12.                 NEWLAND.BLL.T_dictionary zdbll = new NEWLAND.BLL.T_dictionary();  
  13.   
  14.                 s.Append("result[1]='" + zdbll.Getzd("承包经营权取得方式", model.CBFS).Trim() + "';");//承包方式  
  15.                 string qzsj = model.CBKSRQ + "至" + model.CBJSRQ;  
  16.                 s.Append("result[2]='" + qzsj + "';");  
  17.                 s.Append("result[3]='" + model.CBFMC.Trim() + "';");  
  18.                 s.Append("result[4]=" + model.LXDH.Trim() + ";");  
  19.                 s.Append("result[5]='" + zdbll.Getzd("证件类型", model.CBFZJLX).Trim() + "';");  
  20.                 s.Append("result[6]='" + model.CBFZJHM.Trim() + "';");  
  21.                 s.Append("result[7]='" + model.CBFDZ.Trim() + "';");  
  22.                 s.Append("result[8]='" + model.YZBM.Trim() + "';");  
  23.                 NEWLAND.BLL.DK dkbll = new NEWLAND.BLL.DK();  
  24.                 DataTable dkbt = dkbll.GetList("cbfdbid='"+this.ddlnh.SelectedValue+"'").Tables[0];  
  25.                 if (dkbt.Rows.Count > 0)  
  26.                 {  
  27.                     string strs = "共"+dkbt.Rows.Count+"块";  
  28.                     decimal htmj = 0;  
  29.                     for (int t = 0; t < dkbt.Rows.Count; t++)  
  30.                     {  
  31.                         try  
  32.                         {  
  33.                             htmj += decimal.Parse(dkbt.Rows[t]["htmj"].ToString());  
  34.                         }  
  35.                         catch { }  
  36.                     }  
  37.                     strs +=htmj+ "亩";  
  38.                     s.Append("result[9]='" + strs + "';");  
  39.                 }  
  40.                 else  
  41.                 {  
  42.                     string strs = "共0块0亩";  
  43.                     s.Append("result[9]='" + strs + "';");  
  44.                 }  
  45.   
  46.                 s.Append(@"//for chrome   
  47. if(window.opener != undefined) {window.opener.returnValue = result;window.opener.close();} else{");  
  48.                 s.Append("window.returnValue=result;}" + "\n");  
  49.   
  50.                 s.Append("window.close();" + "\n");  
  51.                 s.Append("</script>");  
  52.                 Type cstype = this.GetType();  
  53.                 ClientScriptManager cs = this.ClientScript;  
  54.                 string csname = "ltype";  
  55.                 if (!cs.IsStartupScriptRegistered(cstype, csname))  
  56.                     cs.RegisterStartupScript(cstype, csname, s.ToString());  



 

子页面一定记得在head中加入   <base target="_self" />

 

posted on 2014-09-30 10:26  kingreatwill  阅读(985)  评论(0编辑  收藏  举报