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

📄 delnews.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;

public partial class delnews : 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对象,按照指定的查询语句获取结果
            OleDbDataAdapter odr = new OleDbDataAdapter("select * from types ",myconn);
            DataSet ds = new DataSet();
            odr.Fill(ds,"types");
            this.ddlType.DataSource = ds.Tables["types"].DefaultView;
            this.ddlType.DataTextField = "typename";
            this.ddlType.DataValueField = "id";
            this.DataBind();

            this.GridView1.DataSource = myBind();
            this.GridView1.DataBind();
        
        }
    }
    //数据绑定
    public DataView myBind()
    {
        //建立数据连接
        string strConnnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
        strConnnection += Server.MapPath(".\\App_Data\\news.mdb");
        OleDbConnection myconn = new OleDbConnection(strConnnection);
        OleDbDataAdapter odr = new OleDbDataAdapter("select * from contents where typeid="+ddlType.SelectedValue+" order by shijian desc",myconn);
        DataSet ds = new DataSet();
        odr.Fill(ds,"contents");
        return ds.Tables["contents"].DefaultView;

    
    
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //获取当前显示页索引
        GridView1.PageIndex = e.NewPageIndex;
        
        //重新绑定 
       this.GridView1.DataSource= myBind();
       this.GridView1.DataBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string strConnnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
        strConnnection += Server.MapPath(".\\App_Data\\news.mdb");
        OleDbConnection myconn = new OleDbConnection(strConnnection);
        myconn.Open();

        //使用 Value 属性确定记录的主键值。
        //设置
        String id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        String sql = "delete from contents where id ="+id;
        //Response.Write("<script>alert('dddd')</script>");
        OleDbCommand delcmd = new OleDbCommand(sql,myconn);
        try 
        {
            delcmd.ExecuteNonQuery();
            Response.Write("<script>alert('del sucess!')</script>");
        }
        catch(OleDbException ex)
        {
            
        }
        myconn.Close();
        //重新绑定
        this.GridView1.DataSource = myBind();
        this.GridView1.DataBind();


       
    }
    //按新闻类别确定按钮事件
    protected void btnConfrim_Click(object sender, EventArgs e)
    {
        this.GridView1.DataSource = myBind();
        this.GridView1.DataBind();
    }
    //查找按钮事件
    protected void btnZhao_Click(object sender, EventArgs e)
    {
        if (txtSou_suo.Text == "")
        {
            lblMsg.Text = "请输入搜索的内容!";
            this.GridView1.DataSource = myBind();
            this.GridView1.DataBind();
            return;
        }
        else
        {
            lblMsg.Visible = false;
            string strConnnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
            strConnnection += Server.MapPath(".\\App_Data\\news.mdb");
            OleDbConnection myconn = new OleDbConnection(strConnnection);
            OleDbDataAdapter odr = new OleDbDataAdapter("select * from contents where " + ddlContents.SelectedValue + " like '%" + txtSou_suo.Text.ToString() + "%'", myconn);

            DataSet ds = new DataSet();
            try
            {
                odr.Fill(ds, "tt");
                if (ds.Tables["tt"].Rows.Count != 0)
                {
                    this.GridView1.DataSource = ds.Tables["tt"].DefaultView;
                    this.GridView1.DataBind();
                }
                else
                {
                    Response.Write("<script>alert('no record!')</script>");


                }
            }
            catch (Exception ex)
            {

            }
        }
        
    }
}

⌨️ 快捷键说明

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