Implement Model-View-Controller mode in ASP.NET (1)
background:
When creating a web application with ASP.NET, based on the complexity of the program, the program must be split into different parts to reduce the recruitment of the code and reduce changes caused by future changes.
Implementation strategy:
To explain how to implement (MVC) models in ASP.NET - view-controller mode, and instructions to separate software into models, views, and controller roles, this is described as an example as an example. This sample program is a single page program with a drop-down box, which is the data in the database. As shown below.
When the user selects a record in the drop-down box and click the Submit button, the program searches from the database record from the database, and is displayed in the form of a list. Hereinafter, it will be implemented in three different implementations.
Single page mode
There are many ways to solve this problem in ASP.NET, and the simplest is also the most direct way is to put all the code in a file and name Solution.ASPX, and the code is as follows:
<% @ Import namespace = "system.data"%>
<% @ Import namespace = "system.data.sqlclient"%>
Void Page_Load (Object Sender, System.EventArgs E)
{
String selectcmd = "select * from recording";
SqlConnection myconnection =
NEW SQLCONNECTION
"Server = (local); database = 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.database ();
}
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); database = recordings; trusted_connection = yes");
Sqldataadapter mycommand = new sqldataadapter (selectcmd, myconnection);
DataSet DS = New DataSet ();
MyCommand.fill (DS, "Track");
MyDataGrid.dataSource = DS;
MyDataGrid.databind ();
}
script>
hEAD>