Friday 17 February 2012

ASP.NET Open PopUp Window Update Refresh Values


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
<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>
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
01protected void Page_Load(object sender, EventArgs e)
02{
03 string updateValuesScript =
04@"function updateValues(popupValues)
05{
06 document.getElementById('lblFirstName').innerHTML=popupValues[0];
07 document.getElementById('lblLastName').innerHTML=popupValues[1];
08}";
09 
10this.ClientScript.RegisterStartupScript(Page.GetType(),
11"UpdateValues", updateValuesScript.ToString(), true);
12btnPop.Attributes.Add("onclick", "openPopUp('PopUp.aspx')");
13}

VB.NET code behind
1Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
2    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 & "}"
3 
4    Me.ClientScript.RegisterStartupScript(Page.[GetType](), "UpdateValues", updateValuesScript.ToString(), True)
5    btnPop.Attributes.Add("onclick", "openPopUp('PopUp.aspx')")
6End 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
01protected void Page_Load(object sender, EventArgs e)
02{
03 string updateParentScript =
04 @"function updateParentWindow()
05 {                                                                              
06   var fName=document.getElementById('txtPopFName').value;    
07   var lName=document.getElementById('txtPopLName').value;  
08   var arrayValues= new Array(fName,lName);
09   window.opener.updateValues(arrayValues);      
10   window.close();
11 }";
12 this.ClientScript.RegisterStartupScript(this.GetType(),
13     "UpdateParentWindow", updateParentScript, true);
14 if (!IsPostBack)
15 {
16   txtPopFName.Text = Request["fn"];
17   txtPopLName.Text = Request["ln"];
18 }
19   Button1.Attributes.Add("onclick", "updateParentWindow()");
20}
VB.NET code behind
1Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
2    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 & " }"
3    Me.ClientScript.RegisterStartupScript(Me.[GetType](), "UpdateParentWindow", updateParentScript, True)
4    If Not IsPostBack Then
5        txtPopFName.Text = Request("fn")
6        txtPopLName.Text = Request("ln")
7    End If
8    Button1.Attributes.Add("onclick", "updateParentWindow()")
9End Sub

2 comments:

  1. where is the Javascrip? I can't find it. Could you please post it.--Thank you!

    ReplyDelete
  2. dun mind share your JavaScript portion?

    ReplyDelete

Total Pageviews