In this example i am 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
I have added to labels in Default.aspx page and one button to open popup window.
I've 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
And this is the HTML code for PopUp.aspx(child) page
Hope this helps
Download sample code attached
Labels:
ASP.Net
,
Web Programming
I have added to labels in Default.aspx page and one button to open popup window.
I've 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 Javascript in Head section of Default.aspx page
In this i m getting values of lables and passing them to popuup page as querystrings
write this code Page_Load event of Default.aspx (parent) page
C# code behind
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')");
}
VB.NET code behind
Protected Sub Page_Load(ByVal
sender As Object,
ByVal e As
EventArgs)
Dim updateValuesScript As
String = "function
updateValues(popupValues)" & vbCr & vbLf & "{" & vbCr & vbLf & "
document.getElementById('lblFirstName').innerHTML=popupValues[0];"
& vbCr & vbLf & "
document.getElementById('lblLastName').innerHTML=popupValues[1];"
& vbCr & vbLf & "}"
Me.ClientScript.RegisterStartupScript(Page.[GetType](),
"UpdateValues",
updateValuesScript.ToString(), True)
btnPop.Attributes.Add("onclick",
"openPopUp('PopUp.aspx')")
End Sub
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
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()");
}
VB.NET code behind
Protected Sub Page_Load(ByVal
sender As Object,
ByVal e As
EventArgs)
Dim updateParentScript As
String = "function
updateParentWindow()" & vbCr & vbLf & " {
" & vbCr & vbLf & " var fName=document.getElementById('txtPopFName').value; " & vbCr & vbLf & " var
lName=document.getElementById('txtPopLName').value; " & vbCr & vbLf & " var
arrayValues= new Array(fName,lName);" & vbCr & vbLf & "
window.opener.updateValues(arrayValues); " & vbCr & vbLf
& "
window.close(); " & vbCr & vbLf & " }"
Me.ClientScript.RegisterStartupScript(Me.[GetType](), "UpdateParentWindow",
updateParentScript, True)
If Not IsPostBack Then
txtPopFName.Text = Request("fn")
txtPopLName.Text = Request("ln")
End If
Button1.Attributes.Add("onclick",
"updateParentWindow()")
End Sub
Hope this helps
Download sample code attached
Thanks
Suneel Kumar
Responses
0 Respones to "Edit Update and Refresh Page values by PopUp ASP.NET"
Post a Comment