页面间的传值方式主要有2种,一种是通过脚本进行赋值:parent.document.getElementById('id').value='xxx',这种方式主要的缺点是各页面ID值不同,不能做到统一处理,第二种方式是通过ModalDialog,弹出的页面只负责将值返回过来,具体的赋值操作在母页面中进行处理.以下就是第二种方式的例子.
步骤一:
用showModalDialog方法,在本页面取得值,再赋给控件!
//给参照按钮添加属性
this.ibnCode.Attributes.Add("onclick", "return Pop('" + this.txtGoodCode.ClientID + "','" + this.txtDetailCode.ClientID + "','0','" + strCompanyCode + "')");
//取得返回值,并赋给本页面
function Pop(GoodCode,DetailCode,GoodIsFree,CompanyCode)
{
var returnValue= window.showModalDialog("http://www.cnblogs.com /Pop/GoodsSelect.aspx?CompanyCode="+CompanyCode+"& GoodIsFree="+GoodIsFree,"商品代码列表","height=400,width=600, top=300,left=400,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no, status=no");
if (returnValue != null )
{
var valueArray = new Array();
valueArray = returnValue.split('%2C');
document.getElementById(GoodCode).value =valueArray[0];
document.getElementById(DetailCode).value =valueArray[1];
}
return false;
}
//给参照按钮添加属性
this.ibnCode.Attributes.Add("onclick", "return Pop('" + this.txtGoodCode.ClientID + "','" + this.txtDetailCode.ClientID + "','0','" + strCompanyCode + "')");
//取得返回值,并赋给本页面
function Pop(GoodCode,DetailCode,GoodIsFree,CompanyCode)
{
var returnValue= window.showModalDialog("http://www.cnblogs.com /Pop/GoodsSelect.aspx?CompanyCode="+CompanyCode+"& GoodIsFree="+GoodIsFree,"商品代码列表","height=400,width=600, top=300,left=400,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no, status=no");
if (returnValue != null )
{
var valueArray = new Array();
valueArray = returnValue.split('%2C');
document.getElementById(GoodCode).value =valueArray[0];
document.getElementById(DetailCode).value =valueArray[1];
}
return false;
}
步骤二:通过window.parent.returnValue返回值.
在弹出窗口页面,注册脚本事件,返回所选值![这里也可以在页面中实现,即通过脚本对window.parent.returnValue的赋值,而不必通过后台]
通过window.parent.returnValue返回值.
protected void btnChoose_Click(object sender, EventArgs e)
{
//将本页面选中值返回之宿主页面
try
{
string strSelectedValue = hdfTransInfo.Value;
string strScript = "<script language='javascript'>window.parent.returnValue= escape('" + strSelectedValue + "');window.parent.close();</script>";
if (!ClientScript.IsClientScriptBlockRegistered("clientScript"))
{
ClientScript.RegisterClientScriptBlock(System.Type.GetType("System.String"), "clientScript", strScript);
}
}
catch (Exception ex)
{
}
}
protected void btnChoose_Click(object sender, EventArgs e)
{
//将本页面选中值返回之宿主页面
try
{
string strSelectedValue = hdfTransInfo.Value;
string strScript = "<script language='javascript'>window.parent.returnValue= escape('" + strSelectedValue + "');window.parent.close();</script>";
if (!ClientScript.IsClientScriptBlockRegistered("clientScript"))
{
ClientScript.RegisterClientScriptBlock(System.Type.GetType("System.String"), "clientScript", strScript);
}
}
catch (Exception ex)
{
}
}