ClubWEBSite starter kit二次开发及Vs2005中ObjectDataSource控件的应用小记
Vs2005给我们带来很多优秀的初学者工具包,说是说初学者工具包,以我的心得来看,其实这些工具包应该是我们了解和熟悉vs2005以及.net2.0新特性最好的指导性工具包,不同层次的程序员都可以从中得到收获。
在拿到vs2005的第一时间,我就应用其中的Club Web Site 工具包,经过简单的本地化,包括:将所有的标签文字统一到资源文件;加入HtmlEditTextBox;并加强编辑器提供图片及附近上传功能;所有Club原始工具包中未完成的静态文本部分实现数据库管理。经过以上修改,基本可以满足我的基本需求和应用。
其中静态文本动态化(暂且这么称呼),因为是初次使用vs2005,所以全部应用SqlDataSource控件作为数据提供中间件,配合FormView,拖拖拽拽就OK达,感觉真是非常方便和快捷、高效。这里唯一需要注意的就是,如果你采取完全交由SqlDataSource控件来处理数据的获取,那么SqlDataSource控件会在UI层生成大量的SQL字符串,如相应的SELECT、INSERT等语句,造成过多的暴露数据库中表结构信息。因此,建议即使使用SqlDataSource控件,仍须配合存储过程为佳。
随着对站点功能的加强,很希望访客可以在小站留下自己的观感,所以萌发达在Club Web Sit工具包二次开发加入留言板功能的想法,心动不如行动,说干就干。
首先,决定采用ObjectDataSourc控件,采取3层架构来实现留言板功能模块:设计和数据库表结构、相应存储过程。举例说明:
 /// <summary>
        /// <summary>2
 /// 获取指定留言信息
        /// 获取指定留言信息3
 /// </summary>
        /// </summary>4
 /// <param name="id"></param>
        /// <param name="id"></param>5
 /// <returns></returns>
        /// <returns></returns>6
 public static List<Leaveword> GetLeaveword(int id , int mode)
        public static List<Leaveword> GetLeaveword(int id , int mode)7
 {
        {8
 using (SqlConnection sqlConnection = new SqlConnection(ENConfiguration.GetConnectionString()))
            using (SqlConnection sqlConnection = new SqlConnection(ENConfiguration.GetConnectionString()))9
 {
            {10
 using (SqlCommand sqlCommand = new SqlCommand("GetLeavewordByID", sqlConnection))
                using (SqlCommand sqlCommand = new SqlCommand("GetLeavewordByID", sqlConnection))11
 {
                {12
 sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.CommandType = CommandType.StoredProcedure;13
 sqlCommand.Parameters.Add(new SqlParameter("@ID", id));
                    sqlCommand.Parameters.Add(new SqlParameter("@ID", id));14
 sqlCommand.Parameters.Add(new SqlParameter("@Mode", mode));
                    sqlCommand.Parameters.Add(new SqlParameter("@Mode", mode));15

16
 sqlConnection.Open();
                    sqlConnection.Open();17
 using (SqlDataReader dataReader = sqlCommand.ExecuteReader())
                    using (SqlDataReader dataReader = sqlCommand.ExecuteReader())18
 {
                    {19
 List<Leaveword> list = new List<Leaveword>();
                        List<Leaveword> list = new List<Leaveword>();20
 if (dataReader.Read())
                        if (dataReader.Read())21
 {
                        {22
 int currentID = (int)dataReader["ID"];
                            int currentID = (int)dataReader["ID"];23
 string title = (string)dataReader["Title"];
                            string title = (string)dataReader["Title"];24
 string description = (string)dataReader["Description"];
                            string description = (string)dataReader["Description"];25
 DateTime publishDate = (DateTime)dataReader["PublishDate"];
                            DateTime publishDate = (DateTime)dataReader["PublishDate"];26
 string guest = (string)dataReader["guest"];
                            string guest = (string)dataReader["guest"];27

28
 Leaveword leaveword = new Leaveword(currentID, title, description, publishDate, guest);
                            Leaveword leaveword = new Leaveword(currentID, title, description, publishDate, guest);29

30
 list.Add(leaveword);
                            list.Add(leaveword);31

32
 }
                        }33
 return list;
                        return list;34
 }
                    }35
 }
                }36
 }
            }37
 }
        }  此处GetLeaveword方法返回List<Leaveword>对象,正是为配合ObjectDataSource的要求而定义,这样ObjectDataSource作为其他数据绑定控件时,才能正确工作。
  具体效果可以访问:http://www.tianyuanedu.net/smartweb/Leavewords/default.aspx
  我这里仅是抛砖引玉,希望可以看到更多好的案例及Starter kit应用。
 
                    
                     
                    
                 
                    
                
 


 
             
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号