实现过程:
父页面
打开新窗口的javascript函数为:
<script type="text/javascript" language="javascript">
function openpage(htmlurl)
{
var newwin=window.open(htmlurl,"newWin","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=yes,top=100,left=200,width=650,height=300");
newwin.focus();
return false;
}
</script>
function openpage(htmlurl)
{
var newwin=window.open(htmlurl,"newWin","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=yes,top=100,left=200,width=650,height=300");
newwin.focus();
return false;
}
</script>
在按钮中调用:
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return openpage('child.aspx');"/>
子页面绑定gridview的数据源,并在它的RowDataBound事件里写代码如下:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string s = "window.opener.document.getElementById('txbRoleName').value='" + e.Row.Cells[1].Text + "'; "+
"window.opener.document.getElementById('txbRoleID').value='" + e.Row.Cells[0].Text + "'; "
+"window.close();";
if (e.Row.RowType != DataControlRowType.Header)
{
e.Row.Attributes.Add("ondblclick", s);//双击选择
//当鼠标移到的时候设置该行颜色为"", 并保存原来的背景颜色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';");
//当鼠标移走时还原该行的背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
}
}
说明:通过window.open打开新页面,两个页面之前便有了一种父子关系。子页通过opener可以访问父页(控件及写在父页的js函数),父页同 样通过sub可以访问子页。如在父页有个js函数sayhello(),在子页中只需要opener.sayhello()便可以调用了。{
string s = "window.opener.document.getElementById('txbRoleName').value='" + e.Row.Cells[1].Text + "'; "+
"window.opener.document.getElementById('txbRoleID').value='" + e.Row.Cells[0].Text + "'; "
+"window.close();";
if (e.Row.RowType != DataControlRowType.Header)
{
e.Row.Attributes.Add("ondblclick", s);//双击选择
//当鼠标移到的时候设置该行颜色为"", 并保存原来的背景颜色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';");
//当鼠标移走时还原该行的背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
}
}
只使用很少的Javascript代码与asp.net结合,便完成了一种很好用的效果。
转自:http://www.cnblogs.com/jeffamy/archive/2006/05/11/396880.html