Often when developing projects, we expect the system to pop up a window when the user clicks the button. For example, the following code:
Copy Code code as follows:
{
String strscript = "<script language=javascript>\n";
Strscript + + "Window.alert" ("+" \ "hello\" "+"); ";
Strscript + = "";
Response.Write (Strscript);
}
The code above will pop up a Hello dialog box. The method is to write a JavaScript script to the foreground in your background. cs file, but I think you can see this code, it may feel very uncomfortable, a bit messy! If you need to pop up a page, and you need to pass parameters, the code that you write will look more messy!
Later, I thought about whether I could put the script part in the. aspx file, and directly refer to the function name in the. cs. As it turns out to be possible, see the following implementation method:
1. Add this code before ASPX
Copy Code code as follows:
function ShowMessageBox ()
{
Window.alert ("Hello");
}
2. Add the following code in the Page_Load event of the CS file
Copy Code code as follows:
{
Place user code here to initialize page
This. BUTTON1.ATTRIBUTES.ADD ("onclick", "Javascript:showmessagebox ();" );
}
3. Now when you click on the Button1 button on the page, the effect is similar to the previous method, but the entire system code looks much more comfortable.
Summary: Using this method to write a program does not make your program run faster and more stably .... The role it plays is to make your code easier to read and communicate with people. Using this idea, we can extrapolate and try not to write a large number of JavaScript scripts in CS files.