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

📄 booksearch.aspx.cs

📁 基于web的图书管理系统 (SQL Server 2000)
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 WebApplication1
{
	/// <summary>
	/// BookSearch 的摘要说明。
	/// </summary>
	public class BookSearch : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Button btSearch;
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		protected System.Web.UI.WebControls.Literal ErrorMsg;
		protected System.Web.UI.WebControls.Label Label1;
		protected CCUtility Utility;
		private Access acc=new Access();
		private DataSet ds=new DataSet();
		private DataSet ds1=new DataSet();
		protected string sFormAction="bookinfo.aspx?";
		protected string PrintPreviewAction="WebPrintPreview.aspx?";
		protected System.Web.UI.WebControls.DropDownList booktype;
		protected System.Web.UI.WebControls.TextBox bookname;
		protected System.Web.UI.WebControls.Button PrintPreview;
		protected string Search_FormAction="BookSearch.aspx?";
		private void Page_Load(object sender, System.EventArgs e)
		{
				this.Utility=new CCUtility(this);
			Utility.CheckSecurity();
			string str_sql="select * from DB_bookinfo";
			acc.GetDataSet(str_sql,ds1);
			this.booktype.Items.Add("");
			for(int i=0;i<ds1.Tables[0].Rows.Count;i++)
			{this.booktype.Items.Add(ds1.Tables[0].Rows[i]["Type"].ToString());}
			if (!(this.IsPostBack))
			{	
				try
				{
					ViewState["Sort"]="";
					ViewState["Page"]="0";
					Page_Show(sender,e);
				}
				catch (Exception er)
				{this.MsgBox(er.Message);}
				ErrorMsg.Visible =false;
			}
			
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}
		
		private void InitializeComponent()
		{    
			this.btSearch.Click += new System.EventHandler(this.btSearch_Click);
			this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
			this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
			this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
			this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
			this.PrintPreview.Click += new System.EventHandler(this.PrintPreview_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}

		protected void Page_Show(object sender, EventArgs e)
		{
			Search_Show();
			GridBind();		
		}
		void Search_Show() 
		{
			CCUtility Utility=new CCUtility(this);
			string Book_Type,Book_Name;	
			Book_Type=Utility.GetParam("Type");
			Book_Name=Utility.GetParam("BookName");	
			try 
			{
			this.booktype.SelectedIndex=this.booktype.Items.IndexOf(this.booktype.Items.FindByValue(Book_Type));
			}
			catch{}
            this.bookname.Text=Book_Name;
		}
		private void GridBind()
		{
			CCUtility Utility=new CCUtility(this);
			string sWhere = "";	
			bool HasParam = false;	
			System.Collections.Specialized.StringDictionary Params =new System.Collections.Specialized.StringDictionary();	
			if(!Params.ContainsKey("BookName"))
			{
				string temp=Utility.GetParam("BookName");
				Params.Add("BookName",temp);
			}
			if(!Params.ContainsKey("Type"))
			{
				string temp=Utility.GetParam("Type");
				Params.Add("Type",temp);
			}	
			if (Params["BookName"].Length>0) 
			{
				HasParam = true;
				sWhere +="BookName like '%" + Params["BookName"].Replace( "'", "''")+"%'";
			}
			if (Params["Type"].Length>0) 
			{
				if (sWhere.Length >0) sWhere +=" and ";
				HasParam = true;
				sWhere += "Type = '" + Params["Type"].Replace( "'", "''") +  "'" ;				
			}	
			if(HasParam)
				sWhere = " WHERE " + sWhere + ""; 
			DataView MyDv;			
			String strsql;
			strsql = "select * from DB_bookinfo";
			if (!HasParam)
			{sWhere=" where BookName='-1'";}
			strsql+=sWhere;
			acc.GetDataSet(strsql,ds);
			MyDv=ds.Tables[0].DefaultView ;
			if(!object.Equals(ViewState["Sort"],null))
				MyDv.Sort =ViewState["Sort"].ToString() ;
			DataGrid1.DataSource =MyDv;
			if (ViewState["NowPage"] != null)
				DataGrid1.CurrentPageIndex = int.Parse(ViewState["NowPage"].ToString().Trim());
			try
			{
				DataGrid1.DataBind();
			}
			catch  //比如查询时过滤掉了很多,ViewState["NowPage"]中保存的页数已经偏大
			{
				DataGrid1.CurrentPageIndex = DataGrid1.PageCount-1;
				DataGrid1.DataBind();
			}

		}
	    #endregion
		public void MsgBox( string msg )
		{
			string strScript =  "<script language='Javascript'>alert('" + msg + "');</script>";
			Page.RegisterStartupScript("alert", strScript);
		}
		private void btSearch_Click(object sender, System.EventArgs e)
		{
			string sURL="";
			string search_type=this.booktype.SelectedValue.ToString();
			if(search_type!="")
			{
				 sURL = Search_FormAction + "BookName="+this.bookname.Text+"&"
					+ "Type="+this.booktype.SelectedItem.Value.ToString()+"&";
				Response.Redirect(sURL);
			}
			else
			{
				sURL = Search_FormAction + "BookName="+this.bookname.Text+"&";
				Response.Redirect(sURL);
			}
			
		}

		private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			ViewState["NowPage"] = e.NewPageIndex;
			this.GridBind();	
		}
	
		private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{	
			string book_id;
			try
			{
				book_id=e.Item.Cells[0].Text.Trim();
				Response.Redirect(this.sFormAction+"ID="+book_id+"&");
			}
			catch(Exception ex)
			{
				this.MsgBox(ex.Message);
			}
          
		}
		private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			string book_id;
			book_id=e.Item.Cells[0].Text.Trim();
			string delete_sql="delete from DB_bookinfo where BookID='"+book_id+"'";
			bool success=acc.ExecuteSQL(delete_sql);		
			if(success==true)
			{this.MsgBox("删除成功!");this.GridBind();}
			else {this.MsgBox("删除失败!");}
		}
	
		private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			string strSortBy = "";
			string strSortAscending = "";
			//缓存当前信息
			if (ViewState["SortExpression"] != null)
				strSortBy = ViewState["SortExpression"].ToString().Trim();
			if (ViewState["SortAscending"] != null)
				strSortAscending = ViewState["SortAscending"].ToString().Trim();
			//设置新的排序表达式
			ViewState["SortExpression"] = e.SortExpression;
			//如果单击排序列,排序顺序颠倒!
			if (e.SortExpression == strSortBy)
			{
				ViewState["SortExpression"]=e.SortExpression + " desc";
				ViewState["SortAscending"]="no";
			}
			else
			{
				ViewState["SortExpression"] = e.SortExpression;
				ViewState["SortAscending"]="yes";
			}
			this.GridBind();			
		}

		private void PrintPreview_Click(object sender, System.EventArgs e)
		{
			CCUtility Utility=new CCUtility(this);
			string sWhere = "";	
			bool HasParam = false;	
			System.Collections.Specialized.StringDictionary Params =new System.Collections.Specialized.StringDictionary();	
			if(!Params.ContainsKey("BookName"))
			{
				string temp=Utility.GetParam("BookName");
				Params.Add("BookName",temp);
			}
			if(!Params.ContainsKey("Type"))
			{
				string temp=Utility.GetParam("Type");
				Params.Add("Type",temp);
			}	
			if (Params["BookName"].Length>0) 
			{
				HasParam = true;
				sWhere +="BookName like '%" + Params["BookName"].Replace( "'", "''")+"%'";
			}
			if (Params["Type"].Length>0) 
			{
				if (sWhere.Length >0) sWhere +=" and ";
				HasParam = true;
				sWhere += "Type = '" + Params["Type"].Replace( "'", "''") +  "'" ;				
			}	
			if(HasParam)
				sWhere = " WHERE " + sWhere + ""; 
			String strsql;
			strsql = "select * from DB_bookinfo";
			if (!HasParam)
			{sWhere=" where BookName='-1'";}
			strsql+=sWhere;
			string sURL = this.PrintPreviewAction + "strsql="+strsql+"&";
			Response.Redirect(sURL);
			
		}

		



	}
}

⌨️ 快捷键说明

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