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_piclinkedit : System.Web.UI.Page
{
    public string adurl="";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }
            if(Request.QueryString["doing"]=="edit")
            {
                    if (!IsPostBack)
                    {
                        SqlConnection conn = CDatabase.wtoconn();
                        conn.Open();
                        SqlCommand smd = new SqlCommand("select * from Baseadvertisement where id=" + Request.QueryString["id"], conn);
                        SqlDataReader sdr = smd.ExecuteReader();
                        sdr.Read();
                        this.alt.Text = sdr["alt"].ToString();
                        this.href.Text = sdr["href"].ToString();
                        this.subject.Text = sdr["title"].ToString();
                        this.cd.Text = sdr["cd"].ToString();
                        if (sdr["type"].ToString() == "jpg" || sdr["type"].ToString() == "gif")
                        {
                            this.Image1.ImageUrl = "~/images/AD/" + sdr["pic"].ToString().Trim();
                            this.Panel1.Visible = true;
                        }
                        if (sdr["type"].ToString() == "swf")
                        {
                            adurl = "/images/ad/" + sdr["pic"].ToString().Trim();
                            this.Panel2.Visible = true;
                            Page.DataBind();
                        }
                    }
                    this.Label1.Visible = true;
                    this.LinkButton3.Visible = false;
                    this.LinkButton1.Visible = true;
                    this.Label2.Visible = true;
                    this.Image1.Visible = true;
                    this.LinkButton1.Click += new EventHandler(LinkButton3_Click);
            }
        
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string isfp, title, href;
        href=this.href.Text;
        string picname="";
        string fpath2 = this.img1.PostedFile.FileName;
        string ftype2 = this.img1.PostedFile.ContentType;
        string[] type2 = ftype2.Split('/');
        string S_hz = "";
        S_hz = (fpath2.Substring(fpath2.LastIndexOf(".") + 1)).ToLower().Trim();
        if (S_hz == "swf" || S_hz == "jpg" || S_hz == "gif")
        {
            picname = DateTime.Now.ToString("yyyyssssMMddssss") + "." + S_hz;
            string pt2 = Server.MapPath("~/images/AD/") + picname;
            this.img1.SaveAs(pt2);
            this.img1.Dispose();
            if (Doadmin.Exec("insert into Baseadvertisement(title,pic,href,alt,cd,type) values ('" + this.subject.Text + "','" + picname + "','" + href + "','" + this.alt.Text + "','" + this.cd.Text + "','" + S_hz + "')"))
            {
                Response.Write("<script>alert('添加成功');location.href('BaseAdvertisement.aspx')</script>");
            }
            else
            {
                Response.Write("<script>alert('此次操作失败');</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('格式有误;只允许FLASH(SWF)和图片文件(JPG GIF)');</script>");
        }

    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        string isfp, title, href,picre="";
        href = this.href.Text;
        string fpath3 = this.img1.PostedFile.FileName;
        string S_hz = "";
        if (fpath3 != "")
        {
            string ftype3 = this.img1.PostedFile.ContentType;
            string[] type3 = ftype3.Split('/');
            S_hz = (fpath3.Substring(fpath3.LastIndexOf(".") + 1)).ToLower().Trim();
            if(S_hz=="swf" ||S_hz=="jpg" ||S_hz=="gif"  )
            {
                picre = DateTime.Now.ToString("yyyyssssMMddssss") + "." + S_hz;
                string pt3 = Server.MapPath("~/images/AD/") + picre;
                this.img1.SaveAs(pt3);
                this.img1.Dispose();        
                if (Doadmin.Exec("update Baseadvertisement set title='"+this.subject.Text+"',href='" + href + "',alt='" + this.alt.Text + "',pic='" + picre + "',cd='"+this.cd.Text+"',type='"+S_hz+"'  where id=" + Request.QueryString["id"]))
                {
                Response.Write("<script>alert('发布成功');location.href('BaseAdvertisement.aspx')</script>");
                }
                else
                {
                   Response.Write("<script>alert('此次操作失败');</script>");
                }
            }
            else
            {
               Response.Write("<script>alert('格式有误;只允许FLASH(SWF)和图片文件(JPG GIF)');</script>");
            }
        }
        else
        {
            if (Doadmin.Exec("update Baseadvertisement set title='"+this.subject.Text+"',href='" + href + "',alt='" + this.alt.Text + "',cd='"+this.cd.Text+"'  where id=" + Request.QueryString["id"]))
            {
                Response.Write("<script>alert('发布成功');location.href('BaseAdvertisement.aspx')</script>");
            }
            else
            {
                Response.Write("<script>alert('此次操作失败');</script>");
            }
        }
    }

}
