How to pop up a new window in the ASP. NET program and get the return value

Source: Internet
Author: User

First, let's start with a simple example. This example shows how to click a button on the page to open a new page, input data, return to the initial Page, and update the page.
 

1. Create a project with vs. NET and select "Asp.net web application"Program"
2. Add a textbox and a button in the webform1.aspx window.
3. In the webform1.aspxCodeIn page_load, add

If (not isclientscriptblockregistered ("clientscript") then
Dim strscript as string = "<SCRIPT>" + vbcrlf
Strscript + = "function openwin () {" + vbcrlf
Strscript + = "Var STR = Window. showmodaldialog ('webform2. aspx ', document. form1.textbox1. Value)" + vbcrlf
Strscript + = "If (STR! = NULL) document. form1.textbox1. value = Str "+ vbcrlf
Strscript + = "}" + vbcrlf
Strscript ++ = "</SCRIPT>" + vbcrlf
Registerclientscriptblock ("clientscript", strscript)
End if
Button1.attributes. Add ("onclick", "openwin ()")

4. Add a webform2
5. Switch to HTML and delete the content between <body> </body> segments.

<Frameset rows = "0, *">
<Frame src = "about: blank">
<Frame src = "webform3.aspx">
</Frameset>

6. Create a New webform3
7. Switch to HTML and add "id = 'mybody'" in <body> '"
8. Add a textbox and a button to webform3.
9. Add a variable to the code.

Protected mybody as system. Web. UI. htmlcontrols. htmlcontrol

10. Add in page_load

If ispostback then
Dim strscript as string = "<SCRIPT>" + vbcrlf
Strscript + = "window. Parent. returnvalue = '" + textbox1.text. Replace ("'", "\ '") + "'" + vbcrlf
Strscript + = "window. Parent. Close ()" + vbcrlf
Strscript ++ = "</SCRIPT>" + vbcrlf
If (not isclientscriptblockregistered ("clientscript") then
Registerclientscriptblock ("clientscript", strscript)
End if
End if
If not ispostback then
Mybody. Attributes. Add ("onLoad", "document. form1.textbox1. value = Window. Parent. dialogarguments ")
End if
Now, let's try it. An error occurs. How can this happen?

Now, on the webform3 HTML page, find the <body> area and add a runat = "server"
Run it again. Everything is OK.

Now let's take a look at the above Code,

First, at the beginning of webform1, we defined a Javascript script block called "clientscript" and registered it on this page.

This script block defines a function openwin
Function openwin ()
{
VaR STR = Window. showmodaldialog ('webform2. aspx ', document. form1.textbox1. value)
If (STR! = NULL)
Document. form1.textbox1. value = Str
}
 

In addition, this function is associated with the onclick event of the button button1.

This function uses the showmodaldialog event of window to open a modal dialog box webpage webform2.aspx. The parameter is the value in textbox.
Secondly, in webform2, we don't write code, but let webform2 act as a container and define it as a framework, where webform3 is a webpage in the framework.

Finally, we go to webform3. The specified JavaScript code in page_load is to set the webform2 return value to the textbox value in webform3, and then disable webform2.

Obviously, webform2 is just a transition page. Why do we need to do this?
We tried to modify the Javascript script of webform1, open webform3.aspx directly, modify window. Parent to window in page_load of webform3, and then run it again to see what would happen.
When the webpage dialog box is opened, everything is normal, but in the dialog box, the buttons cannot be returned, neither return nor close themselves, but a window flashes through.
What's going on?
In fact, it is because we use window. showmodaldialog to open the modal dialog box. There is no way to close a modal dialog box opened in showmodaldialog mode by window. Close. After you press the button, a new page is opened and the page is closed.

Therefore, we use frame in webform2 to prevent opening a new page during submission.

So what if we use open instead of showmodaldialog?
In webform1, continue to modify the script
Window. open ('webform3. aspx ', 'newwindow', 'height = 100, width = 400, toolbar = No, menubar = No, scrollbars = No, resizable = No, location = No, status = no ')
It is normal to enable this function, but the parameter cannot be passed without being passed in.

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.