/*
'' ============================================================================================
'' 项目名称：风越项目范例ASPX普通三层结构
'' 项目版本：V1.0
'' 项目描述：风越项目范例，用于建立ASPX普通三层结构
'' 文件名称：ChengShi_add.aspx
'' 文件描述：信息增改文件 （城市）
'' 公司名称：中极网盛
'' 开发人员：刘怒
'' 创建日期：2008-12-25 20:23:50
'' 修订日期：2009-01-14 12:12:15
'' 版权信息：Copyright (C) 2008

'' 项目升级时需读取以下生成工具的版权信息
'' 生成工具：风越代码生成器 [FireCode Creator]
'' 当前版本：通用版 V1.5
'' 官方网站：http://www.sino8848.com
'' ============================================================================================
*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FypSystem.Common;
using FypSystem.DBManager;

namespace Fyp_3TierCommon.Web
{
    public partial class ChengShi_add : System.Web.UI.Page
    {
        /// <summary>
        /// 当前页面是否编辑状态
        /// </summary>
        private bool bIsEdit = false;
        
        int req_CITY_ID; 
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = WebPage.GetConfig("fypWebName");
            if (!StringExtend.IsEmpty(this.Request.QueryString["CITY_ID"]))
            {
            
                
                
                req_CITY_ID = int.Parse(this.Request.QueryString["CITY_ID"]);
                

                
                
            
                bIsEdit = true;
            }

            if (!this.Page.IsPostBack)
            {
                Initialize();
            }
        }

        /// <summary>
        /// 编辑记录时，读取数据库指定记录
        /// </summary>
        /// <returns></returns>
        private Fyp_3TierCommon.Model.Model_CITY GetModelData()
        {
            Fyp_3TierCommon.Model.Model_CITY model_CITY = new Fyp_3TierCommon.Model.Model_CITY();
            if (bIsEdit)
                model_CITY = new Fyp_3TierCommon.BLL.BLL_CITY().GetModel(req_CITY_ID);

            return model_CITY;
        }

        /// <summary>
        /// 导入默认值、数据
        /// </summary>
        private void Initialize()
        {
            //绑定属性
            

            //默认值
            


            //直接填充下拉框
            FypSystem.DBManager.FillCtrlData fillCtrlData = new FillCtrlData();
    
        
            
            
            
            

            Fyp_3TierCommon.BLL.BLL_PROVINCE bll_PROVINCE = new Fyp_3TierCommon.BLL.BLL_PROVINCE();
            DataTable dt_PROVINCE = bll_PROVINCE.GetDataTable("");
            fillCtrlData.FillListControl(this.PROVINCE_ID, dt_PROVINCE, "PROVINCE_ID", "PROVINCE_ID", "--请选择--", "");
            dt_PROVINCE.Dispose();
            
        
    


            //编辑记录时导入数据，并设置相应属性
            if (bIsEdit)
            {
                //设置相应属性
                this.btnSubmit.Text = "修改";

                //编辑时设置只读状态
            

                //导入数据
                Fyp_3TierCommon.Model.Model_CITY model_CITY = GetModelData();
                if (model_CITY != null)
                {
                    try
                    {
        
                        //根据条件填充下拉框
            
        
                        
                        
                        this.CITY_ID.Text = model_CITY.CITY_ID.ToString();
                        

                        
                        

                        

                        
                        
                        
                        

                        
                        
                        
                        this.PROVINCE_ID.Text = model_CITY.PROVINCE_ID.ToString();
                        

                        
                        

                        

                        
                        
                        
                        

                        
                        
                        
                        this.CITY_CN.Text = model_CITY.CITY_CN.ToString();
                        

                        
                        

                        

                        
                        
                        
                        

                        
                        
                        
                        this.CITY_EN.Text = model_CITY.CITY_EN.ToString();
                        

                        
                        

                        

                        
                        
                        
                        

                        
                        
                        
                        this.CITY_AREA_CODE.Text = model_CITY.CITY_AREA_CODE.ToString();
                        

                        
                        

                        

                        
                        
                        
                        

                        
                        
                    }
                    catch (System.Exception err)
                    {
                        FypSystem.Common.WebPage.JsAlert(this, err.Message);
                    }
                }
                else
                {
                    //找不到指定记录时
                    FypSystem.Common.WebPage.JsAlert(this, "未找到指定记录！");
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SaveData();
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            string strMsg = "";
            string strUrl = "";

            
            if (this.Page.IsValid)
            {
                try
                {
                

                    Fyp_3TierCommon.Model.Model_CITY model_CITY = GetModelData();
                
                
                    
                    model_CITY.CITY_ID = int.Parse(this.CITY_ID.Text);
                    

                    
                

                

                    
                    
                
                
                    
                    model_CITY.PROVINCE_ID = int.Parse(this.PROVINCE_ID.Text);
                    

                    
                

                

                    
                    
                
                
                    

                    
                    model_CITY.CITY_CN = this.CITY_CN.Text;
                    
                

                

                    
                    
                
                
                    

                    
                    model_CITY.CITY_EN = this.CITY_EN.Text;
                    
                

                

                    
                    
                
                
                    
                    model_CITY.CITY_AREA_CODE = int.Parse(this.CITY_AREA_CODE.Text);
                    

                    
                

                

                    
                    
                

                    Fyp_3TierCommon.BLL.BLL_CITY bll_CITY = new Fyp_3TierCommon.BLL.BLL_CITY();
                    if (bIsEdit)
                        bll_CITY.Update(model_CITY);
                    else
                        bll_CITY.Add(model_CITY);

                    strMsg = "保存数据成功";
                    strUrl = this.Request.Url.ToString();
                }
                catch (System.Exception err)
                {
                    strMsg = "保存数据时出错：" + err.Message;
                }
            }
            else
            {
                strMsg = "输入信息不正确";
            }
            //FypSystem.Common.WebPage.JsAlert(this, strMsg, strUrl);
            FypSystem.Common.WebPage.JsRun(this, "ShowMsg(\"" + FypSystem.Common.WebPage.FormatJsStr(strMsg) + "\");try{window.opener.location.reload(); window.close();}catch(e){GoUrl('" + strUrl + "')}");
        }

    

    
    }
}