六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

asp.net高級圖文詳細教程(二)- 轉換編程思維

[摘要]上次的內容說過asp.net和asp的最大區別在于編程思維的轉換,那么我們現在就來看看如何轉換編程思想。以前的web編程從cgi(perl)到asp,php,jsp的編程過程都是這樣:美工人員給出頁...
上次的內容說過asp.net和asp的最大區別在于編程思維的轉換,那么我們現在就來看看如何轉換編程思想。以前的web編程從cgi(perl)到asp,php,jsp的編程過程都是這樣:美工人員給出頁面原型,編程人員照頁面填空,最后堆起來算完,下次如果原型變動,那么就再修改程序,這樣業務邏輯和html頁面混在一起,可以說是事倍功半。那么,現在有了asp.net,我們應該怎么做呢?

    讓我們找個實際的例子,就拿論壇來說吧,先從頂至下看看它的業務邏輯。我們可以把一個論壇視做一個對象,它有自己的屬性和方法,常見的屬性有名稱、貼子數、用戶數、版面數等等,這樣的話,我們就可以這樣來構造論壇對象:

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) , 我將在下節的內容里詳細解釋。




主站蜘蛛池模板: 日日摸夜夜添夜夜添一区二区 | 日韩视频福利 | 日本免费色 | 又粗又硬又大又爽免费观看 | 欧美怡红院免费全部视频 | 亚洲国产精久久久久久久 | 色吧亚洲欧美另类 | 在线a网站| 亚洲男人的天堂网站 | 日本a视频在线 | 三级自拍 | 欧美无人区码卡二卡3卡4免费 | 日日噜噜噜夜夜爽爽狠狠69 | 伊在线视频 | 欧美在线观看视频网站 | 在线观看亚洲免费视频 | 亚洲欧美综合在线观看 | 在线观看91精品国产剧情免费 | 一级做a爰片久久免费 | 日韩中文一区宇都宫紫苑 | 中文字幕在线免费视频 | 天天想夜夜操 | 色狠狠综合网 | 婷婷伊人五月 | 亚洲第一色网 | 日日影视 | 四虎精品影院在线观看视频 | 亚洲 欧美 中文 日韩欧美 | 伊人五月综合 | 日本老年人精品久久中文字幕 | 图片区亚洲 | 青草视频在线观看国产 | 四虎8848精品永久在线观看 | 亚洲香蕉中文网 | 亚洲欧洲国产精品你懂的 | 青春草视频在线观看免费 | 速度与激情9免费观看 | 亚洲福利在线视频 | 天天射天天草 | 欧美一区色 | 亚洲主播自拍 |