CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Working with Popup windows

By Satheesh babu
Posted On Jun 14,2008
Article Rating:
Be first to rate
this article.
No of Comments: 1
Category: Javascript
Print this article.

Working with Popup windows

 

Whenever we build any web applications it can have requirements to work with popup windows. For example: To pick a date for a date field in an aspx page, we can place a calendar control on a popup so that user can select a date from the calendar instead of typing himself, a simple date picker control. This article will help you understand some of tasks that we more often do with popup windows.

Refreshing a parent page from a popup ASP.Net page

There are situations where we will be required to populate the information’s entered on the popups to the parent page and refresh the parent page so that it can be accessed in the server side. The below example will help us in achieving it.

The below example will populate the entered name in the child.aspx(popup) to the parent(Parent.aspx) and submit the page to the server.

 

Parent.aspx

<script language="javascript">   

    function OpenWindow(strChildPageUrl)

    {

        var testwindow = window.open(strChildPageUrl,"Child","width=400px,height=400px,top=0,left=0,scrollbars=1");

    }

</script>

 

<form id="form1" runat="server"  >

<div>

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>

<input type="button" ID="Button1" value="Enter Name" OnClick="OpenWindow('Child.aspx')" />

</div>

</form>

 

Child.aspx

<script language="javascript">

     function SubmitToParent()

     {    

     window.opener.document.forms[0].txtName.value = document.getElementById('txtChildName').value;    

     window.opener.document.forms[0].submit();

     self.close();

     }

</script>

 

<form id="form1" runat="server” >

<div>

 <asp:TextBox ID="txtChildName" runat="server"></asp:TextBox>

 <input type="button" ID="Button1" value="Submit to Parent" OnClick="SubmitToParent()" />

</div>

</form>

 

Now, in the parent page we can access the submitted name in the codebehind during the refresh.

 

protected void Page_Load(object sender, EventArgs e)

    {

if(txtName.Text != "")

Response.Write(txtName.Text);

    }


Recent Articles

Window.Open Clears Parent page and writes [Object]

                   

Sometimes we will have requirements to open an asp.net page as a popup when we click a hyperlink. Consider the below code,

 

<a href="javascript:window.open('http://www.CodeDigest.com')"> CodeDigest</a>

 

The above code will clear the existing content on the page and will output only [Object]. This behaviour is because the actual return value of Window.Open function is the window object itself.

To prevent the above behaviour and have the parent page without clearing its existing content we need to change the above code similar to below,

 

<a href="javascript:void(window.open('http://www.CodeDigest.com'))"> CodeDigest </a>

or

<a href="#" onclick="javascript:window.open('http://www.CodeDigest.com'); return false;"> CodeDigest </a>

 

Opening ASP.Net Popup window in specific location of the screen

 

Sometimes we would like to open our popup window on specific location on our screen. This can be done by MoveTo() method of the window object.

 

function OpenWindow(strChildPageUrl)

{

  var testwindow = window.open(strChildPageUrl,"Child","width=400px,height=400px,top=0,left=0,scrollbars=1");

        testwindow.moveTo(100,0);

}

 

Raising a Popup window when Closing the Page

At times we will have requirements to open a popup window when we close an asp.net page. For example, when a user closes the IE where our site is opened we can open a popup page thanking him and getting a feedback of our site. This can be achieved by opening a popup window on OnUnload event of BODY.

 

<body onunload="OpenWindow('ThankYouPage.aspx')">

 

Happy Coding!!

Similar Articles

Latest Articles

You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Title  
Submitted By  
Comment  
Enter the verification number
 
Comments
body onunload="OpenWindow('ThankYouPage.aspx')"
Raising a Popup window when Closing the Page

At times we will have requirements to open a popup window when we close an asp.net page. For example, when a user closes the IE where our site is opened we can open a popup page thanking him and getting a feedback of our site. This can be achieved by opening a popup window on OnUnload event of BODY.