Effect: For example, in an equipment management system, the equipment number needs to be filled in somewhere, but the equipment number is usually difficult to remember, and all that may be remembered is the equipment in which department and location. Therefore, we want to add a button next to the text box. After clicking, a sub-page will pop up. There is a table comparing the device number and various details of the device. I only need to find the device based on the location, double-click this record, and the device number will be filled in. Went up.
Implementation process:
The javascript function to open a new window on the parent page is:
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>Call in button:
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return openpage('child.aspx');"/>
The subpage binds the data source of the gridview and writes the following code in its RowDataBound event:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string s = "window.opener.document.getElementById('textbox1').value='" + e.Row.Cells[1].Text + "'; window.close();";
if (e.Row.RowType != DataControlRowType.Header)
{
e.Row.Attributes.Add("ondblclick", s);//Double-click selection
//When the mouse moves over, set the color of the line to "" and save the original background color
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';");
//Restore the background color of the row when the mouse is moved away
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
}
}Explanation: Open a new page through window.open. There is a parent-child relationship between the two pages. The child page can access the parent page (controls and js functions written on the parent page) through opener, and the parent page can also access the child page through sub. For example, if there is a js function sayhello() in the parent page, you only need opener.sayhello() to call it in the child page.
Using only a small amount of Javascript code and combining it with asp.net, a very useful effect is achieved.