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

📄 update.cs

📁 这是一个用ASP加SQL做的新闻网站,里面有不少的新闻可以浏览,共享的.
💻 CS
字号:
using System; 
using System.Data; 
using System.Data.OleDb; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 


namespace www
{
	public class MyCodeBehind : Page 
	{ 
		public DataGrid     MyList; 
        public DropDownList DropDownList1; 
		public DropDownList DropDownList2; 
        public TextBox   TextBox1;
		public LinkButton   btnFirst; 
		public LinkButton   btnPrev; 
		public LinkButton   btnNext; 
		public LinkButton   btnLast; 
		public Label     lblCurrentPage; 
		public Label     lblPageCount;
		public Label     lblRecordCount;
		public TextBox    txtIndex;

		public int PageCount,RecordCount;  
 
		
		private void Page_Load(Object sender, EventArgs e)    
		{    
			if(!IsPostBack) 
			{
				OleDbConnection MyConnection =new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("new.mdb"));  
				OleDbDataAdapter myCommand=new OleDbDataAdapter("SELECT id,typename FROM newstype ",MyConnection);   
				DataSet ds= new DataSet();  
				myCommand.Fill(ds,"bb");  
				DropDownList2.DataSource = ds.Tables["bb"].DefaultView;    
				DropDownList2.DataTextField = "typename";  
				DropDownList2.DataValueField = "id";  
				DropDownList2.DataBind(); 
                DataBind(); 
			}
				DataBind();   
		}    
          
      
		DataView CreateDataSource()   
		{   
			OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ Server.MapPath("new.mdb"));       
			OleDbDataAdapter myCommand = new  OleDbDataAdapter("select  * from news WHERE typeid="+ DropDownList2.SelectedItem.Value+" order by shijian desc", myConnection);             
			DataSet ds = new DataSet();    
			myCommand.Fill(ds, "aa");    
			return ds.Tables["aa"].DefaultView;    
		}   

		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();    
		}       
        
		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; 

			//由外部分页 UI 使用    
			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("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ Server.MapPath("new.mdb"));   
			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("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ Server.MapPath("new.mdb"));
			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; 
		} 
	

	} 
}

⌨️ 快捷键说明

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