using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

public partial class hpfb : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated == false)
        {
            Response.Redirect("login.aspx?url=hpfb");
        }

    }

    protected void FormView1_ItemInserting(object sender,FormViewInsertEventArgs e)
    {
        FileUpload FileObj;
        SqlDataReader Dr;
        String SqlStr;
        basesql SqlObj = new basesql();
        String thePath = Server.MapPath("wppic");
        FileObj = (FileUpload)FormView1.FindControl("pic");
        if (FileObj.HasFile)
        {
            if (!Directory.Exists(thePath))
            {
                Directory.CreateDirectory(thePath);
            }
            FileObj.SaveAs(thePath + "\\" + FileObj.FileName);
            e.Values["wppic"] = FileObj.FileName;
        }
        e.Values["userid"] = HttpContext.Current.User.Identity.Name.ToString();
        DropDownList theDrop;
        theDrop = (DropDownList)FormView1.FindControl("drop1");
        String theSel = theDrop.SelectedValue;
        e.Values["wptype"] = theSel;


        String NewWuID = System.DateTime.Now.Year.ToString() + ((System.DateTime.Now.Month < 10) ? ("0" + System.DateTime.Now.Month.ToString()) : System.DateTime.Now.Month.ToString()) + ((System.DateTime.Now.Day < 10) ? ("0" + System.DateTime.Now.Day.ToString()) : System.DateTime.Now.Day.ToString());
        SqlStr = "select * from wu_info where left(wuid,8)='" + NewWuID + "' order by wuid desc";
        Dr = SqlObj.ExecuteReader(SqlStr);
        if (Dr.Read())
        {
            String Str1;
            int i;
            int j;
            Str1 = (int.Parse(Dr["wuid"].ToString().Substring(8, 4)) + 1).ToString();
            j = Str1.Length;
            for (i = 0; i < 4 - j; i++)
            {
                Str1 = "0" + Str1;
            }
            NewWuID = NewWuID + Str1;
        }
        else
        {
            NewWuID = NewWuID + "0001";
        }
        e.Values["wpid"] = NewWuID;
        Dr.Close();

    }

    protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "alert('数据添加成功！');window.location='hpfb.aspx';", true);
    }
}
