Implement MVC mode in ASP.NET (3)

zhaozj2021-02-16  54

Implement Model-View-Controller mode in ASP.NET (3)

Model and controller part:

The second part of this solution is hidden background code:

Using system;

Using system.data;

Using system.data.sqlclient;

Public Class Solution: System.Web.ui.page

{

Protected system.Web.ui.WebControls.button submit;

Protected system.Web.ui.WebControls.DataGrid mydatagrid;

Protected system.Web.ui.webcontrols.dropdownlist recordingselect;

Private Void Page_Load (Object Sender, System.EventArgs E)

{

IF (! ispostback)

{

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 ();

}

#Region Web Form Designer Generated Code

Override protected void oninit (Eventargs E)

{

//

// Codegen: This Call is Required by The ASP.NET Web Form Designer.

//

InitializationComponent ();

Base.onit (e);

///

/// Required Method for Designer Support - Do Not Modify

/// The contents of this method with the code editor.

///

Private vidinitiRizeComponent ()

{

This.Submit.Click = new system.eventhandler (this.submitbtn_click);

This.Load = New System.EventHandler (this.page_load);

}

#ndregion

}

Here will move the code from the previous implementation method to a file in its own file. The views and model controllers are connected to a whole through some mechanisms. If the member variables in this class are the same name as the control used in the Solution.aspx file. Another must be displayed that how the controller connects the behavior to the events thereafter. In this example, the InitializationComponent function is connected to two events. The first one connects the LOAD event with the Page_Load function, the second is the Click event, call the submitbtn_click function when the Submit button is clicked. The code separation is an excellent mechanism that separates the view part and the model and controller part. But when you want to remove the code from the isolated background to other pages, it may be insufficient. Technically, the code reuse behind the page is feasible, but as you need to share the increase in the page, it is difficult to connect the page with the background code.

转载请注明原文地址:https://www.9cbs.com/read-25076.html

New Post(0)