This is often the case during recent project development. When a user clicks a button on the page, a window pops up. This window may be a standard dialog box or a new page. At the beginning, I didn't know how to implement this function. According to my previous programming habits, I think there should be a method similar to showmessage. However, unfortunately, this method does not exist on the web. By checking the information on the Internet, it is found that the general practice is to write the following in the onclick event of the button: Code : Private Void Button#click ( Object Sender, system. eventargs E)
{
String Strscript = " <Script language = JavaScript> \ n " ;
Strscript + = " Window. Alert ( " + " \ " Hello \ "" + " ); " ;
Strscript + = "" ;
Response. Write (strscript );
}
The effect of the above Code is that when you click the button1 button, a dialog box will pop up. In this way, a Javascript script file is embedded in your. CS file. However, I think it may be uncomfortable to see such code, so many "" are easy to faint! If you need to bring up a page and pass parameters, the code you write will feel dizzy!
Later, I was wondering if I could put all the scripts in the. aspx file and directly reference the function name in. CS. It turns out that this is acceptable. See the following implementation method:
1. Add this code before aspx < Script Language = " JScript " >
Function Showmessagebox ()
{
Window. Alert ( " Hello " );
}
2. Add the following code to the page_load event of the CS file: Private Void Page_load ( Object Sender, system. eventargs E)
{
// Place user code here to initialize the page
This . Button1.attributes. Add ( " Onclick " , " Javascript: showmessagebox (); " );
}
3. Now, when you click the button1 button on the page, the result is similar to the previous method, but the entire system code looks quite comfortable.