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 code in The OnClick event of the Button:
{
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
Function showmessagebox ()
{
Window. alert ("hello ");
}
2. Add the following code to the Page_Load event of the cs file:
{
// 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 much more comfortable.
[Conclusion]: using this method to write a program won't make your program run faster and more stable .... it only makes your code easier to read and communicate with others. Using this idea, we can draw a line by mistake, and try not to write a large number of javascript script files in cs files. If you have any good ideas, please feel free to contact me!