MyFriends.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>
- /// MyFriends 的摘要说明。
- /// </summary>
- public class MyFriends : System.Web.UI.Page
- {
- protected System.Data.SqlClient.SqlConnection MyConnection;
- protected System.Web.UI.WebControls.DataGrid MyDataGrid;
- protected System.Web.UI.WebControls.TextBox Friend;
- protected System.Web.UI.WebControls.Label stats;
- private int totalPerson;
- public string PID,FID;
- protected System.Web.UI.HtmlControls.HtmlInputButton AddFriend;
- private void Page_Load(object sender, System.EventArgs e)
- {
- MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
- if(!IsPostBack)
- {
- MyDataGrid.DataSource = CreateDataSource();
- MyDataGrid.DataBind();
- Session["ndt"] = CreateDataSource();
- }
- // 在此处放置用户代码以初始化页面
- }
- private DataTable CreateDataSource()
- {
- SqlCommand MyCommand00 = new SqlCommand("PersonID",MyConnection);
- MyCommand00.CommandType = CommandType.StoredProcedure;
- MyCommand00.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar, 50));
- MyCommand00.Parameters["@Name"].Value = User.Identity.Name;
- if(MyConnection.State.ToString()=="Closed")
- MyConnection.Open();
- SqlDataReader myReader00 = MyCommand00.ExecuteReader();
- myReader00.Read();
- PID = myReader00.GetInt32(0).ToString().Trim();
- FID = myReader00["Friends"].ToString().Trim();
- Session["FID"]=CleanInput(FID);
- Session["PID"]=CleanInput(PID);
- myReader00.Close();
- DataTable dt = new DataTable();
- dt.Columns.Add(new DataColumn("PersonID", typeof(Int32)));
- dt.Columns.Add(new DataColumn("FriendName", typeof(string)));
- if(FID!="")
- {
- string [] str = RemoveDup(FID.Split(new char[] {','}));
- for(int i = 0; i<str.Length; i++)
- {
- if(str[i]!="")
- {
- DataRow dr = dt.NewRow();
- dr["PersonID"] = Int32.Parse(str[i].Trim());
- SqlCommand MyCommand0 = new SqlCommand("PersonName",MyConnection);
- MyCommand0.CommandType = CommandType.StoredProcedure;
- MyCommand0.Parameters.Add(new SqlParameter("@PersonID", SqlDbType.Int, 4));
- MyCommand0.Parameters["@PersonID"].Value = Int32.Parse(str[i].Trim());
- if(MyConnection.State.ToString()=="Closed")
- MyConnection.Open();
- SqlDataReader myReader0 = MyCommand0.ExecuteReader();
- while(myReader0.Read())
- dr["FriendName"] = myReader0["Name"].ToString().Trim();
- myReader0.Close();
- dt.Rows.Add(dr);
- }
- }
- }
- totalPerson=dt.Rows.Count;
- ShowStats();
- return dt;
- }
- public void MyDataGrid_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
- {
- DataGrid gridThatCausedEvent = (DataGrid) sender;
- gridThatCausedEvent.CurrentPageIndex = e.NewPageIndex;
- gridThatCausedEvent.DataSource = CreateDataSource();
- gridThatCausedEvent.DataBind();
- }
- 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;
- stats.Text = String.Format("<B>共</B>{2}<B>条记录中的</B>{0}-{1}<B>条记录",
- startOffset,endOffset,totalPerson);
- MyDataGrid.PagerStyle.Visible = (totalPerson <= MyDataGrid.PageSize) ? false : true;
- }
- }
- public void MyDataGrid_ItemDataBound(Object sender, DataGridItemEventArgs e)
- {
- if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
- {
- /* SqlCommand MyCommand1 = new SqlCommand("PersonName",MyConnection);
- MyCommand1.CommandType = CommandType.StoredProcedure;
- MyCommand1.Parameters.Add(new SqlParameter("@PersonID", SqlDbType.Int, 4));
- MyCommand1.Parameters["@PersonID"].Value = e.Item.Cells[3].Text;
- if(MyConnection.State.ToString()=="Closed")
- MyConnection.Open();
- SqlDataReader myReader1 = MyCommand1.ExecuteReader();
- myReader1.Read();
- e.Item.Cells[3].Text = myReader1["Name"].ToString();
- myReader1.Close();
- MyConnection.Close();
- */ LinkButton myDeleteButton;
- myDeleteButton = (LinkButton) e.Item.Cells[2].Controls[0];
- myDeleteButton.Attributes.Add("onclick", @"return confirm('你确认要删除吗?');");
- }
- }
- public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs E)
- {
- if(Session["PID"]!=null||Session["FID"].ToString().Trim()!="")
- {
- string [] str = Session["FID"].ToString().Split(new char[] {','});
- string NFID="";
- for(int i = 0; i<str.Length; i++)
- {
- if(str[i]==MyDataGrid.DataKeys[(int)E.Item.ItemIndex].ToString().Trim())
- NFID += "";
- else
- NFID += ","+ str[i];
- }
- if(NFID.Trim()!="")
- {
- NFID=NFID.Substring(1);
- }
- else
- NFID="";
- SqlCommand MyCommand = new SqlCommand("UpdatePersonFriend",MyConnection);
- MyCommand.CommandType = CommandType.StoredProcedure;
- MyCommand.Parameters.Add(new SqlParameter("@PersonID", SqlDbType.Int, 4));
- MyCommand.Parameters["@PersonID"].Value = Session["PID"];
- MyCommand.Parameters.Add(new SqlParameter("@Friends", SqlDbType.NVarChar, 200));
- MyCommand.Parameters["@Friends"].Value = NFID;
- 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();
- }
- }
- #region Web 窗体设计器生成的代码
- override protected void OnInit(EventArgs e)
- {
- //
- // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
- //
- InitializeComponent();
- base.OnInit(e);
- }
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.AddFriend.ServerClick += new System.EventHandler(this.AddFriend_ServerClick);
- this.Load += new System.EventHandler(this.Page_Load);
- }
- #endregion
- private void AddFriend_ServerClick(object sender, System.EventArgs e)
- {
- string NFID="";
- if(Friend.Text.Trim()=="")
- RegisterStartupScript("alert","<script>alert('你未选择好友!')</script>");
- else
- {
- string[] myData = CleanInput(Friend.Text.Trim()).Replace(",,",",").Split(new Char [] {','});
- string FriendSec = "";
- string Sumac ="";
- string [] str = RemoveDup(myData);
- for(int i = 0; i<str.Length; i++)
- {
- if(str[i]!="")
- {
- SqlCommand MyCommand1 = new SqlCommand("PersonID",MyConnection);
- MyCommand1.CommandType = CommandType.StoredProcedure;
- MyCommand1.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar, 50));
- MyCommand1.Parameters["@Name"].Value = str[i];
- if(MyConnection.State.ToString()=="Closed")
- MyConnection.Open();
- SqlDataReader myReader1 = MyCommand1.ExecuteReader();
- while(myReader1.Read())
- {
- FriendSec = myReader1.GetInt32(0).ToString();
- }
- myReader1.Close();
- if(FriendSec.Trim()!="")
- {
- Sumac += ","+FriendSec;
- }
- }
- }
- if(Sumac!="")
- {
- if(Session["FID"].ToString().Trim().Replace(",,",",")!="")
- {
- string NF = Session["FID"].ToString().Trim()+Sumac;
- string [] nstr = NF.Trim().Replace(",,",",").Split(new Char [] {','});
- string [] nnstr = RemoveDup(nstr);
- string NID = "";
- for(int i = 0; i<nnstr.Length; i++)
- {
- NID += ","+nnstr[i];
- }
- NFID = NID.Substring(1);
- }
- else
- NFID = Sumac.Substring(1);
- }
- SqlCommand MyCommand = new SqlCommand("UpdatePersonFriend",MyConnection);
- MyCommand.CommandType = CommandType.StoredProcedure;
- MyCommand.Parameters.Add(new SqlParameter("@PersonID", SqlDbType.Int, 4));
- MyCommand.Parameters["@PersonID"].Value = Session["PID"];
- MyCommand.Parameters.Add(new SqlParameter("@Friends", SqlDbType.NVarChar, 200));
- MyCommand.Parameters["@Friends"].Value = NFID;
- if(MyConnection.State.ToString()=="Closed")
- MyConnection.Open();
- try
- {
- MyCommand.ExecuteNonQuery();
- }
- catch (SqlException)
- {
- RegisterStartupScript("alert","<script>alert('出现错误:未能删除记录!')</" + "script>");
- }
- MyConnection.Close();
- Friend.Text = "";
- MyDataGrid.DataSource = CreateDataSource();
- MyDataGrid.DataBind();
- }
- }
- public static string[] RemoveDup(string[] myData)
- {
- if (myData.Length > 0)
- {
- Array.Sort(myData);
- int size = 1;
- for (int i=1; i<myData.Length; i++)
- if (myData[i] != myData[i-1])
- size++;
- string[] myTempData = new string[size];
- int j=0;
- myTempData[j++] = myData[0];
- for (int i=1; i<myData.Length; i++)
- if (myData[i] != myData[i-1])
- myTempData[j++] = myData[i];
- return myTempData;
- }
- return myData;
- }
- public String CleanInput(string strIn)
- {
- return System.Text.RegularExpressions.Regex.Replace(strIn, @"[^w,]", "");
- }
- }
- }