Thursday, November 27, 2008

The use Of window.opener function in Java Script .

HI All.


Today I m going to describe the use of the window.opner function .Basically It is used to update the Value Of parent page from the child pop Up page.

The fully described code is given as below.

Step 1.) Parent Page.

A.) Script of the parent page :


function goChild()
{

window.open("~\WindowOpener\ChildPage.aspx");

}



B.)HTML of this page is :

[form id="form1" runat="server"]
[div]
[asp:Label ID="lblSum" runat="server" /][br /]

[br /]
[asp:Button ID="btnChild" OnClientClick="goChild();" Text="PopUp Open" runat="server" / /]
[br /]
[/div]
[/form]

C.) The Code Behind File of the Page.

There is no need to write any line of the code for this little Example




Step 2.)child Popup page.


A.) Script of the Child popUp page

function SumAndGO(varValue)
{
self.close();
window.opener.document.getElementById('lblSum').innerHTML=varValue;
}
function Validation(varValue)
{
for(i=0; i< varValue.length ;i++)
{
alert(varValue.charCodeAt(i));
}
}



B.) HTML of this page is :

[body]
[form id="form1" runat="server"]
[div]
Number 1: [asp:TextBox onblur="Validation(this.value);" ID="TxtNum1" runat="server"][/asp:TextBox][br /]
Number 1: [asp:TextBox ID="TxtNum2" runat="server"][/asp:TextBox][br /]
[asp:Button ID="btnSum" runat="server" Text="Go For Sum" OnClick="btnSum_Click" /]
[/div]
[/form]
[/body]




c.) The Code Behind File of the Page.

protected void btnSum_Click(object sender, EventArgs e)
{
int sum = Convert.ToInt32(TxtNum1.Text) + Convert.ToInt32(TxtNum2.Text);
string Jscript = "SumAndGO(" + sum + ")";
Jscript = "";
ClientScript.RegisterStartupScript(typeof(Page), "Jscript", Jscript);
}




Regards:
Sanjeev Chauhan

No comments: