Implement Model-View-Controller mode in ASP.NET (2)
The MVC mode describes this implementation is a passive implementation mechanism, and the ASP.NET acts as the role of the controller in the execution, but the programmer must add the specific event processing method to the event's response function. As in this example, the controller calls the Page_Load function before the page is loaded and executes the code when the user clicks on the Submit button and executes.
This type of implementation is very straightforward, and it can be a good method when the application is small and not modified often, but if you appear in some cases, you may Start considering modifying this implementation:
Make the programmed work parallel and reduce the likelihood of this result. In order to increase the parallelism of the work, improve efficiency, you may want to write different people to write code and model code and try to reduce the likelihood of this work mode. For example: When all the code is in a file, then a programmer may inadvertently modify the code accessed inadvertent when changing the DataGrid display format. This error is difficult to discover until the page is compiled, and it will appear. .
Reuse your data access code in other pages. In this single file implementation, in addition to copying code, there is no other method to be reused. Copy code is difficult to be maintained, because once the code changes, you must modify all pages.
In order to avoid the occurrence of these conditions, ASP.NET introduces a code-behind mechanism.
Reconstructing with code separation mechanism
The code separation mechanism in Microsoft Visual Studio .NET makes the code from the code layer and the code of the model and the code of the controller can be easily separated. Each ASP.NET page has a mechanism that enables the method to be called in a class that is separated from it. This way is very convenient, and can also use some other features in Visual Studio .NET to complete development work. For example, when you use the code separation mechanism to develop your page, you can use the IntelliSense Technology to display a list of available methods to be programmed. The intelligent perception technology cannot be used in the .aspx page.
The following example is the implementation of the above example utilizes the code separation mechanism.
View part:
The code of the view layer is now implemented in a separate file. Solution.aspx:
<% @ Page language = "c #" codebehind = "soliion.aspx.cs"
Autoeventwireup = "false" inherits = "solution"%>
hEAD>