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

📄 addnews.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;
using System.Data.OleDb;
using System.IO;
using System.Text;
//该源码下载自www.51aspx.com(51aspx.com)

public partial class addnews : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string strConnnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
            strConnnection += Server.MapPath(".\\App_Data\\news.mdb");

            OleDbConnection myconn = new OleDbConnection(strConnnection);
            OleDbDataAdapter mycmd = new OleDbDataAdapter("select id ,typename from types", myconn);
            
            DataSet ds = new DataSet();
            mycmd.Fill(ds, "types");
            this.DropDownList1.DataSource = ds.Tables["types"].DefaultView;
           
            //绑定DropDownList
            DropDownList1.DataTextField = "typename";
            DropDownList1.DataValueField = "id";
            DropDownList1.DataBind();
        }
    }
    protected void btnFabiao_Click(object sender, EventArgs e)
    {
       

        
      

        //创建上传文件的路径字符串
       String savePath = Server.MapPath("file/");
        
        
        //检查新闻标题、内容和作者是否为空
        if ((txtBiaoti.Text == "") || (txtNeirong.Text != "") || (txtZuozhe.Text == ""))
        {

            lblMsg.Text = "标题、内容、作者等不能为空!";
        }//检查新闻标题长度
        else if (txtBiaoti.Text.Length >= 50)
        {
            lblMsg.Text = "你的标题太长了";
        }//检查上传文件大小
        else if (fuUpLoad.PostedFile.ContentLength > 153600)
        {
            lblMsg.Text = "上传的文件不能超过70kb";
            return;
        }
        //检查是否已经有同名的上传文件
      
        if (fuUpLoad.HasFile)
        {
            String filename = fuUpLoad.FileName;//上传文件名
            savePath += filename;      //上传路经
            fuUpLoad.SaveAs(savePath);//保存文件

        }

        //建立数据连接
        string strConnnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
        strConnnection += Server.MapPath(".\\App_Data\\news.mdb");
        OleDbConnection myconn = new OleDbConnection(strConnnection);

        string sql = "insert into contents(biaoti,neirong,zuozhe,shijian,click,img,typeid)values";
        sql += "('" + txtBiaoti.Text + "','" + txtNeirong.Text + "','" + txtZuozhe.Text + "','" + DateTime.Now.ToString() + "',0,'" + fuUpLoad.FileName.ToString() + "','" + DropDownList1.SelectedItem.Value + "')";

        OleDbCommand mycmd = new OleDbCommand(sql, myconn);
        mycmd.Connection.Open();
        mycmd.ExecuteNonQuery();
        mycmd.Connection.Close();
        Response.Redirect("default.aspx");


    }
    protected void btnQuxiao_Click(object sender, EventArgs e)
    {
        txtZuozhe.Text = "";
        txtNeirong.Text = "";
        txtBiaoti.Text = "";
    }
}







































⌨️ 快捷键说明

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