This example explains how to Open PopUp Window In Asp.Net And Update Refresh Parent Child Values using ClientScript .
I am opening popup window from aspx page and updating values in parent window from child or popup window using javascript
I have added to labels in Home.aspx page and one button to open popup window.
I've also added a PopupWindow.aspx page which is having two textboxes and a button to update lable values of parent page.
The textboxes in popup window are populated with Text values of lables in parent page (Home.aspx), after making changes in textbox values i'm updating values back in parent page.
HTML source Parent page (Home.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="JavaScriptDemo.Home" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function openPopUp() {
var firstName = document.getElementById("txtFirstName").value;
var lastName = document.getElementById("txtLastName").value;
var url = "popupWindow.aspx?firstname=" + firstName + "&lastname=" + lastName;
window.open(url, "popup", "width=400,height=200,left=300,top=150");
}
function updateValues(arr) {
document.getElementById("txtFirstName").value = arr[0];
document.getElementById("txtLastName").value = arr[1];
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblFirstName" runat="server" Text="First Name"></asp:Label>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="lblLastName" runat="server" Text="Last Name"></asp:Label>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnModify" runat="server" Text="Modify" OnClientClick="openPopUp()" />
</div>
</form>
</body>
</html>
HTML SOURCE OF PopupWindow.aspx(child) page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="popupWindow.aspx.cs" Inherits="JavaScriptDemo.popupWindow" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function updateParentAndClose() {
var arr = new Array();
arr.push(document.getElementById("txtFirstName").value);
arr.push(document.getElementById("txtLastName").value);
window.opener.updateValues(arr);
window.close();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblFirstName" runat="server" Text="First Name"></asp:Label>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="lblLastName" runat="server" Text="Last Name"></asp:Label>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnModify" runat="server" Text="Modify" OnClientClick="updateParentAndClose()" />
</div>
</form>
</body>
</html>
SOURCE OF PopupWindow.aspx,cs (child) page
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["firstname"] != null)
{
txtFirstName.Text = Request.QueryString["firstname"];
txtLastName.Text = Request.QueryString["lastname"];
}
}
No comments:
Post a Comment