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:
- private void Button1_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. As it turns out, you can see the following ASP. NET code separation 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 VoidPage_Load (ObjectSender, 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 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.
- Analysis on ASP. NET data type conversion
- Analysis of validation groups in ASP. NET data verification
- Analysis on the use of ASP. NET data verification controls
- Analysis on five common controls for ASP. NET data verification
- Some discussions about ASP. NET code separation