Implementing the MVC pattern in asp.net (i)

Source: Internet
Author: User
asp.net implementation of Model-view-controller mode in asp.net (i)



Background:



When using ASP.net to create Web applications, based on the complexity of the program, the program must be split into different parts to reduce the duplication of code and reduce the changes caused by future changes.



Implementation strategy:



To explain how to implement the (MVC) model-View-controller pattern in asp.net, as well as the benefits of separating the software into models, views, and controller roles, take an example program to illustrate this. The example program is a single page program with a Drop-down box that displays the data in the database. The following figure.





When the user selects a record in the Drop-down box and clicks the Submit button, the program searches the database for the database records associated with the selected record and displays it in the form of a list. Below, the implementation will be implemented in three different implementations.



Single page mode



There are many solutions to this problem in asp.net, the simplest and most direct way is to put all the code into a file, and named solution.aspx, implementation code as follows:

<%@ Import namespace= "System.Data"%>

<%@ Import namespace= "System.Data.SqlClient"%>



<title>start</title>

<script language= "C #" runat= "Server" >

void Page_Load (object sender, System.EventArgs e)

{

String selectcmd = "SELECT * from Recording";



SqlConnection myconnection =

New SqlConnection (

"Server= (local);d atabase=recordings; Trusted_connection=yes ");

SqlDataAdapter mycommand = new SqlDataAdapter (Selectcmd,

MyConnection);



DataSet ds = new DataSet ();

Mycommand.fill (ds, "Recording");



Recordingselect.datasource = ds;

Recordingselect.datatextfield = "title";

Recordingselect.datavaluefield = "id";

Recordingselect.databind ();

}



void SubmitBtn_Click (Object sender, EventArgs e)

{

String Selectcmd =

String.Format (

"SELECT * from Track where RecordingID = {0} ORDER by id",

(string) recordingSelect.SelectedItem.Value);



SqlConnection myconnection =

New SqlConnection (

"Server= (local);d atabase=recordings; Trusted_connection=yes ");



SqlDataAdapter mycommand = new SqlDataAdapter (Selectcmd,

MyConnection);



DataSet ds = new DataSet ();

Mycommand.fill (ds, "Track");



Mydatagrid.datasource = ds;

Mydatagrid.databind ();

}

</script>


<body>

<form id= "Start" method= "POST" runat= "Server" >


Select a recording:<br/>

<asp:dropdownlist id= "Recordingselect" runat= "Server"/>

<asp:button runat= "Server" text= "Submit" onclick= "SubmitBtn_Click"/>

<p/>

<asp:datagrid id= "Mydatagrid" runat= "Server" width= "700"

Backcolor= "#ccccff" bordercolor= "Black" showfooter= "false"

cellpadding= "3" cellspacing= "0" font-name= "Verdana"

Font-size= "8pt" headerstyle-backcolor= "#aaaadd"

Enableviewstate= "false"/>

</form>

</body>




This implementation file contains the three roles of model, view, controller, but does not divide them into different files or classes. The View object is implemented in HTML, with a data-bound control to display data from the dataset returned from the database. Model roles are implemented in the Page_Load and SubmitBtn_Click functions. The controller role is not explicitly implemented, but is implicitly implemented by ASP.net. When the program is running, when the user makes a request for the page, the page is updated with the user's choice.


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.