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 System.Data.SqlClient;
using System.Configuration;
using System.IO;

namespace norke
{
    public partial class zs_update : System.Web.UI.Page
    {
        public static string ImgName;
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["norkeyangjian"];
            if (cookie == null)
            {
                Response.Redirect("~/login.aspx");
                return;
            }
            if (!IsPostBack)
            {
                this.DateTimePicker1.Value = DateTime.Now;
                csf();
                setSelectClass();
                load();

            }

        }
        private void load()
        {
            string strID = Request.QueryString["id"].ToString();
            DBConn myDB = new DBConn();
            string sql = "select * from zhaoshang  where hyname='" + Request.Cookies["norkeyangjian"].Value + "' and  id=" + strID;
            SqlDataReader dr = myDB.getDataReader(sql);
            if (dr.Read())
            {
                ViewState["ID"] = dr["ID"].ToString();
                TextBox1.Text = dr["zname"].ToString();
                DropDownList2.SelectedValue = dr["zhezuo"].ToString();
                TextBox2.Text = dr["zztze"].ToString();
                ImgName = dr["zimg"].ToString();
                xiangmujianjie.Text = dr["zgaikuang"].ToString();
                shichang.Text = dr["zscfx"].ToString();
                jingji.Text = dr["zjjxy"].ToString();

            }
            dr.Close();
            myDB.Close();
        }
        private void setSelectClass()//绑定 类别选框
        {
            DBConn myDB = new DBConn();
            string mySql = "select CID,CName from Category";
            DropDownList3.DataSource = myDB.getDataReader(mySql);
            DropDownList3.DataTextField = "CName";
            DropDownList3.DataValueField = "CID";
            DropDownList3.DataBind();
            myDB.Close();
        }
        private void csf()//绑定 类别选框
        {
            DBConn myDB = new DBConn();
            string mySql = "select ID,CName from csf";
            DropDownList1.DataSource = myDB.getDataReader(mySql);
            DropDownList1.DataTextField = "CName";
            DropDownList1.DataValueField = "ID";
            DropDownList1.DataBind();
            myDB.Close();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] == "")
            {
                Page.RegisterStartupScript("msgOnlyAlert", "<script>alert(修改失败');</script>");
                return;
            }
            if (uploadFile.PostedFile.FileName.Trim() != String.Empty && (Path.GetExtension(uploadFile.PostedFile.FileName) != ".gif" && Path.GetExtension(uploadFile.PostedFile.FileName) != ".jpg"))
            {
                Response.Write("<Script>alert('上传的图片格式必须为gif或jpg!!')</Script>");
                return;
            }       

            try
            {
                if (uploadFile.PostedFile.FileName.Trim() != String.Empty && (Path.GetExtension(uploadFile.PostedFile.FileName) != ".gif" && Path.GetExtension(uploadFile.PostedFile.FileName) != ".jpg"))
                {
                    Response.Write("<Script>alert('上传的图片格式必须为gif或jpg!!')</Script>");
                    return;
                }
                if (uploadFile.PostedFile.FileName.Trim() != String.Empty)
                {
                    if (ImgName != "showimg.gif")
                    {
                        System.IO.File.Delete(base.Server.MapPath(".\\zhaoshang\\") + ImgName);
                    }
                    string nam = uploadFile.PostedFile.FileName;
                    //取得文件名(抱括路径)里最后一个"."的索引
                    int i = nam.LastIndexOf(".");
                    //取得文件扩展名
                    string newext = nam.Substring(i);
                    //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
                    DateTime now = DateTime.Now;
                    string newname = now.DayOfYear.ToString() + uploadFile.PostedFile.ContentLength.ToString();
                    uploadFile.PostedFile.SaveAs(Server.MapPath(".\\zhaoshang\\" + newname + newext));
                    string imgname = newname + newext;
                    string strID = Request.QueryString["id"].ToString();
                    string sql = "update zhaoshang set zname='" + TextBox1.Text.Trim() + "',zdiqu='" + DropDownList3.SelectedValue + "',zspfl='" + DropDownList1.SelectedValue + "',zjzrg='" + DateTimePicker1.Value + "',zgaikuang='" + xiangmujianjie.Text.Trim() + "',zhezuo='" + DropDownList2.SelectedItem + "',zztze='" + TextBox2.Text.Trim() + "',zscfx='" + shichang.Text.Trim() + "',zjjxy='" + jingji.Text.Trim() + "',zimg='" + imgname+ "' where id=" + strID;
                    DBConn myDB = new DBConn();
                    myDB.Update(sql);
                    myDB.Close();
                    Response.Redirect("zs_Manage.aspx");
                }
                else
                {
                    string strID = Request.QueryString["id"].ToString();
                    string sql = "update zhaoshang set zname='" + TextBox1.Text.Trim() + "',zdiqu='" + DropDownList3.SelectedValue + "',zspfl='" + DropDownList1.SelectedValue + "',zjzrg='" + DateTimePicker1.Value + "',zgaikuang='" + xiangmujianjie.Text.Trim() + "',zhezuo='" + DropDownList2.SelectedItem + "',zztze='" + TextBox2.Text.Trim() + "',zscfx='" + shichang.Text.Trim() + "',zjjxy='" + jingji.Text.Trim() + "' where id=" + strID;
                    DBConn myDB = new DBConn();
                    myDB.Update(sql);
                    myDB.Close();
                    Response.Redirect("zs_Manage.aspx");
                }
            }
            catch
            {
            }
        }
    }
}