Obtain the value of the parent page from the Child page

Source: Internet
Author: User

Obtain the value of the parent page from the Child page

There are multiple methods for transferring values between pages:

1. the most common is to use a hyperconnection or response. the redirect method is used to transfer a value from one page to another. When you want to obtain the value of the parent page in the new page, the value must be transmitted from the URL, that is, the value is transmitted using the get method. Passing values in this way has the following Disadvantages:
A) The passed value must be following the URL, so the data is insecure.
B) because the URL length is limited, the transmitted value is also limited.
Therefore, to transmit a large amount of data, you must use post to transmit the value. So we thought of the second method of passing values:

2. Use server. Transfer (URL) to redirect pages.
Server. transfer () and response. the biggest difference between redirect is response. redirect is actually like re-entering the address of the page to jump in the address bar, so it does not save the information of the previous page. However, server. Transfer () is completely different. server. Transfer () is like re-displaying the page you want to go to on the server side, but the previous page information is still saved on the server. Therefore, you can still get the value of the previous page on the new page. In. net, you can use previouspage to obtain the Page Object of the previous page. With the page object, you can easily get any value on the page. For example, if I want to find the value of ID = textbox1 ON THE PREVIOUS PAGE, the statement is:

(Textbox) previouspage. findcontrol ("textbox1"). Text

However, the above two methods are all forwarded, and the previous page is not displayed. Sometimes the Child Window is opened but the parent window still exists, just as in the _ PostBack () method, the Child window pops up in the parent window.
Let the child window pop up from the parent window, it is easy to think of using JavaScript.
There are two Javascript methods to open a window: window. open (); window. showdialog ()
If you use these two methods to obtain the value of the parent window in the Child Window, of course, there are two methods to pass the value following the URL and directly obtain the value of the parent window object.
The value of the first method is very simple. You can use querystring directly. However, as mentioned above, this method of passing values through get has some disadvantages.
The following two methods are discussed respectively to obtain the value of the parent page object.
1. use window. open (URL) to open a subwindow. It is very easy to obtain the object of the parent window in the subwindow. opener can get the parent window object. We also use this method to call the _ PostBack () method of the parent window in the Child Window. For example, I want to obtain the textbox value of ID = "textbox1" in the parent window.CodeAs follows:

<Script language = "JavaScript">
VaR text = plain opener.doc ument. getelementbyid ("textbox1"). value;
Alert (text );
</SCRIPT>

2. When you use the window. showmodaldialog () method to pop up a subwindow, if you want to get the value in the parent window, you still cannot get the parent window object using window. opener. Here we need to input the window parameter in the showmodaldialog () method. Then, obtain the window representing the parent window in the Child window, so that you can operate the value in the parent window at will.
The script is as follows:
In the parent window, if we call JavaScript code in the server control, you can write as follows:
Page. clientscript. registerstartupscript (this. GetType (), "test", "window. showmodaldialog ('default4. aspx ', window)", true );
Default4.aspx is the page of the subwindow. The page cannot be used. clientscript. registerclientscriptblock () because the block method will write the script in front of all controls, so that when the page is loaded, the textbox1 control has not been loaded, and the subwindow opens, therefore, no control value is obtained in the subwindow. The registerstartupscript () method is the opposite. It writes the corresponding script behind all controls, so the child window can get the values of any controls in the parent window.
If you write a Javascript script on the client, it will be simpler.

<Script language = "JavaScript">
Function popwindow ()
...{
Window. showmodaldialog ('default4. aspx ', window );
}
</SCRIPT>

The following script can be written in the subwindow:

<Script language = "JavaScript">
VaR vwin = Window. dialogarguments; // obtain the window parameter.
VaR Doc = vwin.doc ument. getelementbyid ("textbox1"). value; // obtain the textbox Value
Alert (DOC );
</SCRIPT>

 

 

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.