|
Open PopUp Window Update Refresh Parent Values From Child ASP.NET |
|
In this example we are going to describe how to open popup window from aspx page with values from parent page, and update or refresh values in parent window from child or popup window using javascript and ClientScript.RegisterStartupScript method in ASP.NET
|
|
we have added to labels in Default.aspx page and one button to open popup window.we have also added a PopUp.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 (Default.aspx), after making changes in textbox values i'm updating values back in parent page. |
|
HTML source of Default.aspx (parent) page is
|
|
|
|
<form id="form1" runat="server">
<div>
First Name :
<asp:Label ID="lblFirstName" runat="server" Text="amiT">
</asp:Label><br />
<br />
Last Name:
<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>
|
write this code Page_Load event of Default.aspx (parent) page
C# code behind
|
|
|
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')");
}
|
|
And this is the HTML code for PopUp.aspx(child) page
|
|
<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" OnClick="Button1_Click"
Text="Button" /></div>
</form>
|
Code behind for PopUp.aspx
C# code behind |
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()");
}
|
|
|