asp.net implementation of Model-view-controller mode in asp.net (ii.)
The MVC pattern describes this implementation as a passive implementation mechanism, ASP. NET acts as a controller in program execution, but programmers must add specific event-handling methods to the response function of the event. In this example, the controller calls the Page_Load function and executes the code before the page is loaded, and the system calls the SubmitBtn_Click function and executes it when the user clicks the Submit button.
This is a very straightforward way to implement the code in a single file, and it's a good way to do it when the application is small and not often modified, but you might start thinking about modifying the implementation if some of the following situations occur:
Make programming work parallel and reduce the likelihood of errors that occur. To increase the parallelism and efficiency of your work, you may want different people to write the view code and the model code and try to reduce the likelihood of error in this way of working. For example, when all the code is in one file, a programmer may inadvertently modify the data access code while changing the DataGrid display format. This error is difficult to discover until the page is compiled as a whole.
Make your data access code reusable in other pages. In such a single file implementation, in addition to copying code, there is no other way to achieve the reuse of code. Copy code is difficult to maintain, because once the code changes, you have to modify all the pages.
To avoid these situations above, ASP. NET introduces the code separation (code-behind) mechanism.
Refactoring with code separation mechanisms
Microsoft Visual Studio. NET makes the code of the view layer easy to separate from the model and the Code of the controller. Each asp.net page has a mechanism that enables the method to be invoked to be implemented in a class that is detached from it. This is a convenient way to use and can also take advantage of visual Studio. NET, to complete the development work together with some other features. For example, when you develop your page using code-splitting mechanisms, you can use IntelliSense technology to display a list of available methods for easy programming. IntelliSense technology is not available in. aspx pages.
Here is the example above that utilizes the implementation of the code separation mechanism.
View section:
The code for the view layer is now implemented in a separate file. Solution.aspx:
This line tells the ASP.net execution environment, the specific implementation mechanism of this page in a separate class. Because this page is independent, this page does not need to be changed if the code for data access changes. Similarly, some designers can change the code of the page without causing any data access errors.
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