Three ways to pop up information windows in C #

Source: Internet
Author: User

Pop-up message box, which is a browser client event. The server does not have the ability to eject the message box.

Method One:

For an ASP. NET page if you need to pop up a message box, you need to register a JavaScript script on the foreground page, using the alert method. Use the Clientscript.registerstartupscript () method to register the script.

Clientscript.registerstartupscript ()

RegisterStartupScript (Type,key,script)

Type: Types of script events, typically with this. GetType () get

Key: The name of the script event cannot be duplicated.

Script:javascript script.

Example:

(1) String script= "<script>alert (' Registration information ') </scritp>"; Clientscript.registerstartupscript (this. GetType (), "success", script);

(2) The message box prompts you to refresh this page. String script= "<script>alert (' Registration information ');location.href=location.href</scritp>"; Clientscript.registerstartupscript (this. GetType (), "success", script);

(3) The message box prompts you to go to the new page. String script= "<script>alert (' registration information '); location.href= ' index.aspx ' </scritp>"; Clientscript.registerstartupscript (this. GetType (), "success", script);

(4) Open a new page in a new window. String script= "<script>alert (' registration information '); window.open (' index.aspx ') </scritp>"; Clientscript.registerstartupscript (this. GetType (), "success", script);

Windos.open () corresponds to Window.close (), one for opening a new window and one for closing the current window.

Summary: Modal window. This method is the recommended method.

Because it is used frequently, you can put this method into a class. To do this: Create a new Web site---The site root right-click---Add an ASP.---Select App_Code----Right-click the App_Code---Add a new Item---Select the class to which the new file is completed.

The new method in the class is as follows:

Pop-up message, info content is info

public static void Alert (string info, page p)
{
String script = "<script>alert ('" +info+ "') </script>";
P.clientscript.registerstartupscript (P.gettype (), "", script);
}

The method that calls the class is:

The class name. Alert (registration information, this); Because the method is a static method, it is called directly from the class name. If the method is not a static method, the object needs to be instantiated after the call. Instantiate as follows:

Class name A=new class name (); Then call: A.alert (registration successful, this);

Method Two: Response.Write ();

String script= "<script>alert (' Registration information ') </scritp>"; Response.Write (script);

Summary: Modal window, the pop-up window does not close, the page can not be manipulated. Not recommended, this pop-up window will deform the page.

Method Three: MessageBox.Show ("registration success");

Before using this method, you need to prepare the following:

Site Directory Right-click---Add Reference---find System.Windows.Forms, OK. Then add: Using System.Windows.Forms on the page, and then use this method in the page.

Summary: C # is often used in modal windows, Web sites (Web pages) are not modal windows, Web pages are not recommended, C # is recommended to use.

Three ways to pop up information windows in C #

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.