asp.net高级教程(二)--- 转换编程思维
点击次数:25 次 发布日期:2008-11-26 16:20:25 作者:源代码网
|
源代码网推荐 源代码网推荐 让我们找个实际的例子,就拿论坛来说吧,先从顶至下看看它的业务逻辑。我们可以把一个论坛视做一个对象,它有自己的属性和方法,常见的属性有名称、贴子数、用户数、版面数等等,这样的话,我们就可以这样来构造论坛对象: 源代码网推荐 源代码网推荐 namespace MyOwnClass 源代码网推荐 { 源代码网推荐 using System; 源代码网推荐 using System.Data.SQL ; 源代码网推荐 using System.Data ; 源代码网推荐 源代码网推荐 //////////////////////////////////////////////////////////////////// 源代码网推荐 // 源代码网推荐 // Class Name : BBS 源代码网推荐 // 源代码网推荐 // Description: 论坛类,构造一个论坛对象 源代码网推荐 // 源代码网推荐 // date: 2000/02/03 源代码网推荐 // 源代码网推荐 /// //////////////////////////////////////////////////////////////// 源代码网推荐 public class BBS 源代码网推荐 { 源代码网推荐 //私有变量 源代码网推荐 private string m_strTitle ; //bbs名称 源代码网推荐 private int m_intForumCount ; //版面数 源代码网推荐 private int m_intTopicCount ; //贴子数 源代码网推荐 private int m_intUserCount ; //注册用户数 源代码网推荐 源代码网推荐 //属性 源代码网推荐 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 ; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 //构造函数 源代码网推荐 public BBS(string a_strTitle) 源代码网推荐 { 源代码网推荐 // 源代码网推荐 // TODO: Add Constructor Logic here 源代码网推荐 // 源代码网推荐 m_strTitle = a_strTitle ; 源代码网推荐 源代码网推荐 //读取数据库 源代码网推荐 MyConnection myConn = new MyConnection() ; 源代码网推荐 SQLCommand myCommand = new SQLCommand() ; 源代码网推荐 myCommand.ActiveConnection = myConn ; 源代码网推荐 myCommand.CommandText = "up_GetBBSInfo" ; //调用存储过程 源代码网推荐 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("表或存储过程不存在")) ; 源代码网推荐 } 源代码网推荐 源代码网推荐 //清场 源代码网推荐 myReader.Close(); 源代码网推荐 myConn.Close() ; 源代码网推荐 } 源代码网推荐 catch(SQLException e) 源代码网推荐 { 源代码网推荐 throw(new Exception("数据库出错:" + e.Message)) ; 源代码网推荐 } 源代码网推荐 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 这个bbs类很简单,有四个私有变量,对应四个只读属性,方法只有一个带参数的构造函数,作用是从数据库中读取相应的数据,填充四个私有变量。类构造好了,让我们看看如何使用,在需要显示论坛这些属性的页面文件里(.aspx)里,构造四个Label,象这样: 源代码网推荐 <table width=140 cellpadding=4 cellspacing=1 border=0> 源代码网推荐 <tr> 源代码网推荐 <td class=cn> 源代码网推荐 <font color=white>注册用户数:</font> 源代码网推荐 </td> 源代码网推荐 <td> 源代码网推荐 <asp:label id="lblUserCount" runat=Server class=cn></asp:label> 源代码网推荐 </td> 源代码网推荐 </tr> 源代码网推荐 <tr> 源代码网推荐 <td class=cn> 源代码网推荐 <font color=white>贴子总数:</font> 源代码网推荐 </td> 源代码网推荐 <td> 源代码网推荐 <asp:label id="lblTopicCount" runat=Server class=cn></asp:label> 源代码网推荐 </td> 源代码网推荐 </tr> 源代码网推荐 <tr> 源代码网推荐 <td class=cn> 源代码网推荐 <font color=white>版面数:</font> 源代码网推荐 </td> 源代码网推荐 <td> 源代码网推荐 <asp:label id="lblForumCount" runat=Server class=cn></asp:label> 源代码网推荐 </td> 源代码网推荐 </tr> 源代码网推荐 </table> 源代码网推荐 然后在对应的c#文件里这样使用: 源代码网推荐 源代码网推荐 protected void Page_Init(object sender, EventArgs e) 源代码网推荐 { 源代码网推荐 // 源代码网推荐 // CODEGEN: This call is required by the ASP+ Windows Form Designer. 源代码网推荐 // 源代码网推荐 InitializeComponent(); 源代码网推荐 源代码网推荐 //初始化页面对象 源代码网推荐 //创建bbs对象 源代码网推荐 try 源代码网推荐 { 源代码网推荐 m_objBBS = new BBS("鹰翔山庄论坛") ; 源代码网推荐 } 源代码网推荐 catch(Exception exp) 源代码网推荐 { 源代码网推荐 #if DEBUG 源代码网推荐 Response.Write ("初始化bbs对象出错:" + exp.Message + "<br>") ; 源代码网推荐 return ; 源代码网推荐 #endif//DEBUG 源代码网推荐 Server.Transfer("error.aspx") ; 源代码网推荐 } 源代码网推荐 源代码网推荐 //论坛名称 源代码网推荐 lblBBSName.ForeColor = Color.White ; 源代码网推荐 lblBBSName.Text = m_objBBS.Title ; 源代码网推荐 源代码网推荐 //用户数 源代码网推荐 lblUserCount.ForeColor = Color.White ; 源代码网推荐 lblUserCount.Text = m_objBBS.UserCount.ToString() ; 源代码网推荐 源代码网推荐 //文章数 源代码网推荐 lblTopicCount.ForeColor = Color.White ; 源代码网推荐 lblTopicCount.Text = m_objBBS.TopicCount.ToString() ; 源代码网推荐 源代码网推荐 //版面数 源代码网推荐 lblForumCount.ForeColor = Color.White ; 源代码网推荐 lblForumCount.Text = m_objBBS.ForumCount.ToString() ; 源代码网推荐 } 源代码网推荐 源代码网推荐 看出这样使用的好处吗?对,就是业务逻辑和html代码分开,这样无论页面原型如何修改,代码都不需要做丝毫改动。bbs对象构造好了,让我们看看论坛的其他对象,他们分别是用户(BBSUser)、版面(Forum)和贴子(Topic) , 我将在下节的内容里详细解释 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
