⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addblog.aspx.cs

📁 内容管理 内容管理 内容管理 内容管理 内容管理
💻 CS
字号:
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;

public partial class WebPage_AddBlog : System.Web.UI.Page
{
    string VirtualPath = "";

    protected void Page_Load(object sender, System.EventArgs e)
    {
        //验证用户功能-------------------------------------------------
        this.ValidUserStation();
        //-------------------------------------------------------------
        // string VirtualPath=RichCompany.GetVirtualPath.getVirtualPath("../../index.aspx");
        //Content.BasePath=VirtualPath+Content.BasePath;

        //初始化功能权限列表(临时放在这里)
        if (!IsPostBack)
        {
            string strID = Request["ID"];												//文章ID
            string strFunID = Request["FunID"];											//功能ID(标识执行的状态,新建、查看并编辑)

            if (strID == null || strID == "") { strID = "-1"; }
            if (strFunID == null || strFunID == "")
            {
                if (strID == null || strID == "")
                { strFunID = "2"; }
                else
                { strFunID = "1"; }
            }

            this.FunID.Value = strFunID;


            setControlAttributes();

            switch (strFunID.Trim())
            {
                case "1":			//查看
                    EditData(strID);												//获取当前文章数据,并初始化界面
                    break;
                case "2":			//新建
                    NewData(strID);													//清空界面,为新建新闻做准备
                    break;
                default:			//新建
                    NewData(strID);													//清空界面,为新建新闻做准备
                    break;
            }
        }
    }

    #region	初始控件选择状态
    /// <summary>
    /// 初始控件选择状态
    /// </summary>
    private void setControlAttributes()
    {
        this.Title.Attributes["onfocus"] = "javascript:this.select();";
    }
    #endregion



    #region	验证用户可使用的功能
    /// <summary>
    /// 验证用户身份
    /// </summary>
    /// <returns></returns>
    private void ValidUserStation()
    {
        string userid = "";
        HttpCookie Cookie = CheckLogin();

        if (Cookie != null)
        {
            userid = Cookie["User_ID"].ToString();

        }

        else
        {
            Response.Write("<script language='javascript'>window.setTimeout(\" alert('您必须登陆才可以留言!')\",100);</script>");
            return;

        }
        
        if (userid != "")
        {
            this.hidIsModify.Value = "modify";
            this.hidIsNew.Value = "new";
        }
        else
        {
            ShowPrompt();
        }
    }

    #region CheckLogin()//个人用户是否登录<返回一个HttpCookie>
    /// <summary>
    /// 是否登录
    /// </summary>
    /// <returns></returns>
    private HttpCookie CheckLogin()
    {

        HttpCookie Cookie = Request.Cookies["Message"];
        HttpCookie retCookie = null;
        if (Cookie == null || Cookie.ToString() == "")
        {
            return retCookie;
        }
        else
        {
            retCookie = Request.Cookies["Message"];
            return retCookie;
        }

    }
    #endregion

    private void ShowPrompt()
    {
        string strUrl = "<script language='javascript'>"
            + "window.setTimeout(\"alert('系统检查到[会话已过期]或[您尚未登录],将自动转到登录页面!')\", 100);"
            + "</script>";
        Response.Write(strUrl);

        strUrl = "<script language='javascript'>"
            + "window.open("
            + "\"../default.aspx\","
            + "\"_top\","
            + "\"\""
            + ",true);"
            + "</script>";
        Response.Write(strUrl);
        Response.End();
        return;
    }
    #endregion



    #region 查看并编辑文章
    /// <summary>
    /// 查看并编辑文章
    /// </summary>
    /// <param name="str_ID">文章ID</param>
    private void EditData(string str_ID)
    {
        string m_strID = str_ID;
        DataSet ds = new DataSet();
        DAL.boBusiness bo = new DAL.boBusiness();
        DAL.MakeConnection Conn = new DAL.MakeConnection();
        DAL.clsDBConnkey ConnKey = new DAL.clsDBConnkey();
        ConnKey = bo.loadkey();//得到config数据库连接


    
        try
        {
           
            string m_strSQL = "Select * From Article where ID="+str_ID;
            //------------------------------------------------------------------
            ds = (DataSet)Conn.MakeConnectionMethod(m_strSQL, ConnKey, DAL.executeMethod.execute_DataSet, DAL.EnumDBType.Sql, VirtualPath);
            DataView m_DV = ds.Tables[0].DefaultView;

            //初始化编辑界面-----------------------------------------------------
            if (m_DV.Count > 0)
            {
                this.labFunState.Text = "查看&编辑<font color='gray'>『" + m_DV[0].Row["Title"].ToString() + "』</font>";
                this.imgState.Src = "../image/Face/Modify.gif";
                //获取(NewsContent)数据------------------------------------------
                //ID,Group_ID,Title,Content,ReleaseTime,ReadedTimes,User_ID,IsNominate
                this.FunID.Value = "1";
                this.Table_ID.Value = str_ID;



                this.IsNominate.Checked = (m_DV[0].Row["IsNominate"] != DBNull.Value ? Convert.ToBoolean(m_DV[0].Row["IsNominate"]) : false);
                this.ReadTotal.Text = m_DV[0].Row["ReadTotal"].ToString();
                this.txtSummary.Text = m_DV[0].Row["Summary"].ToString();
                this.Title.Text = m_DV[0].Row["Title"].ToString();
                this.Content.Value = m_DV[0].Row["Content"].ToString();
            }
            else
            {
                this.labFunState.Text = "查看&编辑<font color='gray'>『新闻信息』</font>";
                this.imgState.Src = "../image/Face/Modify.gif";
            }
        }
        catch
        {

        }
        //-----------------------------------------------------------------------
    }
    #endregion

    #region 新建文章
    /// <summary>
    /// 新建新闻
    /// </summary>
    private void NewData(string str_ID)
    {
        //初始化新闻编辑界面-------------------------

        try
        {
            this.labFunState.Text = "新建<font color='gray'>『文章』</font>";
            this.imgState.Src = "../image/Face/New.gif";

            //获取(NewsContent)数据-----------------------------------------------------------
            //ID,Group_ID,Title,Content,ReleaseTime,ReadedTimes,ID,IsNominate
            this.FunID.Value = "2";
            this.Table_ID.Value = "";

            this.IsNominate.Checked = true;
            //this.IsAffiche.Checked = false;
            this.Title.Text = "";
            this.Content.Value = "";
        }
        catch
        { }
        //-------------------------------------------
    }
    #endregion

    #region	保存文章
    /// <summary>
    /// 保存文章
    /// </summary>
    private void SaveData()
    {
        bool m_blnCheckValue = true;
        string m_strSQL = "";
        DataSet ds = new DataSet();
        DAL.boBusiness bo = new DAL.boBusiness();
        DAL.MakeConnection Conn = new DAL.MakeConnection();
        DAL.clsDBConnkey ConnKey = new DAL.clsDBConnkey();
        ConnKey = bo.loadkey();//得到config数据库连接


        System.Data.SqlClient.SqlCommand o_CommUser = new System.Data.SqlClient.SqlCommand();
        string strErrMsg = "";
       
            
         switch (this.FunID.Value.Trim())
        {
            case "1":																				//查看编辑状态,更新所做的改动
                //检查必要的参数正确性(新闻ID,新闻ID不允许为空)

                if (this.Table_ID.Value.Trim() == "")						//[新闻]ID
                {
                    m_blnCheckValue = false;
                    break;
                }
                //为更新文章生成SQL脚本
                //NewsContent
                //ID,Group_ID,Title,Content,ReleaseTime,ReadedTimes,User_ID,IsNominate,IsAffiche
                m_strSQL = "Update Article"
                    + " Set "
                    + "Title=@Title,"
                    + "ReadTotal=@ReadTotal,Summary=@Summary,"
                    + "Content=@Content,"
                    + "IsNominate=@IsNominate "
                    + " Where"
                    + " ID=@ID";

                o_CommUser.CommandText = m_strSQL;
                o_CommUser.Parameters.Clear();

                o_CommUser.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(this.Table_ID.Value.Trim());
                o_CommUser.Parameters.Add("@ReadTotal", SqlDbType.Int).Value = Convert.ToInt32(this.ReadTotal.Text);
                o_CommUser.Parameters.Add("@Title", SqlDbType.NVarChar).Value = this.Title.Text.Trim();
                o_CommUser.Parameters.Add("@Summary", SqlDbType.NText).Value = this.txtSummary.Text.Trim(); 
                o_CommUser.Parameters.Add("@Content", SqlDbType.NText).Value = this.Content.Value.Trim();
                o_CommUser.Parameters.Add("@IsNominate", SqlDbType.Bit).Value = (this.IsNominate.Checked ? 1 : 0);
               

                break;
            case "2":																				
                //为新建文章生成SQL脚本
                //NewsContent
                //ID,Group_ID,Title,Content,ReleaseTime,ReadedTimes,User_ID,IsNominate,IsAffiche
                m_strSQL = "Insert Into Article("
                    + "Title,Summary,Content,ReadTotal,IsNominate,CreateTime"
                    + ")"
                    + " Values( "
                    + "@Title,@Summary,@Content,@ReadTotal,@IsNominate,@CreateTime"
                    + ")";

                o_CommUser.CommandText = m_strSQL;
                o_CommUser.Parameters.Clear();

                o_CommUser.Parameters.Add("@ReadTotal", SqlDbType.Int).Value = 0;
                o_CommUser.Parameters.Add("@Title", SqlDbType.NVarChar).Value = this.Title.Text.Trim();
                o_CommUser.Parameters.Add("@Summary", SqlDbType.NText).Value = this.txtSummary.Text.Trim(); 
                o_CommUser.Parameters.Add("@Content", SqlDbType.NText).Value = this.Content.Value.Trim();
                o_CommUser.Parameters.Add("@IsNominate", SqlDbType.Bit).Value = (this.IsNominate.Checked ? 1 : 0);
                o_CommUser.Parameters.Add("@CreateTime", SqlDbType.DateTime).Value = System.DateTime.Now;

                //----------------------------------------------------------
                break;
        }


             try
             {
            //执行保存(新建、更新)
            if (m_blnCheckValue)
            {
                Conn.MakeConnectionSqlCommand(o_CommUser, ConnKey, DAL.executeMethod.execute_NoneQuery, DAL.EnumDBType.Sql);
            
            }
        }
        catch (Exception es)
        {
            strErrMsg = es.Message;
            m_blnCheckValue = false;
        }
      

        //当保存成功后刷新界面
        if (m_blnCheckValue)
        {
            if (this.FunID.Value.Trim() == "1")
            {
                Response.Redirect("BlogList.aspx?");
            }
            else
            {
                Response.Redirect("AddBlog.aspx?ID=1");
            }
        }
    }
    #endregion

    private void btnReturn_Click(object sender, System.EventArgs e)
    {
              
        Response.Redirect("BlogList.aspx?FID=", true);
    }




    protected void btnSave_Click1(object sender, EventArgs e)
    {
        //保存前检查当前用户是否保存的权限[新建、修改]
        string strStateMsg = "";
        if (this.FunID.Value.Trim() == "1")
        {
            if (this.hidIsModify.Value.ToLower() != "modify")
            { strStateMsg = "修改"; }
        }
        else if (this.FunID.Value.Trim() == "2")
        {
            if (this.hidIsNew.Value.ToLower() != "new")
            { strStateMsg = "新建"; }
        }
        else
        { return; }

        if (strStateMsg != "")
        {
            string strUrl = "<script language='javascript'>"
                + "window.setTimeout(\"alert('您没有保存[" + strStateMsg + "]权限,如有需要请与系统管理员联系!')\", 100);"
                + "</script>";
            Response.Write(strUrl);
            return;
        }
            SaveData();   
    }
    

    protected void btnReturn_Click1(object sender, EventArgs e)
    {      
        Response.Redirect("BlogList.aspx");
    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -