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 manage_Editnews : System.Web.UI.Page
{
    static int nResult = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        Loginjudge();
        if (!Page.IsPostBack)
        {
            if (Request.Params["ShowID"] != null)
            {
                if (Int32.TryParse(Request.Params["ShowID"].ToString(), out nResult) == false) { return; }
                Bindtonews(nResult);
            }
        }
    }
    //取新闻按ID
    private void Bindtonews(int nMynewsID)
    {
        INews shownews=new Mynews();
        SqlDataReader dr=shownews.GetnewsByID(nMynewsID);
        if(dr.Read())
        {
            txtTitle.Text = dr["Title"].ToString();
            FreeTextBox1.Text = dr["Content"].ToString();
            txtAuthor.Text = dr["Author"].ToString();
            if (dr["Chuchu"].ToString() == "转载")
            {
                drpChuchu.SelectedIndex = 0;
            }
            else
            {
                drpChuchu.SelectedIndex = 1;
            }
            switch (dr["Leibie"].ToString())
            {
                case "企业新闻":
                    DrpLeibie.SelectedIndex = 0;
                    break;
                case "业内资讯":
                    DrpLeibie.SelectedIndex = 1;
                    break;
                case "技术文档":
                    DrpLeibie.SelectedIndex = 2;
                    break;
                default: break;
            }
        }
    }
    //修改新闻
    public int Updatenews(int nMynewsID, string sTitle, string sContent, string sAuthor, string sChuchu, string sLeibie)
    {
        int nJieguo=-1;
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString);
        string cmdText = "UPDATE Mynews SET " +
            "Title='" + sTitle + "'," +
            "Content='" + sContent + "'," +
            "Author='" + sAuthor + "'," +
            "Chuchu='" + sChuchu + "'," +
            "Leibie='" + sLeibie + "' WHERE MynewsID=" + nMynewsID;
        SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
        try
        {
            myConnection.Open();
            nJieguo = myCommand.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myConnection.Close();
        }
        return nJieguo;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string title = txtTitle.Text.ToString().Trim();
        string mycontent = FreeTextBox1.Text.ToString();
        string author = txtAuthor.Text.ToString();
        string chuchu = drpChuchu.SelectedValue.ToString();
        string leibie = DrpLeibie.SelectedValue.ToString();
        if (title.Length < 1 || mycontent.Length < 1)
        {
            lblError.Text = "标题和内容不能为空，请认真填写！";
            return;
        }
        int nJieguo = Updatenews(nResult, title, mycontent, author, chuchu, leibie);
        if (nJieguo == 1)
        {
            lblError.Text = "新闻修改成功！";
        }
        else
        {
            lblError.Text = "修改失败！";
        }
    }
    private void Loginjudge()
    {
        if (Session["Login"] == null)
        {
            Response.Redirect("Error.aspx");
        }
        else
        {
            if (Session["Login"] != "pass")
            {
                Response.Redirect("Error.aspx");
            }
            
        }
    }
        
}
