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;

public partial class WTOSYSadmin_news_newadd2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
        if (!IsPostBack)
        {
            SqlConnection conn = CDatabase.wtoconn();
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select * from NewsCategory", conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            this.lbname.DataSource = ds;
            this.lbname.DataTextField = "BigCategory";
            this.lbname.DataValueField = "id";
            this.lbname.DataBind();
            conn.Close();
        }
            if (Request.QueryString["doing"] != null)
            {
                switch (Request.QueryString["doing"])
                {
                    case "add":
                        this.databind();
                        break;
                    case "edit":
                            this.databind2();  
                        this.addedit.Click += new EventHandler(editclick);
                            break;
                }
            }
            else
            {
                Response.Redirect("~/SYSadmin/news/newsmag.aspx");
            }

        



    }
    protected void databind()
    {
        this.title.Text = "文章添加";
        this.addedit.Click += new EventHandler(addclick);
    }
    protected void databind2()
    {
        if (!IsPostBack)
        {
          
            this.title.Text = "文章编辑";
            SqlConnection conn2 = CDatabase.wtoconn();
            conn2.Open();
            SqlCommand smd = new SqlCommand("select * from news where id=" + Request.QueryString["id"], conn2);
            SqlDataReader sdr = smd.ExecuteReader();
            sdr.Read();
            this.lbname.SelectedValue =Convert.ToString(sdr.GetInt32(1));
            this.newstitle.Text = sdr.GetString(3);
            this.newsource.Text = sdr.GetString(8);
            this.Author.Text = sdr.GetString(9);
            this.Content.Text = Server.HtmlDecode(sdr.GetString(6));
            string picsele = sdr.GetString(10);

            if (sdr.GetString(5) == "y")
            {
                this.y.Checked = true;
            }
            else
            {
                this.n.Checked = true;
            }
            sdr.Close();
            conn2.Close();
            RegisterStartupScript("", "<script>picsele('" + picsele + "');<" + "/script>");
        }
    }


    protected void addclick(object sender, EventArgs e)
    {
        string biglbid, biglbname, adddate, isfp,  Content, times, Source, Author;
        biglbid = this.lbname.SelectedItem.Value;
        biglbname = this.lbname.SelectedItem.Text;
        if (y.Checked == true)
        {
            isfp = "y";
        }
        else
        {
            isfp = "n";
        }
        times = "1";

        Source = this.newsource.Text;
        Author = this.Author.Text;
        Content = Server.HtmlEncode(this.Content.Text.Replace("'", "''"));

        string sql1 = "insert into news(biglbid,biglbname,title,adddate,isfp,Content,times,Source,Author,picsmall)";
        sql1 += " values(" + biglbid + ",'" + biglbname + "','" + this.newstitle.Text + "','" + DateTime.Now.ToString() + "','" + isfp + "','" + Content + "'," + times + ",'" + Source + "','" + Author + "','" + Request["pic"].ToString() + "')";
        if (Doadmin.Exec(sql1))
        {
            Response.Write("<script>alert('添加成功');location.href('../news/newsmag.aspx')</script>");

        }
        else
        {
            Response.Write("<script>alert('此次操作失败')</script>");
        }
    }
    protected void editclick(object sender, EventArgs e)
    {
        string biglbid1, biglbname1, adddate1, isfp1, Content1, times1, Source1, Author1;
        biglbid1 = this.lbname.SelectedItem.Value;
        biglbname1 = this.lbname.SelectedItem.Text;
        if (y.Checked == true)
        {
            isfp1 = "y";
        }
        else
        {
            isfp1 = "n";
        }
        Source1 = this.newsource.Text;
        Author1 = this.Author.Text;
        Content1 = Server.HtmlEncode(this.Content.Text.Replace("'", "''"));

        string sql1 = "update news set biglbid=" + biglbid1 + ",biglbname='" + biglbname1 + "',title='" + this.newstitle.Text + "',adddate='" + DateTime.Now.ToString() + "',isfp='" + isfp1 + "',Content='" + Content1 + "',Source='" + Source1 + "',Author='" + Author1 + "',picsmall='"+Request["pic"].ToString()+"' where id=" + Request.QueryString["id"];
        if (Doadmin.Exec(sql1))
        {
            Response.Write("<script>alert('修改成功');location.href('../news/newsmag.aspx')</script>");

        }
        else
        {
            Response.Write("<script>alert('此次操作失败')</script>");
        }
    }
}
