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

📄 bmgl.aspx.cs

📁 ASP.net+Access数据库源码数据库源码
💻 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 Admin_bmgl : System.Web.UI.Page
{
    //page加载事件
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            OleDbConnection con = DB.mycon();
            con.Open();
            OleDbCommand cmm = new OleDbCommand("select distinct bmID,bmName from bmTable", con);
            OleDbDataReader ddr = cmm.ExecuteReader();
            this.DropDownList1.DataSource = ddr;
            this.DropDownList1.DataTextField = "bmName";
            this.DropDownList1.DataValueField = "bmID";
            this.DropDownList1.DataBind();
            ddr.Close();
            con.Close();
            BindToDataGrid(DropDownList1.Text.Trim());
        }
    }

    //数据填充方法
    private void BindToDataGrid(string ss)
    {
        OleDbConnection con = DB.mycon();
        con.Open();
        OleDbCommand cmd = new OleDbCommand("select bmID,bsID,bsName from bmTable where bmID='"+ss+"'", con);
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds,"bmTable");
        this.DataGrid1.DataKeyField = "bsID";
        this.DataGrid1.DataSource = ds.Tables["bmTable"];
        this.DataGrid1.DataBind();
    }

    private void BindTo()
    {
        OleDbConnection con = DB.mycon();
        con.Open();
        OleDbCommand cmd = new OleDbCommand("select bmID,bsID,bsName from bmTable", con);
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "bmTable");
        this.DataGrid1.DataKeyField = "bsID";
        this.DataGrid1.DataSource = ds.Tables["bmTable"];
        this.DataGrid1.DataBind();
    }

    //分页
    protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
        this.BindToDataGrid(DropDownList1.Text.Trim());
    }
    
    //高亮显示
    protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //鼠标悬停高亮显示
            e.Item.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#fff5ee'");
            e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            //删除询问
            ((LinkButton)(e.Item.Cells[2].Controls[0])).Attributes.Add("onclick", "return confirm('确认删除吗?');");
        }
    }

    //编辑按钮事件
    protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
    {
        this.DataGrid1.EditItemIndex = e.Item.ItemIndex;
        this.BindToDataGrid(DropDownList1.Text.Trim());
    }

    //更新按钮事件
    protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
    {
        string bsID = this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
        string bsName = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;
        //string bmName = ((TextBox)(e.Item.Cells[2].Controls[0])).Text;
        OleDbConnection con = DB.mycon();
        con.Open();
        OleDbCommand cmd = new OleDbCommand("update bmTable set bsName='" + bsName + "' where bsID='" + bsID + "' and bmID='"+DropDownList1.Text.Trim()+"'", con);
        cmd.ExecuteNonQuery();
        this.BindToDataGrid(DropDownList1.Text.Trim());
    }

    //取消按钮事件
    protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
    {
        this.DataGrid1.EditItemIndex = -1;
        this.BindToDataGrid(DropDownList1.Text.Trim());
    }
    
    //删除按钮事件
    protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
    {
        string bsID = this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
        OleDbConnection con = DB.mycon();
        con.Open();
        OleDbCommand cmd = new OleDbCommand("delete from bmTable where bmID='" + DropDownList1.Text.Trim() + "' and bsID='" +bsID+ "'", con);
        OleDbCommand cmm = new OleDbCommand("delete from telph where bmID='" + DropDownList1.Text.Trim() + "' and bsID='" + bsID + "'", con);
        cmd.ExecuteNonQuery();
        cmm.ExecuteNonQuery();
        this.BindToDataGrid(DropDownList1.Text.Trim());
    }
    
    //下拉列表事件
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sd = DropDownList1.Text.Trim();
        this.BindToDataGrid(sd);
    }
}

⌨️ 快捷键说明

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