How to assign values to a parent window in a subwindow and a Case Study

Source: Internet
Author: User

Today, Insus. NET exercises JavaScript to open a child window from the parent window. After processing some results in the Child window, the results are assigned to the text box in the parent window. Visible effect:
 
Create two aspx pages on the site. One is PageA. aspx and the other is PageB. aspx:
In the Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function popUp (url ){
ObjSubWin = window. open (url, "Popup", "toolbar = no, scrollbars = no, location = no, statusbar = no, menubar = no, resizable = 0, width = 300, height = 80 ");
ObjSubWin. focus ();
}
Function SetValue (val ){
Var amount = document. getElementById ('<% = TextBoxAmount. ClientID %> ');
Amount. value = val;
}
</Script>

Then, in the <body> node, pull a TextBox and a Button:
Copy codeThe Code is as follows:
Amount:
<Asp: TextBox ID = "TextBoxAmount" runat = "server" Enabled = "false"> </asp: TextBox>
<Asp: Button ID = "Button1" runat = "server" Text = "Call child window" OnClientClick = "popUp ('pageb. aspx ')"/>

OK. The parent page is complete. Next, write PageB. aspx subpage, or write Javascript script first, also has two functions, one is to verify that the text box can only enter numbers, the other is the calculation method, within the calc () method, there is a way to call the parent window.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function isNumeric (keyCode ){
Return (keyCode> = 48 & keyCode <= 57) | keyCode = 8)
}
Function calc (){
If (window. opener! = Null &&! Window. opener. closed ){
Var qty = document. getElementById ('<% = TextBoxqty. ClientID %> ');
Var price = document. getElementById ('<% = TextBoxPrice. ClientID %> ');
Window. opener. SetValue (parseInt (qty. value) * parseInt (price. value ));
}
}
</Script>

In the <body> node of PageB. aspx, pull two text boxes and one Button.
Copy codeThe Code is as follows:
Quantity <asp: TextBox ID = "TextBoxqty" runat = "server" onkeydown = "return isNumeric (event. keyCode); "onpaste =" return false; "Width =" 50 "> </asp: TextBox>
* Unit price <asp: TextBox ID = "TextBoxPrice" runat = "server" onkeydown = "return isNumeric (event. keyCode); "onpaste =" return false; "Width =" 50 "> </asp: TextBox>
<Asp: Button ID = "Button1" runat = "server" Text = "Calculate" OnClientClick = "calc ()"/>

Additional article about text box verification: http://www.jb51.net/article/33586.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.