The last article said that the biggest difference between Asp.net and ASP is the conversion of programming thinking. Now let's look at how to convert programming ideas. The previous web programming process from CGI (Perl) to ASP, PHP, and JSP is like this: the artist gives the page prototype, the programmer fills in the blank according to the page, and finally completes the heap, next time, if the prototype changes, you can modify the program. In this way, the business logic is mixed with the HTML page. So what should we do with Asp.net?
Let's look for a practical example. Let's take the Forum as an example. Let's first look at its business logic from top to bottom. We can regard a forum as an object. It has its own attributes and Methods. common attributes include name, number of posts, number of users, and number of pages. In this way, we can construct the Forum object as follows:
Namespace myownclass
{
Using system;
Using system. Data. SQL;
Using system. Data;
//////////////////////////////////////// ////////////////////////////
//
// Class Name: BBS
//
// Description: Forum class, which constructs a forum object.
//
// Date: 2000/02/03
//
//////////////////////////////////////// ///////////////////////////
Public class BBS
{
// Private variable
Private string m_strtitle; // bbs name
Private int m_intforumcount; // Number of pages
Private int m_inttopiccount; // Number of posts
Private int m_intusercount; // number of registered users
// Attributes
Public String title
{
Get
{
Return m_strtitle;
}
}
Public int forumcount
{
Get
{
Return m_intforumcount;
}
}
Public int topiccount
{
Get
{
Return m_inttopiccount;
}
}
Public int usercount
{
Get
{
Return m_intusercount;
}
}
// Constructor
Public BBS (string a_strtitle)
{
//
// Todo: Add constructor logic here
//
M_strtitle = a_strtitle;
// Read the database
Myconnection myconn = new myconnection ();
Sqlcommand mycommand = new sqlcommand ();
Mycommand. activeconnection = myconn;
Mycommand. commandtext = "up_getbbsinfo"; // call the Stored Procedure
Mycommand. commandtype = commandtype. storedprocedure;
Try
{
Myconn. open ();
Sqldatareader myreader;
Mycommand. Execute (Out myreader );
If (myreader. Read ())
{
M_intforumcount = (INT) myreader ["forumcount"];
M_inttopiccount = (INT) myreader ["topiccount"];
M_intusercount = (INT) myreader ["usercount"];
}
Else
{
Throw (new exception ("table or stored procedure does not exist "));
}
// Clear the scene
Myreader. Close ();
Myconn. Close ();
}
Catch (sqlexception E)
{
Throw (new exception ("Database Error:" + E. Message ));
}
}
}
}
This BBS class is very simple. There are four private variables corresponding to four read-only attributes. The method has only one constructor with parameters to read the corresponding data from the database and fill in four private variables. Class has been constructed. Let's see how to use it. In the page file (. aspx) that needs to display the attributes of the forum, we construct four labels, like this:
<Table width = 140 cellpadding = 4 cellspacing = 1 border = 0>
<Tr>
<TD class = cn>
<Font color = white> Number of registered users: </font>
</TD>
<TD>
<Asp: Label id = "lblusercount" runat = server class = cn> </ASP: Label>
</TD>
</Tr>
<Tr>
<TD class = cn>
<Font color = white> total number of posts: </font>
</TD>
<TD>
<Asp: Label id = "lbltopiccount" runat = server class = cn> </ASP: Label>
</TD>
</Tr>
<Tr>
<TD class = cn>
<Font color = white> Number of pages: </font>
</TD>
<TD>
<Asp: Label id = "lblforumcount" runat = server class = cn> </ASP: Label>
</TD>
</Tr>
</Table>
Then use the following in the corresponding C # file:
Protected void page_init (Object sender, eventargs E)
{
//
// Codegen: This call is required by the ASP + windows Form Designer.
//
Initializecomponent ();
// Initialize the Page Object
// Create a BBS object
Try
{
M_objbbs = new BBS ("Yingxiang Villa Forum ");
}
Catch (exception exp)
{
# If debug
Response. Write ("An error occurred while initializing the BBS object:" + exp. Message + "<br> ");
Return;
# Endif // debug
Server. Transfer ("error. aspx ");
}
// Forum name
Lblbbsname. forecolor = color. White;
Lblbbsname. Text = m_objbbs.title;
// Number of users
Lblusercount. forecolor = color. White;
Lblusercount. Text = m_objbbs.usercount.tostring ();
// Number of articles
Lbltopiccount. forecolor = color. White;
Lbltopiccount. Text = m_objbbs.topiccount.tostring ();
// Number of pages
Lblforumcount. forecolor = color. White;
Lblforumcount. Text = m_objbbs.forumcount.tostring ();
}
Can you see the benefits of using this feature? Yes, that is, the business logic is separated from the HTML code, so no matter how the page prototype is modified, the Code does not need to be modified. After the BBS object is constructed, let's look at other Forum objects, which are user (bbsuser), layout (forum), and topic ), I will explain it in detail in the next section.