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

📄 managenews.aspx.cs

📁 asp做的新闻系统
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace news
{
	/// <summary>
	/// Update1 的摘要说明。
	/// </summary>
	public partial class ManageNews : System.Web.UI.Page
	{
	
		public int PageCount,RecordCount;
 				
		string strDBPath;	//数据库存储路径
		string strConn;		//数据库连接

		protected void Page_Load(Object sender, EventArgs e)    
		{    
			strDBPath = System.Configuration.ConfigurationSettings.AppSettings["DBPath"];
			strConn = System.Configuration.ConfigurationSettings.AppSettings["Connection"] +  Server.MapPath(strDBPath);

			//页面初始化
			if(!IsPostBack) 
			{
				//创建数据库连接,读取新闻类别信息
				OleDbConnection MyConnection = new OleDbConnection(strConn);
				OleDbDataAdapter myCommand=new OleDbDataAdapter("SELECT id,name FROM Type ",MyConnection);   
				DataSet ds= new DataSet();  
				myCommand.Fill(ds,"bb");  
				DropDownList2.DataSource = ds.Tables["bb"].DefaultView;    
				DropDownList2.DataTextField = "name";  
				DropDownList2.DataValueField = "id";  
				DropDownList2.DataBind(); 
				DataBind(); 
			}
			DataBind();   
		}    
          
		//创建视图,根据新闻类别读取新闻信息
		DataView CreateDataSource()   
		{   
			OleDbConnection myConnection = new OleDbConnection(strConn);
			OleDbDataAdapter myCommand = new  OleDbDataAdapter("select  * from News WHERE typeid="+ DropDownList2.SelectedItem.Value+" order by DTime desc", myConnection);             
			DataSet ds = new DataSet();    
			myCommand.Fill(ds, "News");    
			return ds.Tables["News"].DefaultView;    
		}   

		//为MyList绑定数据源
		public void DataBind()
		{   
			DataView source=CreateDataSource();   
			if(!IsPostBack)   
			{   
				RecordCount=source.Count;   
				PageCount=RecordCount/MyList.PageSize;   
				if((RecordCount%MyList.PageSize)!=0) PageCount++;
				lblRecordCount.Text=RecordCount.ToString();  
				lblPageCount.Text=PageCount.ToString();  
				lblCurrentPage.Text="1"; 
			}   
			MyList.DataSource = source;    
			MyList.DataBind();    
		}   

		public void MyList_Page(Object sender, DataGridPageChangedEventArgs e) 
		{    
			//设置当前页码    
			MyList.CurrentPageIndex = e.NewPageIndex;    
			DataBind();    
		}       
        
		//将输入的页码设置为MyList当前页,实现页面跳转功能
		public void txtIndex_Changed(Object sender, EventArgs e) 
		{             
			btnFirst.Enabled=true; 
			btnPrev.Enabled=true; 
			btnNext.Enabled=true; 
			btnLast.Enabled=true; 
           
			int index=Int32.Parse(txtIndex.Text.ToString());     
			PageCount=Int32.Parse(lblPageCount.Text.ToString()); 
			if(index>=1&&index<=PageCount)     
			{     
				MyList.CurrentPageIndex=index-1;      
				DataBind();   
				lblCurrentPage.Text=index.ToString(); 
	
				if(index==1) 
				{
					btnFirst.Enabled=false; 
					btnPrev.Enabled=false; 
				} 
				else if(index==PageCount) 
				{
					btnLast.Enabled=false; 
					btnNext.Enabled=false; 
				}                    
				else 			
				{ 
					txtIndex.Text=""; 
				} 
				DataBind();
			}       
		}     

        //实现多页面之间的切换
		public void PagerButtonClick(Object sender, CommandEventArgs e)     
		{    
			btnFirst.Enabled=true; 
			btnPrev.Enabled=true; 
			btnNext.Enabled=true; 
			btnLast.Enabled=true; 

			string arg = e.CommandArgument.ToString();
			PageCount=Int32.Parse(lblPageCount.Text.ToString());  
			int pageindex=Int32.Parse(lblCurrentPage.Text.ToString())-1; 

			//设置当前页码
			switch(arg)
			{    
				case "Next":    
					if (pageindex < (PageCount - 1))    
						pageindex ++;    
					break;    
				case "Prev":    
					if (pageindex > 0)    
						pageindex --;    
					break;    
				case "Last":    
					pageindex = (PageCount - 1);    
					break;  
				case "First":  
					pageindex=0;  
					break;  
			}    
			if(pageindex==0) 
			{
				btnFirst.Enabled=false; 
				btnPrev.Enabled=false; 
			} 
			else if(pageindex==PageCount-1) 
			{
				btnLast.Enabled=false; 
				btnNext.Enabled=false; 
			} 
			MyList.CurrentPageIndex=pageindex; 
			DataBind();    
			lblCurrentPage.Text=(MyList.CurrentPageIndex+1).ToString();  
	
		}  
		//根据关键字查询新闻
		public void SubmitBtn_Click(Object sender, EventArgs e) 
		{
			OleDbConnection myConnection = new OleDbConnection(strConn);
			OleDbDataAdapter myCommand = new  OleDbDataAdapter("select * from News where " + DropDownList1.SelectedItem.Value + "  like '%" + TextBox1.Text.ToString() + "%'", myConnection);

			DataSet ds = new DataSet();
			myCommand.Fill(ds, "tt");

			MyList.DataSource = ds.Tables["tt"].DefaultView;
			MyList.DataBind();
		}

		//删除新闻
		public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs e)
		{
			OleDbConnection myConnection = new OleDbConnection(strConn);
			String deleteCmd = "DELETE from News where id = @Id";
			OleDbCommand myCommand = new OleDbCommand(deleteCmd, myConnection);
			myCommand.Parameters.Add(new OleDbParameter("@Id", OleDbType.Char, 11));
			myCommand.Parameters["@Id"].Value = MyList.DataKeys[(int)e.Item.ItemIndex];

			myCommand.Connection.Open();

			try
			{
				myCommand.ExecuteNonQuery();
			
			}
			catch (OleDbException)
			{
			
			}

			myCommand.Connection.Close();
           
			DataBind();
		}

		//格式化字符串
		protected string FormatString(string str) 
		{ 
			str=str.Replace(" ","&nbsp;&nbsp;"); 
			str=str.Replace("<","&lt;"); 
			str=str.Replace(">","&gt;"); 
			str=str.Replace('\n'.ToString(),"<br>"); 
			return str; 
		} 

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.MyList.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_Delete);

		}
		#endregion

	}
}

⌨️ 快捷键说明

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