Archive

Archive for September, 2011

Life Cycle of page in ASP.Net

September 30, 2011 Leave a comment

There is sequence on event for a page(Who has master  page and a user control)

UserControl_Init

Master_Init

Page_Init

Page Load

Master Load

UserControl_Load

Control_Event(Button click etc)

Page Load_Complete

Page_PreRender

ViewState Saved

Page_Render

Page_UnLoad

Categories: ASP.Net

window.opener object to access the parent browser window from a child window

September 14, 2011 1 comment

Using window.opener object to access the parent browser window from a child window: (compatible with IE & Firefox)
The window.opener object can be used to access the HTML elements present in the parent browser window from the child window(popup or similar).

Let us consider two HTML files:

openwindow.htm – this has a text box and a button, clicking the button opens the target.htm file in a new browser window
target.htm – this has code to change the value of the text box present in the parent window(openwindow.htm) that opens this file

openwindow.htm:
window.open("target.htm","_blank","height=200,width=400, status=yes,toolbar=no,menubar=no,location=no")
}
input type=button onclick="javascript:openwindow()" value="Open window.."

If you are not familiar with window.open() method then you would be wondering what is “height=200, width=400,status=yes,toolbar=no,menubar=no,location=no”, don’t worry, it just specifies the size and appearance of the new window, this line of code can be changed to window.open(“target.htm”,”_blank”) just see the difference in output if you change.

Note that if the parameter ”_blank” is not provided then the functionality will differ between IE and Firefox, in firefox a new tab will be opened instead of a new window. Just use window.open(“target.htm”) and see the difference yourself. For more info about window.open() method and its parameters, see http://msdn2.microsoft.com/en-us/library/ms536651(VS.85).aspx. Also note, in , the id attribute is necessary, then only the code of target.htm(shown below) will execute in Firefox.

target.htm:

function changeparent()
{
window.opener.document.getElementById('text1').value="Value changed.."
}
input type=button onclick="javascript:changeparent()" value="Change opener's textbox's value.."

Hope you can get what is going on in the above code, in the new window(target.htm) we are using the window.opener object to access the text box present in the parent window(openwindow.htm). You just need to prefix “window.opener.” and write the same code that you will write in the parent window’s HTML page to access its elements.

Using window.showModalDialog() to send data to parent browser window from a child window: (only IE, wont work in Firefox)
If you want to open a modal dialog and access the parent window’s elements in it, then this method can be used, but this will work only in IE. (If you are new to the term modal dialog, see http://en.wikipedia.org/ wiki/Modal_window) Here we will be calling the window.showModalDialog() method to open the child window and assign the return value of this method to a js variable, the return value for the window.showModalDialog() method is provided in the child window by using the window.returnValue property, as shown in the below example.

Here I am going to show two files similar to the example shown for window.opener object.

showmodal.htm – this has a text box and a button, clicking the button opens the modaltarget.htm file in a new browser window
modaltarget.htm – this has code to change the value of the text box present in the window that opens this file

showmodal.htm:

function openwindow()
{
retval=window.showModalDialog("modaltarget.htm")
document.getElementById('text1').value=retval
}
input type=button onclick="javascript:openwindow()" value="Open window.."

modaltarget.htm:

function changeparent()
{
window.returnValue="Value changed.."
window.close()
}
input type=button onclick="javascript:changeparent()" value="Change main window’s textbox value.."
Categories: .Net, ASP.Net