Friday, 6 May 2011

Open PopUp Window With Value, Update the Value and Refresh Parent Values From Child ASP.NET

//parent page source file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ParentUpdatePage.aspx.cs" Inherits="ParentUpdatePage" %>

<!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 popUrl = 'PopUp.aspx?fn=' + document.getElementById('<%= lblFirstName.ClientID %>').innerHTML + '&ln=' + document.getElementById('<%= lblLastName.ClientID %>').innerHTML;
var name = 'popUp';
var appearence ='dependent=yes,menubar=no,resizable=no,'+
'status=no,toolbar=no,titlebar=no,' +
'left=5,top=280,width=230px,height=140px';
var openWindow = window.open(popUrl, name, appearence);
openWindow.focus();
}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    First Name :
<asp:Label ID="lblFirstName" runat="server" Text="amiT">
</asp:Label><br />
 <br />
Last Name:&nbsp;
<asp:Label ID="lblLastName" runat="server" Text="jaiN">
</asp:Label><br />
<br />
<asp:Button ID="btnPop" runat="server" Text="Click To Edit Values" />
    </div>
    </form>
</body>
</html>


//parent page code file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ParentUpdatePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string updateValuesScript =
        @"function updateValues(popupValues)
        {
            document.getElementById('lblFirstName').innerHTML=popupValues[0];
            document.getElementById('lblLastName').innerHTML=popupValues[1];
        }";
        this.ClientScript.RegisterStartupScript(Page.GetType(),
        "UpdateValues", updateValuesScript.ToString(), true);
         btnPop.Attributes.Add("onclick", "openPopUp('PopUp.aspx')");
    }
}
//child page source file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PopUp.aspx.cs" Inherits="PopUp" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    First Name :
<asp:TextBox ID="txtPopFName" runat="server" Width="113px">
</asp:TextBox><br />
<br />
Last Name:<asp:TextBox ID="txtPopLName" runat="server"
                       Width="109px">
</asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" 
            Text="Button" />
    </div>
    </form>
</body>
</html>
//child page code file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class PopUp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string updateParentScript =
        @"function updateParentWindow()
        {                                                                             
            var fName=document.getElementById('txtPopFName').value;   
            var lName=document.getElementById('txtPopLName').value; 
            var arrayValues= new Array(fName,lName);
            window.opener.updateValues(arrayValues);     
            window.close();
        }";
        this.ClientScript.RegisterStartupScript(this.GetType(),
        "UpdateParentWindow", updateParentScript, true);
        if (!IsPostBack)
        {
            txtPopFName.Text = Request["fn"];
            txtPopLName.Text = Request["ln"];
        }
       Button1.Attributes.Add("onclick", "updateParentWindow()");
    }
}












No comments:

Post a Comment