Object-oriented programming ideas in ASP.NET

zhaozj2021-02-17  53

First of all, let's talk about the programming idea of ​​the object. I think the mainstream programming idea is not two: structural and object-oriented. Previously, we were completely structural ideas in ASP. Now, ASP.NET can fully support object-oriented programming ideas, this has to be said to be a qualified leap.

I remember that I wrote a "object-oriented object" from the artificial earth in about half a year. "At that time, I found that anything can be seen in an object-oriented thinking. I am still talking about it now. It is nothing more than the object to be a class. If you want to use the class, you must create an instance of the class. This is very good, thanks to the parter is a class, we can't say people to do things (so do not targeted), we tend to say Zhang San or Li Si, this three or Li Si is a class of people. An example. This is the concept of the object. Telling again is an event and action, an example: starting, raining, receiving clothes. It is getting up, raining is an event. This incident will inspire this action. This action is an object. If you have learned the trigger of SQL Server, I want to be more clear about this idea. Object-oriented is more important to inherit (inherits) and polymorphism. This is very understanding, for example, Zhang San will receive clothes, Zhang San Lifeng's son inherits Zhang San's characteristics, will also collect clothes, this is inherited, and said that Zhang San's son will drive, this is a polymorphism.

Ok, for the object-oriented understanding, let me say this, or how to program the object-oriented views in ASP.NET. When we edit the web program, we usually have to process: UI (user interface, but also data display) and code (how to handle the data of the data. In order to achieve the complete separation of the UI and Code, we treat UI as a Object, Code is an object. Of course we want to study the relationship between these two objects, remember that we will copy the same paragraph code when editing the ASP program, it seems that Code is a parent class, UI is a subclass However, they all inherited from System.Web.ui.page. Raicit the relationship of the class, we don't even understand the idea of ​​code UI and CODE. Relationship diagram: Introduction Name Space: using system.Web.ui Parent Class Page → Many Subcador Code (*. CS Files) → Multiple Subclass UI (*. ASPX Files)

Because I mainly want to know this programming idea, I only give a simple example, I think everyone has thought, and then look for examples.

File two: UI Class: Default.aspx is mainly used to display data, use the day DataGrid control CODE class: default.aspx.cs mainly used to establish data link, query, binding data in the DataGrid control, I created my own Namespace named Vagrant, as well as class Myvagrant Database: Data, there is a relationship table Student

To illustrate the Default.aspx class inherited from default.aspx.cs, you need to declap in default.aspx: <% @ page language = "c #" codeBehind = "default.aspx.cs" inherits = "vagrant.myvagrant"% > CodeBehind indicates the source file of the parent class, inherits = "vagrant.myvagrant" indicates which class from which the file is inherited. Now give the source file default.aspx code:

<% @ Page language = "c #" codebehind = "default.aspx.cs" inherits = "Vagrant.myvagrant"%>

Default.aspx.cs file code Note: Required compilation / * Function Description: DEFAULT.ASPX.CS is used to separate the UI with the source code. This is the code section, which needs to be compiled by Vagrant 2001.10.17 Person: http: // Www.weavedream.net * /

File: // Introduced the necessary namespace Using System; use system.data; using system.data.sqlclient; file: // To introduce the namespace necessary to use the SQL database Using System.Web.ui; use System.Web.ui .WebControls; using system.web.ui.htmlControls; Namespace Vagrant File: // Create your own namespace {

Public class myvagrant: Page File: // My class Myvagrant is {Protected DataGrid MyDataGrid inherited from class system.Web.ui.page class;

Private Void Page_Load (Object Sender, System.EventArgs E) // Loading initialization, check the page is being processed for the first time {

// Step: Establish a database connection, use the object-oriented view is an instance of a SQL database joint object, and initialize SqlConnection myconnection = new SQLConnection ("Server = (local); database = data; uid = sa; pwd = ");

File: // Step 2: Read the database to object-oriented the object to create an instance of a query object and initialize SqlDataAdapter mycommand = new sqldataadapter ("Select * from stay", myConnection;

File: //: Store data Dataset DS = new dataset (); mycommand.fill (DS, "Vagrant"); // Step 4: Binding data MyDataGrid.dataSource = DS.TABLES ["Vagrant" ] .DefaultView mydatagrid.database ();

}

}

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

New Post(0)