//绑定Dropdownlist
 public void DropDownList_Bind()
 {
     EFModel.Database1Entities context = new EFModel.Database1Entities();
     DropDownList_Search.Items.Clear();
     DropDownList_Search.Items.Add(new ListItem("请选择您要搜素的科室名称", "0"));
     var query = from t in context.Office orderby t.科室ID where t.所属部门ID == departmentId select t.科室名称;
     int y = 1;
     foreach (var q in query)
     {
         DropDownList_Search.Items.Add(new ListItem(q.ToString(), y.ToString()));
         y++;
     }
 }
 protected void SubmitButton_Click(object sender, EventArgs e)
 {
     /*
     if (string.IsNullOrEmpty(username.Text))
     {
         Label1.Text = "请输入用户名!";
         return;
     }
     if (string.IsNullOrEmpty(password.Text))
     {
         Label1.Text = "请输入密码";
         return;
     }
      * */
     Label1.Text = "正在验证……";
     EFModel.Database1Entities context = new EFModel.Database1Entities();
     var q = from t in context.LoginInfo
             where t.用户名 == Username.Text
             select t;
     if (q.Count() == 0)
     {
         Label1.Text = "用户名不存在!";
     }
     else
     {
         if (Password.Text == q.First().密码)
         {
             Label1.Text = "登录成功!即将为您跳转……";
             Session["UserFlag"] = true;
             Session["UserName"] = Username.Text;
             Session["UserType"] = q.First().用户类型;
             //Response.Redirect(string.Format("WebForm1.aspx?userName={0}", Server.UrlEncode(q.First().用户名)));
             if (q.First().用户类型 == "超级管理员")
             {
                 Response.Redirect("../Admin/AdminWebForm.aspx");
             }
             else if (q.First().用户类型 == "部门")
             {
                 Response.Redirect("../Department/DepartmentWebForm.aspx");
             }
             else
             {
                 Response.Redirect("../Tourist/TouristWebForm.aspx");
             }
         }
         else
         {
             Label1.Text = "密码不正确!";
         }
     }
 }
 protected void Button1_Click(object sender, EventArgs e)
 {
     string password = string.Empty;
     string str = this.yanZhengMa.Text.ToString();
     string ss = str[0].ToString() + str[1].ToString() + str[2].ToString().ToUpper() + str[3].ToString();
     if (Session["validate"].Equals(ss))
     {
         string name = Session["UserName"].ToString();
         EFModel.Database1Entities context = new EFModel.Database1Entities();
         var query = from t in context.LoginInfo where t.用户名 == name select t.密码;
         foreach (var q in query)
         {
             password = q;
         }
         if (password.Equals(this.oldPassword.Text.ToString()))
         {
             string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString"].ConnectionString;
             SqlConnection conn = new SqlConnection(connectionString);
             string sql = "update LoginInfo set 密码='" + NewPassword.Text + "' where 用户名='" + name + "'";
             SqlCommand cmd = new SqlCommand(sql, conn);
             try
             {
                 conn.Open();
                 cmd.ExecuteNonQuery();
                 conn.Close();
                 Js_ShowMessage("恭喜您,密码修改成功!请重新登录!");
                 Session["UserFlag"] = false;
                 Js_Redirect("../Index.aspx");
                 Response.End();
             }
             catch (Exception)
             {
                 Js_ShowMessage("对不起,密码修改失败!");
                 Js_Redirect("AdminAccount.aspx");
                 Response.End();
                 throw;
             }
         }
         else
         {
             Response.Write("<script>alert('对不起,您输入的原始密码错误!');</script>");
             Js_Redirect("AdminAccount.aspx");
         }
     }
     else
     {
         Response.Write("<script>alert('验证码错误!');</script>");
         Js_Redirect("AdminAccount.aspx");
     }
 }
 /// <summary>
 /// 添加部门
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void btnAddDepartment_Click(object sender, EventArgs e)
 {
     if (this.txtDepartmentName.Text.Equals(""))
     {
         Js_ShowMessage("部门名称不能为空!");
     }
     else
     {
         EFModel.Database1Entities context = new EFModel.Database1Entities();
         var q = from t in context.Department
                 select t.部门名称;
         foreach (var i in q)
         {
             if (i.Equals(this.txtDepartmentName.Text))
             {
                 Js_ShowMessage("该部门已经存在了!");
                 this.txtDepartmentName.Text = "";
                 return;
             }
         }
         string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString"].ConnectionString;
         SqlConnection conn = new SqlConnection(strConn);
         try
         {
             conn.Open();
             SqlCommand cmd = conn.CreateCommand();
             cmd.CommandText = "insert into Department(部门名称,部门地点) values('" + txtDepartmentName.Text + "','" + txtDepartmentPlace.Text + "');insert into LoginInfo values('" + txtDepartmentName.Text + "','0123456789','部门')";
             cmd.ExecuteNonQuery();
             conn.Close();
             Js_ShowMessage("添加成功!");
             this.txtDepartmentName.Text = "";
             GridViewDepartment.DataBind();
             this.DropDownList_Search.DataBind();
             GridViewDepartment.PageIndex = GridViewDepartment.PageCount - 1;   //  将当前显示页的索引转到最后一页
             GridViewDepartment.DataBind();         //重新绑定数据,这是十分重要,这样才能到达最后一页
             int lastSize = GridViewDepartment.Rows.Count;
             this.GridViewDepartment.Rows[lastSize - 1].BackColor = Color.Red;
         }
         catch (Exception)
         {
             Js_ShowMessage("对不起,添加失败!");
             throw;
         }
     }
 }
 //绑定Dropdownlist
 public void DropDownList_Bind()
 {
     EFModel.Database1Entities context = new EFModel.Database1Entities();
     DropDownList_Search.Items.Clear();
     DropDownList_Search.Items.Add(new ListItem("请选择您要搜素的事项名称", "0"));
     var query = from t in context.MatterRelatedDepartment orderby t.事项ID where t.部门ID == departmentId select t.事项名称;
     int y = 1;
     foreach (var q1 in query)
     {
         DropDownList_Search.Items.Add(new ListItem(q1.ToString(),y.ToString()));
         y++;
     }
     DropDownList_office.Items.Clear();
     DropDownList_office.Items.Add(new ListItem("请选择科室名称", "0"));
     var query1 = from t1 in context.Office where t1.所属部门ID == departmentId select t1.科室名称;
     int x = 1;
     foreach (var q1 in query1)
     {
         DropDownList_office.Items.Add(new ListItem(q1.ToString(), x.ToString()));
         x++;
     }
 }
        //代码绑定gridview
        public void Bind()
        {
            EFModel.Database1Entities context = new EFModel.Database1Entities();
            string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            name = HttpContext.Current.Session["UserName"].ToString();
            var q = from t in context.Department where t.部门名称 == name select t.部门ID;
            foreach (var v in q)
            {
                departmentId = v;
            }
            conn.Open();
            string sqlStr = "select 步骤号,事项名称 from MatterRelatedDepartment where 部门ID='" + departmentId + "'";
            SqlDataAdapter myda = new SqlDataAdapter(sqlStr, conn);
            DataSet myds = new DataSet();
            myda.Fill(myds, name);
            GridView1.DataSource = myds;
            GridView1.DataBind();
            conn.Close();

            //MatterRelatedThis.DepartmentId = departmentId;
            //ClientScript.RegisterStartupScript(ClientScript.GetType(), "sclo", "<script>DropDownList_Bind();</script>");
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string officeName = "";
            if (this.DropDownList_office.SelectedIndex == 0)
            {
                officeId = 0;
            }
            else
            {
                officeName = this.DropDownList_office.SelectedItem.Text;
                EFModel.Database1Entities context = new EFModel.Database1Entities();
                var query = from t in context.Office where t.科室名称 == officeName select t.科室ID;
                foreach (var q in query)
                {
                    officeId = q;
                }
            }
            string stepDescript = officeName + "." + this.address.Text + "." + this.person.Text + "." + this.remark.Text;

            string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            try
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                if (officeId == 0)
                {
                    cmd.CommandText = "update MatterRelatedDepartment set 科室ID=null,步骤描述='" + stepDescript + "' where 步骤号='" + a + "' AND 事项名称='" + strName + "'";
                }
                else
                {
                    cmd.CommandText = "update MatterRelatedDepartment set 科室ID='" + officeId + "',步骤描述='" + stepDescript + "' where 步骤号='" + a + "' AND 事项名称='" + strName + "'";
                }
                cmd.ExecuteNonQuery();
                conn.Close();
                Js_ShowMessage("保存成功!");
                this.DropDownList_office.Enabled = false;
                this.address.Enabled = false;
                this.person.Enabled = false;
                this.remark.Enabled = false;
                this.btnSave.Enabled = false;
                this.btnEdit.Enabled = true;
            }
            catch (Exception)
            {
                Js_ShowMessage("对不起,保存失败了!");
                throw;
            }
        }
        //事项详情
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "detailed")
            {
                this.address.Text = "";
                this.person.Text = "";
                this.remark.Text = "";
                this.DropDownList_office.SelectedIndex = 0;
                this.btnEdit.Enabled = true;
                this.btnSave.Enabled = false;
                this.DropDownList_office.Enabled = false;
                this.address.Enabled = false;
                this.person.Enabled = false;
                this.remark.Enabled = false;
                //this.btnEdit.Enabled = true;
                //Button btn = (Button)e.CommandSource;
                //GridViewRow row = (GridViewRow)btn.Parent.Parent;
                //a = int.Parse(row.Cells[0].Text);
                //strName = row.Cells[1].Text;
                //Label1.Text = strName + "第" + a + "步>>步骤描述";
                //div1.Visible = true;
                //EFModel.Database1Entities context = new EFModel.Database1Entities();
                //var q = from t in context.MatterRelatedDepartment where t.步骤号 == a where t.事项名称 == strName select t.步骤描述;
                //foreach (var x in q)
                //{
                //    this.TextArea1.Value = x;
                //}
                //if (this.TextArea1.InnerText == "")
                //{
                //    this.TextArea1.Value = "尚无描述,请您描述该步骤的要求!";
                //}
                //this.TextArea1.Disabled = true;
                Button btn = (Button)e.CommandSource;
                GridViewRow row = (GridViewRow)btn.Parent.Parent;
                a = int.Parse(row.Cells[0].Text);
                strName = row.Cells[1].Text;
                HiddenField1.Value = strName;
                Label1.Text = strName + "第" + a + "步>>步骤描述";
                EFModel.Database1Entities context = new EFModel.Database1Entities();
                var query = from t in context.MatterRelatedDepartment where t.步骤号 == a where t.事项名称 == strName select t.步骤描述;
                foreach (var q in query)
                {
                    if (q == null)
                    {
                        return;
                    }
                    else
                    {
                        s = q.ToString();
                    }
                }
                string[] ss = new string[] { };
                ss = s.Split('.');
                for (int i = 0; i < this.DropDownList_office.Items.Count; ++i)
                {
                    if (this.DropDownList_office.Items[i].Text == ss[0])
                    {
                        //this.DropDownList_office.Items[i].Selected = true;
                        this.DropDownList_office.SelectedIndex = i;
                    }
                    //else
                    //{
                    //    this.DropDownList_office.Items[i].Selected = false;
                    //}
                }
                this.address.Text = ss[1];
                this.person.Text = ss[2];
                this.remark.Text = ss[3];

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断是否登录,减少异常
            if (Session.IsNewSession)
            {
                Session["UserFlag"] = false;
                Response.Redirect("../Account/LoginWebForm.aspx");
                return;
            }
            if (Session["UserFlag"].ToString() == "false")
            {
                Response.Redirect("../Account/LoginWebForm.aspx");
                return;
            }
            if (Session["UserType"].ToString() != "部门")
            {
                Response.Redirect("../Index.aspx");
                return;
            }
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "SELECT * FROM ReplyInfo WHERE 回复对象='" + Session["UserName"] + "' AND isRead='否' ORDER BY 回复时间 DESC";
            SqlDataAdapter adpter = new SqlDataAdapter(sql, conn);
            DataTable table_UnRead = new DataTable();
            adpter.Fill(table_UnRead);
            string strHtml = string.Empty;
            EFModel.Database1Entities entity = new EFModel.Database1Entities();
            for (int i = 0; i < table_UnRead.Rows.Count; i++)
            {
                DataRow row = table_UnRead.Rows[i];
                strHtml += "<div class=\"info_item\">";
                strHtml += row[1] + ":" + row[4] + "  " + row[5];
                strHtml += "<span class=\"reply_span\">回复</span>";
                strHtml += "<div class=\"reply_div\">";
                strHtml += "<textarea style=\"height:20px;width:400px;\">请在此输入回复内容</textarea>"
                    + "<input class=\"replyButton\" type=\"button\" value=\"发表\" data-messageid=\""
                    + row[2] + "\" data-replyuser=\"" + Session["UserName"] + "\" data-replyobject=\""
                    + row[1] + "\" data-objectid=\"" + row[0] + "\"/>";
                strHtml += "</div><br/>";
                var q = from t in entity.LeaveMessagesInfo
                        where t.留言ID == (int)row[2]
                        select t;
                var message = q.First();
                strHtml += "<div class=\"message_content\">" + message.留言用户 + "向"
                    + message.留言部门ID + "留言:" + message.留言内容 + "  " + message.留言时间 + "</div>";
                if ((int)row[7] != -1)
                {
                    var q2 = from t in entity.ReplyInfo
                             where t.回复ID == (int)row[7]
                             select t;
                    var reply = q2.First();
                    strHtml += "<div class=\"reply_content\">" + reply.回复用户 + "回复"
                        + reply.回复对象 + ":" + reply.回复内容 + "  " + reply.回复时间 + "</div>";
                }
                strHtml += "</div>";
                //将“否”改为“是”
                string sql2 = "UPDATE ReplyInfo SET isRead='是' WHERE 回复ID='" + row[0] + "'";
                SqlCommand cmd = new SqlCommand(sql2, conn);
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
                catch { }
                finally
                {
                    conn.Close();
                }
            }

            if (strHtml == string.Empty)
            {
                strHtml += "您没有最新动态!";
            }
            content_right_message.InnerHtml = strHtml;
            Label1.Text = "第1页/共1页";
        }