Use the example of code separation in asp.net to share _ practical tips

Source: Internet
Author: User

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.

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.