Object-oriented programming ideas in asp.net

Source: Internet
Author: User
Tags database join inheritance join
Asp.net| Programming | objects
First of all, let's talk about object-oriented programming ideas. I think the current mainstream programming ideas are nothing more than two kinds: structure and object oriented. In the past, in the ASP, we fully adopted the structure of the idea. Now, ASP.net can fully support object-oriented programming ideas, which has to say is a qualitative leap.

I remember writing to you about six months ago. << object-oriented from man-made earth to talk about >&gt, at that time because of the inspiration to see a novel, found that anything can be viewed with object-oriented thinking. I would like to talk about it now, object-oriented is simply the world as a class, to use the class to do things, you must create an instance of the class. This is very good, for example, a person is a class, we can not say that people go to work (this is not targeted), we often say called John or Dick go, this three or dick is an example of this class of people. This is the concept of the object. Again, the incident and the action, for example: The wind, rain, to accept the clothes. The wind, rain is an event, this event will stimulate the movement of clothing, this action is the performer of the object. If you've learned about SQL Server triggers, I think it will be clearer. Object-oriented is also more important than inheritance (Inherits) and polymorphism. This is very good understanding, for example, John will accept clothes, Zhang Sanseng's son inherits the characteristics of John, also will accept clothes, this is inheritance, and perhaps John Son also can drive, this is polymorphic.

Well, for object-oriented understanding, I'm just saying that, or how to use the object-oriented approach to programming in asp.net. When we're working on a Web program, we usually have to work with the UI (the user interface, the display of the data) and the code (the code that handles the data). To achieve a complete separation of the UI from the code, we treat the UI as an object and code as another object. Of course we have to study the relationship between these two objects, remember that we are in the ASP program to copy the same piece of code to many pages, it seems that the code is the parent class, the UI is a subclass. But they all inherited from System.Web.UI.Page. To sort out the relationship between the classes, it's not difficult to understand the idea that code UI is separate from coding.
Diagram:
Introducing namespaces: Using System.Web.UI
Parent class page→ Many subclass code (*.cs file) → Many sub-subclass UI (*.aspx file)

Because I mainly want everyone to understand this kind of programming thought, so I just give a simple example, I think we have a thought, and then find more examples to see.

File Two:
UI class: Default.aspx is primarily used to display data, using the day DataGrid control
Code class: Default.aspx.cs is primarily used to establish data joins, queries, and bind data to the DataGrid control
Inside I created my own namespace called vagrant, and the class Myvagrant
Database: Data, which has a relational table student

To illustrate that the Default.aspx class was inherited from Default.aspx.cs, it is necessary to declare 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 the file inherits from.

Now give the source file
Default.aspx code:

<%@ Page language= "C #" codebehind= "Default.aspx.cs" inherits= "vagrant. Myvagrant "%>
<HTML>
<HEAD>
</HEAD>
<body>
<form id= "Form1" method= "POST" runat= "Server" >
<asp:datagrid id= "Mydatagrid" runat= "Server"
Style= "Z-INDEX:101; left:197px; Position:absolute; Top:48px "
Borderstyle= "Ridge" gridlines= "None" borderwidth= "2px" bordercolor= "white"
Backcolor= "White" cellpadding= "3" cellspacing= "1" width= "494px" height= "143px" >
<itemstyle horizontalalign= "Center" forecolor= "Black" verticalalign= "middle" backcolor= "#DEDFDE"/>
</asp:datagrid>
</form>
</body>
</HTML>

Default.aspx.cs File code Note: need to compile
/*
Feature Description: Default.aspx.cs is used to separate the UI from the source code, which is part of the code that requires compiling
Created by vagrant
2001.10.17
Personal homepage: http://www.weavedream.net
*/


Introducing the required namespaces
Using System;
Using System.Data;
Using System.Data.SqlClient; Introducing the namespaces necessary to use SQL databases
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;

namespace vagrant//Create your own namespace
{

The public class Myvagrant:P the age//My class Myvagrant is inherited from the class System.Web.UI.Page class.
{
protected DataGrid Mydatagrid;


private void Page_Load (object sender, System.EventArgs e)/page mount initialization to check if the page is being processed for the first time
{

The first step: Establish a database join, using an object-oriented view is to create an instance of the SQL database join object, and initialize
SqlConnection myconnection=new SqlConnection ("server= (local);D atabase=data; Uid=sa; pwd=; ");

Step two: Read the database, and the object-oriented view is to create an instance of the query object and initialize the
SqlDataAdapter Mycommand=new SqlDataAdapter ("select * from Student", MyConnection);

: Step three: Storing data
DataSet ds=new DataSet ();
Mycommand.fill (ds, "vagrant");
Step Fourth: Binding data
Mydatagrid.datasource =ds. Tables ["vagrant"]. DefaultView
Mydatagrid.databind ();

}


}
}



Related Article

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.