ManagePerson.aspx.cs
资源名称:OA_at.rar [点击查看]
上传用户:tree100901
上传日期:2007-06-03
资源大小:2295k
文件大小:10k
源码类别:
OA系统
开发平台:
C#
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Data.SqlClient;
- 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 OA
- {
- /// <summary>
- /// ManagePerson 的摘要说明。
- /// </summary>
- public class ManagePerson : System.Web.UI.Page
- {
- protected System.Web.UI.WebControls.Label stats;
- protected System.Web.UI.WebControls.DataGrid MyDataGrid;
- protected System.Data.SqlClient.SqlConnection MyConnection;
- private int totalPerson;
- protected System.Web.UI.WebControls.LinkButton AddUser;
- public string PID;
- private void Page_Load(object sender, System.EventArgs e)
- {
- AddUser.Attributes.Add("onclick","window.open('NewUser.aspx','_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,revisable=no,left=100,top=0,width=500,height=200');return false;");
- CheckUser CHU = new CheckUser();
- CHU.UserName = User.Identity.Name;
- PID = CHU.UserID.ToString();
- if(CHU.Is_SystemManager() == false)
- Response.Write("<script>alert('对不起,你没有查看此页面的权限!');history.back(2)</" + "script>");
- MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
- if(!IsPostBack)
- {
- MyDataGrid.DataSource = CreateDataSource();
- MyDataGrid.DataBind();
- }
- // 在此处放置用户代码以初始化页面
- }
- private ICollection CreateDataSource()
- {
- MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
- SqlDataAdapter MyCommand1 = new SqlDataAdapter("PersonIDTree0",MyConnection);
- MyCommand1.SelectCommand.CommandType = CommandType.StoredProcedure;
- MyCommand1.SelectCommand.Parameters.Add(new SqlParameter("@JobID", SqlDbType.NVarChar, 50));
- MyCommand1.SelectCommand.Parameters["@JobID"].Value = "%";
- DataSet ds = new DataSet();
- MyCommand1.Fill(ds,"Person");
- MyConnection.Close();
- DataView dv = ds.Tables[0].DefaultView;
- totalPerson=ds.Tables[0].Rows.Count;
- if (Session["tableAllPerson"] == null)
- Session["tableAllPerson"] = ds.Tables[0];
- if (Session["sorterAllPerson"] == null)
- {
- Session["sorterAllPerson"] ="JobID";
- Session["sortAllPerson"]=" DESC";
- }
- dv.Sort = (String) Session["sorterAllPerson"]+(String) Session["sortAllPerson"];
- ShowStats();
- return dv;
- }
- public void MyDataGrid_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
- {
- DataGrid gridThatCausedEvent = (DataGrid) sender;
- gridThatCausedEvent.CurrentPageIndex = e.NewPageIndex;
- gridThatCausedEvent.DataSource = CreateDataSource();
- gridThatCausedEvent.DataBind();
- }
- public void MyDataGrid_Sort(Object sender, DataGridSortCommandEventArgs e)
- {
- Session["sorterAllPerson"] = (string)e.SortExpression;
- Session["sortAllPerson"]=(Session["sortAllPerson"].ToString()==" ASC")?
- " DESC":" ASC";
- MyDataGrid.DataSource = CreateDataSource();
- MyDataGrid.DataBind();
- foreach (DataGridItem item in MyDataGrid.Controls[0].Controls)
- {
- if (item.ItemType == ListItemType.Header)
- {
- Label lb =(Label)item.FindControl((string)Session["sorterAllPerson"]);
- lb.Text = (Session["sortAllPerson"].ToString()==" DESC")?
- "↓":"↑";
- }
- }
- }
- private void ShowStats()
- {
- if (totalPerson == 0)
- {
- MyDataGrid.Visible = false;
- stats.Text = "未找到符合条件的记录!";
- }
- else
- {
- MyDataGrid.Visible = true;
- int startOffset = (totalPerson > 0) ?
- (MyDataGrid.CurrentPageIndex*MyDataGrid.PageSize+1) : 0;
- int pageEndOffset = (MyDataGrid.CurrentPageIndex+1)*(MyDataGrid.PageSize);
- int endOffset = (pageEndOffset > totalPerson) ? totalPerson : pageEndOffset;
- string SortName;
- switch(Session["sorterAllPerson"].ToString())
- {
- case "Name":
- SortName = "姓名";
- break;
- case "Organization":
- SortName="隶属";
- break;
- case "JobID":
- SortName = "角色";
- break;
- default:
- SortName="姓名";
- break;
- }
- stats.Text = String.Format("<B>共</B>{2}<B>条记录中的</B>{0}-{1}<B>条记录 根据</B>{3}<B>排序</B>",
- startOffset,endOffset,totalPerson,SortName);
- MyDataGrid.PagerStyle.Visible = (totalPerson <= MyDataGrid.PageSize) ? false : true;
- }
- }
- public void MyDataGrid_ItemCreated(Object sender, DataGridItemEventArgs e)
- {
- if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
- {
- }
- if((e.Item.ItemType == ListItemType.Header))
- {
- for(int i = 0; i<3; i++)
- {
- Label lb = new Label();
- if(i==0)
- lb.ID = "Name";
- if(i==1)
- lb.ID = "OrganizationID";
- if(i==2)
- lb.ID = "JobID";
- lb.Text = "↓";
- e.Item.Cells[i].Controls.Add(lb);
- }
- }
- }
- public void MyDataGrid_ItemDataBound(Object sender, DataGridItemEventArgs e)
- {
- if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
- {
- string PersonID = DataBinder.Eval(e.Item.DataItem, "PersonID").ToString();
- string OID =e.Item.Cells[1].Text.Trim().Replace("(","").Replace(")","");
- string [] str = OID.Split(new char[] {','});
- string OName="";
- string SID="";
- string SName="";
- string SN="";
- for(int i=0; i<str.Length; i++)
- {
- SqlCommand MyCommand0 = new SqlCommand("OrganizationName",MyConnection);
- MyCommand0.CommandType = CommandType.StoredProcedure;
- MyCommand0.Parameters.Add(new SqlParameter("@OrganizationID", SqlDbType.Int, 4));
- MyCommand0.Parameters["@OrganizationID"].Value = str[i];
- if(MyConnection.State.ToString()=="Closed")
- MyConnection.Open();
- SqlDataReader myReader0 = MyCommand0.ExecuteReader();
- while(myReader0.Read())
- {
- SID = myReader0["SystemID"].ToString();
- OName = ","+myReader0["OrganizationName"].ToString();
- }
- myReader0.Close();
- string [] str2 = SID.Split(new char[] {','});
- for(int j=0; j<str2.Length; j++)
- {
- SqlCommand MyCommand2 = new SqlCommand("SelectSystemName",MyConnection);
- MyCommand2.CommandType = CommandType.StoredProcedure;
- MyCommand2.Parameters.Add(new SqlParameter("@SystemID", SqlDbType.Int, 4));
- MyCommand2.Parameters["@SystemID"].Value = str2[j];
- if(MyConnection.State.ToString()=="Closed")
- MyConnection.Open();
- SqlDataReader myReader2 = MyCommand2.ExecuteReader();
- while(myReader2.Read())
- {
- if(str2.Length>1)
- SName += ","+myReader2["SystemName"].ToString();
- else
- SName = ","+myReader2["SystemName"].ToString();
- }
- myReader2.Close();
- }
- SN +=",("+SName.Substring(1)+")"+OName.Substring(1);
- MyConnection.Close();
- }
- e.Item.Cells[1].ToolTip = SN.Substring(1);
- if(SN.Length>11)
- SN = SN.Substring(1,10)+"......";
- else
- SN = SN.Substring(1);
- e.Item.Cells[1].Text = SN;
- string JID =e.Item.Cells[2].Text.Trim().Replace("(","").Replace(")","");
- string [] str1 = JID.Split(new char[] {','});
- string JName="";
- for(int i=0; i<str1.Length; i++)
- {
- SqlCommand MyCommand1 = new SqlCommand("JobName",MyConnection);
- MyCommand1.CommandType = CommandType.StoredProcedure;
- MyCommand1.Parameters.Add(new SqlParameter("@JobID", SqlDbType.Int, 4));
- MyCommand1.Parameters["@JobID"].Value = str1[i];
- if(MyConnection.State.ToString()=="Closed")
- MyConnection.Open();
- SqlDataReader myReader1 = MyCommand1.ExecuteReader();
- while(myReader1.Read())
- JName += ","+myReader1["JobName"].ToString();
- myReader1.Close();
- MyConnection.Close();
- }
- e.Item.Cells[2].ToolTip = JName.Substring(1);
- if(JName.Substring(1).Length>6)
- JName = JName.Substring(1,6)+"......";
- else
- JName = JName.Substring(1);
- e.Item.Cells[2].Text = JName.Substring(0);
- LinkButton myDeleteButton1;
- LinkButton myDeleteButton2;
- myDeleteButton1 = (LinkButton) e.Item.Cells[3].Controls[0];
- myDeleteButton1.Attributes.Add("onclick", @"if(confirm('你确认要修改这条记录吗?')) window.open('EditPerson.aspx?PersonID="+PersonID+"','_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,revisable=no,left=100,top=0,width=600,height=200');return false;");
- myDeleteButton2 = (LinkButton) e.Item.Cells[4].Controls[0];
- myDeleteButton2.Attributes.Add("onclick", @"return confirm('你确认要删除这条记录吗?');");
- }
- }
- public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs E)
- {
- SqlCommand MyCommand = new SqlCommand("DeletePerson",MyConnection);
- MyCommand.CommandType = CommandType.StoredProcedure;
- MyCommand.Parameters.Add(new SqlParameter("@PersonID", SqlDbType.Int, 4));
- MyCommand.Parameters["@PersonID"].Value = MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
- if(MyConnection.State.ToString()=="Closed")
- MyConnection.Open();
- try
- {
- MyCommand.ExecuteNonQuery();
- }
- catch (SqlException)
- {
- RegisterStartupScript("alert","<script>alert('出现错误:未能删除记录!')</" + "script>");
- }
- MyConnection.Close();
- if ((MyDataGrid.CurrentPageIndex!=0)&&((int)E.Item.ItemIndex==0))
- {
- if (MyDataGrid.Items.Count==1)
- MyDataGrid.CurrentPageIndex-=1;
- }
- MyDataGrid.DataSource = CreateDataSource();
- MyDataGrid.DataBind();
- Response.Write("<script>opener.location.href=opener.location.href;opener=null;window.close();</"+"script>");
- }
- #region Web 窗体设计器生成的代码
- override protected void OnInit(EventArgs e)
- {
- //
- // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
- //
- InitializeComponent();
- base.OnInit(e);
- }
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.Load += new System.EventHandler(this.Page_Load);
- }
- #endregion
- }
- }