Summary of window-related operations in Asp.net (JavaScript)

Source: Internet
Author: User
These Code It is written in the code page separated from the page (codebehind). If you want to embed it into the *. ASPX page
Problems may occur.

1. open a new window
This is simple: Response. Write (@ "<script
Language = 'javascript '> window. Open ('url'); </SCRIPT> ");
2. Close the window
// Close the current window and prompt the user to close it. Yes: close. No: exit.
Response. Write (@ "<script language = 'javascript '> window. Close (); </SCRIPT> ");
// Delayed closing window (the following code indicates closing in 2 seconds without confirmation)
Response. Write (@ "<script
Language = 'javascript '> setTimeout ('self. Close ()', 2000); </SCRIPT> ");
3. Delay Time
This is not much different from the above. I used the case that, after the user operation is complete, a prompt is displayed, "n seconds later, the page
You only need to remove the double self. Close () to the switch.
Response. Write (@ "<script
Language = 'javascript '> setTimeout ('', 2000); </SCRIPT> ");
4. A prompt or warning window is displayed.
Response. Write (@ "<script language = 'javascript '> alert ('added successfully, page 2 seconds later
Will automatically Jump '); </SCRIPT> ");
5. Refresh other pages
There are still many situations in this use. For example, data is updated and modified on page B, and a must keep the latest data on the other page.
To the customer, we need to refresh a after the operations on B are completed:
Response. Write (@ "<script language = 'javascript '> window. opener. location.
Href = './default. aspx' </SCRIPT> ");
6. page Jump
Sometimes you can jump to a page when you are learning to give a prompt. You cannot use response. Redirect ("url ");
For example, after the customer completes the operation, click the button to submit. A prompt box is displayed (Use 3 and 4 above ).
Response. Redirect ("url ");
The page will not be prompted, and the pages 3 and 4 will go directly if they do not work.
If you are using the following procedure:
1). response. Write (@ "<script language = 'javascript '> alert (' added successfully, page 2 seconds later
The plane automatically jumps '); </SCRIPT> ");
2). response. Write (@ "<script
Language = 'javascript '> setTimeout ('', 2000); </SCRIPT> ");
3) page orientation:
Response. Write ("<meta http-equiv = 'refresh'
Content = '0; url =./default. aspx '> ");
// I don't know how to implement it using JavaScript.
7. Window Transfer Problem
This problem is annoying. I have not implemented the mode window (showmodeldialog). Please add it.
The specific situation is as follows. For example, a opens a new window B. After the user completes operations on B, we obtain the necessary
Data is automatically assigned to textbox and other controls in a. This process is completed on the client, just like when an email is sent,
You need to select from the address book and then pass back the selected sending Address.
1) Add the following code to a to open a new window B.
Response. Write (window. "> @" window.
Open ('B. aspx', '', 'toolbar = No, menubar = No, status = Yes, location = No,
Resizable = No, scrollbars = No, width = 500, Height = 350 '); </SCRIPT> ");
2) After completing the operations on B, obtain the required data tmpstr and assign it to textbox1 in.
Response. Write (@ "<script
Language = 'javascript '> opener.doc ument. All. textbox1.value
= '"+ Tmpstr +"'; </SCRIPT> ");
Note: This process is completed by using JavaScript on the client, so we cannot follow the programming habits of the server.
And cannot use the server properties of the Web Service Control for operations. textbox1 is a Web Service Control,
Its ID is textbox1, and page a is parsed as HTML by the server. After browsing through the browser, textbox1 becomes
Pure HTML control. Here we use JavaScript to operate the HTML control, so the value assignment is
The Value Attribute of textbox1 is not the text attribute. In addition, after the ID of the Web server control is parsed into HTML,
The ID is changed sometimes. You only need to pay attention to the ID resolved by the server. Right-click in the browser and choose> View source file.
You can get

In case 7, it is better to open the modal window windows. showmodeldialog (), but it is troublesome,
No.


Connect:

Adjust the size and position of this window
Response. Write ("<SCRIPT> window. resizeTo (500,400); </SCRIPT> ");
Response. Write ("<SCRIPT> window. moveTo (300,200); </SCRIPT> ");


Connect:

Supplement: Use the modal window to pass values

The main code is as follows:
A. aspx and A. aspx. CS

<% @ Page Language = "C #" codebehind = "A. aspx. cs" autoeventwireup = "false"
Inherits = "genesis02.a" %>
<HTML>
<Head>
<Title> A </title>
<Script language = "JavaScript">
VaR STR;

Function POP (URL)

{
VaR mydialog = document. A. tboxtype. value;

If (window. showmodaldialog)
{
STR = Window. showmodaldialog (URL, mydialog, "dialogheight:
300px; dialogwidth: 500px; center: Yes; help: No; resizable: Yes; Status:
No ;");
If (typeof (STR )! = "Undefined ")
{
Document. A. tboxtype. value = STR;
}
}
}
</SCRIPT>
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "A" method = "Post" runat = "server">
<Asp: textbox id = "tboxtype"
Runat = "server"> </ASP: textbox> <asp: button id = "btngettype" runat = "server"
TEXT = "open"> </ASP: button>
</Form>
</Body>
</Html>
__________________________________________________________________

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;

Namespace genesis02
{
/// <Summary>
/// Summary of.
/// </Summary>
Public Class A: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. textbox tboxtype;
Protected system. Web. UI. webcontrols. Button btngettype;

Private void page_load (Object sender, system. eventargs E)
{
If (! Ispostback)

{
Btngettype. attributes ["onclick"] = "Pop ('C. aspx '); Return
False ;";
}
}

# Region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion
}
}


######################################## #########################
B. aspx and B. aspx. CS

<% @ Page Language = "C #" codebehind = "B. aspx. cs" autoeventwireup = "false"
Inherits = "genesis02. B" %>
<HTML>
<Head>
<Title> B </title>
<Script language = "JavaScript">
Function closeme ()
{

// Top. returnvalue =
Registry.document.all.lboxrfacontent.options?registr=doc ument. All. lboxrfacontent
. Selectedindex]. value;
Top. returnvalue = Document. getelementbyid ("tboxvalue"). value;
Top. Close ();
}

Function body_load ()
{
If (window. dialogarguments! = NULL ){
Specified parameter Doc ument. getelementbyid ("tboxvalue"). value =
Window. dialogarguments;
}
}
</SCRIPT>
</Head>
<Body ms_positioning = "gridlayout" onLoad = "body_load ()">
<Form ID = "B" method = "Post" runat = "server">
<Asp: button id = "btnclose" style = "Z-INDEX: 101; left: 152px;
Position: absolute; top: Running PX "runat =" server "text =" close me "> </ASP: button>

<Asp: textbox id = "tboxvalue" style = "Z-INDEX: 102; left: 149px;
Position: absolute; top: 69px "runat =" server "> </ASP: textbox>
</Form>
</Body>
</Html>

__________________________________________________________________
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;

Namespace genesis02
{
/// <Summary>
/// Summary of B.
/// </Summary>
Public Class B: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. textbox tboxvalue;
Protected system. Web. UI. webcontrols. Button btnclose;


Private void page_load (Object sender, system. eventargs E)
{
If (! Ispostback)
{
Btnclose. attributes ["onclick"] = "closeme (); Return false ;";
}
}

# Region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion
}
}

######################################## ###############################

C. aspx and C. asp. CS

<% @ Page Language = "C #" codebehind = "C. aspx. cs" autoeventwireup = "false"
Inherits = "genesis02.c" %>
<HTML>

<Head>
<Title> C </title>
</Head>
<Frameset rows = "0, *">
<Frame src = "about: blank">
<Frame src = "B. aspx">
</Frameset>
</Html>


____________________________________________________

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;

Namespace genesis02
{

/// <Summary>
/// Summary of C.
/// </Summary>
Public Class C: system. Web. UI. Page
{
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
}

# Region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}

######################################## #####################################

C. aspx and C. asp. CS

<% @ Page Language = "C #" codebehind = "C. aspx. cs" autoeventwireup = "false"
Inherits = "genesis02.c" %>
<HTML>
<Head>
<Title> C </title>
</Head>
<Frameset rows = "0, *">
<Frame src = "about: blank">
<Frame src = "B. aspx">
</Frameset>
</Html>


____________________________________________________

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;

Namespace genesis02
{
/// <Summary>
/// Summary of C.
/// </Summary>
Public Class C: system. Web. UI. Page
{
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
}

# Region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}

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.