Experience in ASP. NET code separation

Source: Internet
Author: User

The use of ASP. NET code separation comes from the following: this situation is often encountered during project development recently. When a user clicks a Button on the page, a window is required. 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:

 
 
  1. private void Button1_Click(object sender, System.EventArgs e)  
  2. {  
  3.      string strScript = "﹤script language=javascript﹥\n";  
  4.      strScript += "window.alert(" + "\"hello\"" + ");";  
  5.      strScript += "";  
  6.      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. As it turns out, you can see the following ASP. NET code separation implementation method:
1. Add this code before aspx

 
 
  1. ﹤script language="jscript"﹥   
  2.      function showmessagebox()  
  3.     {  
  4.          window.alert("hello");  
  5.      } 

2. Add the following code to the Page_Load event of the cs file:

 
 
  1. Private VoidPage_Load (ObjectSender, System. EventArgs e)
  2. {
  3. // Place user code here to initialize the page 
  4. This. Button1.Attributes. Add ("Onclick","Javascript: showmessagebox ();");
  5. }

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.

ASP. NET code separation Summary: using this method to write programs 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!

The practical experience of ASP. NET code separation is introduced here. I hope to help you understand ASP. NET code separation.

  1. Analysis on ASP. NET data type conversion
  2. Analysis of validation groups in ASP. NET data verification
  3. Analysis on the use of ASP. NET data verification controls
  4. Analysis on five common controls for ASP. NET data verification
  5. Some discussions about ASP. NET code separation

Related Article

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.