?? empsgrid.cs
字號:
namespace Employee_Directory
{
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;
public class EmpsGrid : System.Web.UI.Page
{
//職工管理窗口定義控件的聲名
protected CCUtility Utility;
//表格表單,職工變量等的聲名
protected System.Web.UI.HtmlControls.HtmlTableRow emps_no_records;
protected string emps_sSQL;
protected string emps_sCountSQL;
protected int emps_CountPage;
protected CCPager emps_Pager;protected System.Web.UI.WebControls.LinkButton emps_insert;
protected System.Web.UI.WebControls.Repeater emps_Repeater;
protected int i_emps_curpage=1;
//查詢表單、變量等的聲名
protected System.Web.UI.WebControls.Button Search_search_button;
protected System.Web.UI.WebControls.TextBox Search_emp_login;
protected System.Web.UI.WebControls.TextBox Search_name;
protected System.Web.UI.WebControls.DropDownList Search_manmonth;
//定義各表單事件保護字符串
protected string emps_FormAction="EmpsRecord.aspx?";
protected String[] emps_emp_level_lov = "0;;3;Admin".Split(new Char[] {';'});protected String[] emps_manmonth_lov = "0;;1;Yes".Split(new Char[] {';'});
protected string Search_FormAction="EmpsGrid.aspx?";
protected String[] Search_manmonth_lov = ";All;0;No;1;Yes".Split(new Char[] {';'});
//初始化事件
public EmpsGrid()
{
this.Init += new System.EventHandler(Page_Init);
}
// EmpsGrid中的自定義包含控件結束
public void ValidateNumeric(object source, ServerValidateEventArgs args) {
try{
Decimal temp=Decimal.Parse(args.Value);
args.IsValid=true;
}catch{
args.IsValid=false; }
}
//定義登錄窗口顯示控件過程
//初始化頁面過程,創建一個Utility實例,并調用其相應的各方法
protected void Page_Load(object sender, EventArgs e)
{
Utility=new CCUtility(this);
Utility.CheckSecurity(3);
// 完成窗口安全驗證
if (!IsPostBack){
Page_Show(sender, e);
}
}
//頁面關閉過程
//窗口中控件定義事件處理過程
protected void Page_Unload(object sender, EventArgs e)
{
if(Utility!=null) Utility.DBClose();
}
//窗口中控件定義事件處理過程
protected void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.Unload += new System.EventHandler(this.Page_Unload);
emps_insert.Click += new System.EventHandler (this.emps_insert_Click);
emps_Pager.NavigateCompleted+=new NavigateCompletedHandler(this.emps_pager_navigate_completed);
Search_search_button.Click += new System.EventHandler (this.Search_search_Click);
}
//定義整體顯示頁面過程
protected void Page_Show(object sender, EventArgs e)
{
emps_Bind();
Search_Show();
}
// EmpsGrid Show end
//完成表單初始化
//定義表格每頁顯示職工記錄條數
const int emps_PAGENUM = 20;
//創建數據庫種職工數據記錄的數據源集合
ICollection emps_CreateDataSource() {
//開始定義查詢數據Sql字符串
emps_sSQL = "";
emps_sCountSQL = "";
string sWhere = "", sOrder = "";
bool HasParam = false;
//建立排序方式
sOrder = " order by e.emp_login Asc";
if(Utility.GetParam("Formemps_Sorting").Length>0&&!IsPostBack)
{ViewState["SortColumn"]=Utility.GetParam("Formemps_Sorting");
ViewState["SortDir"]="ASC";}
if(ViewState["SortColumn"]!=null) sOrder = " ORDER BY " + ViewState["SortColumn"].ToString()+" "+ViewState["SortDir"].ToString();
//建立職工屬性
System.Collections.Specialized.StringDictionary Params =new System.Collections.Specialized.StringDictionary();
if(!Params.ContainsKey("emp_login")){
string temp=Utility.GetParam("emp_login");
Params.Add("emp_login",temp);}
if(!Params.ContainsKey("manmonth")){
string temp=Utility.GetParam("manmonth");
if (Utility.IsNumeric(null,temp) && temp.Length>0) { temp = CCUtility.ToSQL(temp, CCUtility.FIELD_TYPE_Number);} else {temp = "";}
Params.Add("manmonth",temp);}
if(!Params.ContainsKey("name")){
string temp=Utility.GetParam("name");
Params.Add("name",temp);}
if (Params["emp_login"].Length>0) {
HasParam = true;
sWhere +="e.[emp_login] like '%" + Params["emp_login"].Replace( "'", "''") + "%'";
}
if (Params["manmonth"].Length>0) {
if (sWhere.Length >0) sWhere +=" and ";
HasParam = true;
sWhere +="e.[manmonth]=" + Params["manmonth"];
}
if (Params["name"].Length>0) {
if (sWhere.Length >0) sWhere +=" and ";
HasParam = true;
sWhere +="e.[name] like '%" + Params["name"].Replace( "'", "''") + "%'";
}
if(HasParam)
sWhere = " WHERE (" + sWhere + ")";
//定制Sql語句
emps_sSQL = "select [e].[emp_id] as e_emp_id, " +
"[e].[emp_level] as e_emp_level, " +
"[e].[emp_login] as e_emp_login, " +
"[e].[manmonth] as e_manmonth, " +
"[e].[name] as e_name " +
" from [emps] e ";
//裝配出完整的Sql語句
emps_sSQL = emps_sSQL + sWhere + sOrder;
if (emps_sCountSQL.Length== 0) {
int iTmpI = emps_sSQL.ToLower().IndexOf("select ");
int iTmpJ = emps_sSQL.ToLower().LastIndexOf(" from ")-1;
emps_sCountSQL = emps_sSQL.Replace(emps_sSQL.Substring(iTmpI + 7, iTmpJ-6), " count(*) ");
iTmpI = emps_sCountSQL.ToLower().IndexOf(" order by");
if (iTmpI > 1) emps_sCountSQL = emps_sCountSQL.Substring(0, iTmpI);
}
//聯結數據庫,讀取數據
OleDbDataAdapter command = new OleDbDataAdapter(emps_sSQL, Utility.Connection);
DataSet ds = new DataSet();
command.Fill(ds, (i_emps_curpage - 1) * emps_PAGENUM, emps_PAGENUM,"emps");
OleDbCommand ccommand = new OleDbCommand(emps_sCountSQL, Utility.Connection);
int PageTemp=(int)ccommand.ExecuteScalar();
emps_Pager.MaxPage=(PageTemp%emps_PAGENUM)>0?(int)(PageTemp/emps_PAGENUM)+1:(int)(PageTemp/emps_PAGENUM);
bool AllowScroller=emps_Pager.MaxPage==1?false:true;
DataView Source;
Source = new DataView(ds.Tables[0]);
if (ds.Tables[0].Rows.Count == 0){
emps_no_records.Visible = true;
AllowScroller=false;}
else
{emps_no_records.Visible = false;
AllowScroller=AllowScroller&&true;}
emps_Pager.Visible=AllowScroller;
return Source;
}
//顯示哪一頁的數據
protected void emps_pager_navigate_completed(Object Src, int CurrPage)
{
i_emps_curpage=CurrPage;
emps_Bind();
}
//綁定數據
void emps_Bind() {
emps_Repeater.DataSource = emps_CreateDataSource();
emps_Repeater.DataBind();
}
//加入新職工記錄過程
void emps_insert_Click(Object Src, EventArgs E) {
string sURL = emps_FormAction+"emp_login=" + Server.UrlEncode(Utility.GetParam("emp_login")) + "&manmonth=" + Server.UrlEncode(Utility.GetParam("manmonth")) + "&name=" + Server.UrlEncode(Utility.GetParam("name")) + "&";
Response.Redirect(sURL);
}
//職工記錄排序方式改變
protected void emps_SortChange(Object Src, EventArgs E) {
if(ViewState["SortColumn"]==null || ViewState["SortColumn"].ToString()!=((LinkButton)Src).CommandArgument){
ViewState["SortColumn"]=((LinkButton)Src).CommandArgument;
ViewState["SortDir"]="ASC";
}else{
ViewState["SortDir"]=ViewState["SortDir"].ToString()=="ASC"?"DESC":"ASC";
}
emps_Bind();
}
//查詢顯示過程
void Search_Show() {
Utility.buildListBox(Search_manmonth.Items,Search_manmonth_lov,null,"");
string s;
s=Utility.GetParam("emp_login");
Search_emp_login.Text = s;
s=Utility.GetParam("name");
Search_name.Text = s;
s=Utility.GetParam("manmonth");
try {Search_manmonth.SelectedIndex=Search_manmonth.Items.IndexOf(Search_manmonth.Items.FindByValue(s));
}catch{}
}
//查詢過程事件
void Search_search_Click(Object Src, EventArgs E) {
string sURL = Search_FormAction + "emp_login="+Search_emp_login.Text+"&"
+ "name="+Search_name.Text+"&"
+ "manmonth="+Search_manmonth.SelectedItem.Value+"&"
;
sURL += "";
Response.Redirect(sURL);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -