熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET項目開發指南:房間信息的添加(2)[1]

2013-11-13 12:28:20  來源: .NET編程 

    房間信息的添加(

  ST_RoomAddModuleascxcs的主要代碼及其解釋

  Page_Load事件加載下拉列表框的數據並進行綁定IsIdValidate()方法用來驗證房間號是否登記如程序所示

  程序  ST_RoomAddModuleascxcs

    public partial class ST_RoomAddModule : ST_ModuleBase

    {

        private void Page_Load(object sender SystemEventArgs e)

        {

            //綁定房間類型下拉列表框

            if(!IsPostBack)

            {

                //從文件WebConfig中讀取連接字符串

                string ST_sqldb=

            ConfigurationSettingsAppSettings[ConnectionString];

                //連接ST_GinShopManage數據庫

                SqlConnection ST_Conn= new SqlConnection (ST_sqldb)

                ST_ConnOpen ()

                //定義SQL語句

                string ST_mysql=select ST_RCategoryIdST_Name from

                    ST_RoomCategory ;

                SqlCommand ST_cm=new SqlCommand  (ST_mysqlST_Conn)

                SqlDataReader dr=ST_cmExecuteReader ()

                while(drRead ())

                {

                    ListItem li=new

    ListItem(dr[ST_Name]ToString()dr[ST_RCategoryId]ToString())

                    RCategoryNameListItemsAdd (li)

                }

                ST_ConnClose ()

            }

        }

        //驗證房間號是否已登記

        public void IsIdValidate(object source

            SystemWebUIWebControlsServerValidateEventArgs args)

        {

            //從文件WebConfig中讀取連接字符串

            string ST_sqldb=

                ConfigurationSettingsAppSettings[ConnectionString];

            //連接ST_GinShopManage數據庫

            SqlConnection ST_Conn= new SqlConnection (ST_sqldb)

            ST_ConnOpen ()

            //構造SQL語句該語句在RoomCategory表中檢查房間類型是否已存在

            string checksql= select * from ST_RoomsInfo where

                ST_RoomId=+RoomIdTextBoxTextTrim() +;

            //創建Command對象

            SqlCommand ST_mycommand=new SqlCommand(checksqlST_Conn)

            //執行ExecuteReader()方法

            SqlDataReader dr=ST_mycommandExecuteReader()

            if(drRead ())

            {

                argsIsValid =false;//房間號已存在

            }

            else

            {

                argsIsValid =true;//房間號未登記

            }

            //關閉連接

            ST_ConnClose()

        }

    …

  【代碼說明】當頁面被首次加載時將讀取ST_RoomCategory表中的ST_RCategoryId和ST_Name列把讀出的ST_RCategoryId作為ListItem類型的li的Value屬性把ST_Name作為li的Text屬性然後再添加到RCategoryNameListItems的集合中這些都是給頁面中的DropDownList控件添加Item

  說明Trim()方法是針對字符類型的操作方法主要是去除字符串前後多余的空格

[]  []  


From:http://tw.wingwit.com/Article/program/net/201311/15971.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.