前台.aspx加上一段JavaScript代码
// 点击复选框时触发事件
function postBackByObject()
{
var o = window.event.srcElement;
if (o.tagName == "INPUT" && o.type == "checkbox")
{
__doPostBack("","");
}
}
后台.aspx.cs
if (!IsPostBack)
{
TreeView1.Attributes.Add("onclick", "postBackByObject()");//javascript控制checkbox选择框
}
**************************************************
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Collections.Generic;
//using System.Windows.Forms;
public partial class FileBooks_UpFilesAdd : System.Web.UI.Page
{
//private TreeView treeView1;
//模板
Model.Files MR = new Model.Files();
//业务
BLL.Files BR = new BLL.Files();
protected void Page_Load(object sender, EventArgs e)
{
//DataSet ds=new DataSet();
//DBUtility.SqlHelper sql = new DBUtility.SqlHelper();
//ds = sql.ExecuteQuery(System.Configuration.ConfigurationManager.ConnectionStrings["DB"].ConnectionString, CommandType.Text, "select * from Department ", null);
//DataView dvTree = new DataView(ds.Tables[0]);
//this.ViewState["ds"] = ds;
AddTree(0, (TreeNode)null, dvTree);
//AddTree(0, (TreeNode)null);
LoginLogic.MatchLoad("../", "FileBooks_UpFilesAdd");
if (!IsPostBack)
{
DropLoadSet();
Panel1.Visible = false;
Panel2.Visible = false;
TreeView1.Attributes.Add("onclick", "postBackByObject()");//javascript控制checkbox选择框
this.btnOk.Visible = false;
}
}
public void DropLoadSet()
{
//密级
KeyValue_MJ.DataTextField = "Text";
KeyValue_MJ.DataValueField = "Value";
KeyValue_MJ.DataSource = EnumGet.MiJiGetEnum();
KeyValue_MJ.DataBind();
KeyValue_MJ.SelectedIndex = KeyValue_MJ.Items.Count - 1;
//紧急
KeyValue_JJDJ.DataTextField = "Text";
KeyValue_JJDJ.DataValueField = "Value";
KeyValue_JJDJ.DataSource = EnumGet.JinJiGetEnum();
KeyValue_JJDJ.DataBind();
KeyValue_JJDJ.SelectedIndex = KeyValue_JJDJ.Items.Count - 1;
//文件分类
KeyValue_WJFL.DataTextField = "Text";
KeyValue_WJFL.DataValueField = "Value";
KeyValue_WJFL.DataSource = EnumGet.WenJianGetEnum();
KeyValue_WJFL.DataBind();
KeyValue_WJFL.SelectedIndex = KeyValue_WJFL.Items.Count - 1;
//公文
KeyValue_GWLB.DataTextField = "Text";
KeyValue_GWLB.DataValueField = "Value";
KeyValue_GWLB.DataSource = EnumGet.GongWenGetEnum();
KeyValue_GWLB.DataBind();
KeyValue_GWLB.SelectedIndex = KeyValue_GWLB.Items.Count - 1;
//卷案
KeyValue_SSJA.DataTextField = "ANMC";
KeyValue_SSJA.DataValueField = "ID";
KeyValue_SSJA.DataSource = EnumGet.RollGetEnum();
KeyValue_SSJA.DataBind();
KeyValue_SSJA.SelectedIndex = KeyValue_SSJA.Items.Count - 1;
}
/// <summary>
/// 上传一个文件
/// </summary>
/// <returns></returns>
public string UPdateFile()
{
if (!string.IsNullOrEmpty(this.KeyValue_FJPath.PostedFile.FileName))
{
string fulname = this.KeyValue_FJPath.PostedFile.FileName;
string filename = fulname.Substring(fulname.LastIndexOf("//") + 1);
string type = fulname.Substring(fulname.LastIndexOf(".") + 1);
if (type == "aspx" || type == "asp" || type == "exe")
{
throw new Exception("不允许上传执行性文件");
}
string GuidStr = Guid.NewGuid().ToString() + "-" + filename;
string FileUpdatePathFull = AppDomain.CurrentDomain.BaseDirectory + "FileBooks//UpLoadFiles//" + GuidStr;
this.KeyValue_FJPath.PostedFile.SaveAs(FileUpdatePathFull);
return GuidStr;
}
else
{
return "";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (this.TextBox1.Text == "")
{
throw new Exception("可查看人员不能为空!");
}
if (KeyValue_WJH.Text == "")
{
throw new Exception("文件号不能为空!");
}
if (KeyValue_BJBT.Text == "")
{
throw new Exception("文件标题不能为空!");
}
DateTime QKeyValue_FWRQ = new DateTime();
if (!DateTime.TryParse(KeyValue_FWRQ.Text, out QKeyValue_FWRQ))
{
throw new Exception("发文日期格式不正确!");
}
int QDYYS = 0;
if (!int.TryParse(KeyValue_DYYS.Text, out QDYYS))
{
throw new Exception("打印页数不正确!");
}
int QWJYS = 0;
if (!int.TryParse(KeyValue_WJYS.Text, out QWJYS))
{
throw new Exception("文件页数不正确!");
}
MR.READNAME = TextBox1.Text;
MR.WJH = KeyValue_WJH.Text;
MR.BJBT = KeyValue_BJBT.Text;
MR.FWDW = KeyValue_FWDW.Text;
MR.MJ = KeyValue_MJ.SelectedValue;
MR.WJFL = KeyValue_WJFL.SelectedValue;
MR.WJYS = QWJYS;
MR.BZ = KeyValue_BZ.Text;
MR.FJPath = UPdateFile();
MR.WJZTC = KeyValue_WJZTC.Text;
MR.WJFBT = KeyValue_WJFBT.Text;
MR.FWRQ = QKeyValue_FWRQ;
MR.JJDJ = KeyValue_JJDJ.SelectedValue;
MR.GWLB = KeyValue_GWLB.SelectedValue;
MR.DYYS = QDYYS;
MR.SSJA = KeyValue_SSJA.SelectedValue;
MR.SCR = SessionInclude.SessionId;
BR.Add(MR);
MessageBox.Show("添加成功");
KeyValue_WJH.Text = "";
KeyValue_BJBT.Text = "";
KeyValue_FWDW.Text = "";
KeyValue_WJYS.Text = "";
KeyValue_WJZTC.Text = "";
KeyValue_WJFBT.Text = "";
KeyValue_FWRQ.Text = "";
KeyValue_DYYS.Text = "";
TextBox1.Text = "";
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
protected void LinbAdd_Click(object sender, EventArgs e)
{
Panel1.Visible = true;
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet ds = new DataSet();
DBUtility.SqlHelper sql = new DBUtility.SqlHelper();
string treeSql = "";
if (RadioButtonList1.SelectedValue.Equals("0"))//以部门选择可以阅读人员
{
this.CheckBoxList1.Visible = false;
this.TreeView1.Visible = true;
treeSql = "select * from Department";
ds = sql.ExecuteQuery(System.Configuration.ConfigurationManager.ConnectionStrings["DB"].ConnectionString, CommandType.Text, treeSql.ToString(), null);
DataView dvTree = new DataView(ds.Tables[0]);
this.ViewState["ds"] = ds;
this.TreeView1.Nodes.Clear();
//AddTree(0, (TreeNode)null, dvTree);
AddTree(0, (TreeNode)null);
}
//以角色选择可以阅读的人员
else
{
this.TreeView1.Visible = false;
this.CheckBoxList1.Visible = true;
treeSql = "select * from User_Priv";
ds = sql.ExecuteQuery(System.Configuration.ConfigurationManager.ConnectionStrings["DB"].ConnectionString, CommandType.Text, treeSql.ToString(), null);
this.CheckBoxList1.DataTextField = "PrivName";
this.CheckBoxList1.DataValueField = "PrivId";
CheckBoxList1.DataSource = ds;
CheckBoxList1.DataBind();
}
Panel2.Visible = true;
}
//递归添加树的节点 参数ParentID为树的最大父结点
public void AddTree(int ParentID, TreeNode pNode)
{
DataSet ds = (DataSet)this.ViewState["ds"];
DataView dvTree = new DataView(ds.Tables[0]);
//过滤ParentID,得到当前的所有子节点 ParentID为父节点ID
dvTree.RowFilter = "[parentDept] = " + ParentID;
//循环递归
foreach (DataRowView Row in dvTree)
{
//声明节点
TreeNode Node = new TreeNode();
绑定超级链接
//Node.NavigateUrl = "List.aspx?Item_ID=" + Row["Item_ID"].ToString();
//开始递归
if (pNode == null)
{
//添加根节点
Node.Text = Row["DeptName"].ToString();
Node.Value = Row["ID"].ToString();
TreeView1.Nodes.Add(Node);
//Node.Expanded = true; //节点状态展开
AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
}
else
{
//添加当前节点的子节点
Node.Text = Row["DeptName"].ToString();
Node.Value = Row["ID"].ToString();
pNode.ChildNodes.Add(Node);
//pNode.Nodes.Add(Node);
//Node.Expanded = true; //节点状态展开
AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
}
}
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
//int departmentId = Convert.ToInt32(this.TreeView1.SelectedNode.Value);
//TreeView1.Attributes.Add("onclick", "postBackByObject()");
//SetChildChecked(e.no);
}
/// <summary>
/// treeview中的checkbox选择项发生变化时触发该事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{
SetChildChecked(e.Node);
this.btnOk.Visible = true;
List<Int32> depId = new List<int>();
string s = "";
foreach (TreeNode t in this.TreeView1.CheckedNodes)
{
s += t.Text + ",";
depId.Add(Convert.ToInt32(t.Value));//获取所选部门ID
}
//foreach (int a in depId)
//{
// Response.Write(a.ToString());
//}
//Response.Write(s);
if (depId.Count!=0)
{
StringBuilder sb = new StringBuilder();//拼接查询字符串
sb.Append("select UserName from Users where DeptId in");
sb.Append("(");
foreach (int a in depId)
{
sb.Append(a);
sb.Append(",");
}
sb.Remove(sb.Length - 1, 1);
sb.Append(")");
DataSet ds = new DataSet();
DBUtility.SqlHelper sql = new DBUtility.SqlHelper();
ds = sql.ExecuteQuery(System.Configuration.ConfigurationManager.ConnectionStrings["DB"].ConnectionString, CommandType.Text, sb.ToString(), null);
GridView1.DataSource = ds;
GridView1.DataKeyNames = new String[] { "UserName" };
GridView1.DataBind();
}
foreach (TreeNode n in this.TreeView1.Nodes)
{
if (n.ChildNodes.Count > 0)
GetSelectedValue(n.ChildNodes);
if (n.Checked)
{
//depId.Add(Convert.ToInt32(n.Value));
//Response.Write(n.Value);
//TextBox1.Text = n.Text;
//s.Append(n.Text);
//Response.Write("<br/>");
}
}
//TextBox1.Text = s.ToString();
}
void GetSelectedValue(TreeNodeCollection tc)
{
//StringBuilder s = new StringBuilder();
foreach (TreeNode n in tc)
{
if (n.ChildNodes.Count > 0)
GetSelectedValue(n.ChildNodes);
if (n.Checked)
{
//depId.Add(Convert.ToInt32(n.Value));
//Response.Write(n.Value);
//TextBox1.Text = n.Text;
//Response.Write("<br/>");
//s.Append(n.Text);
}
}
//TextBox1.Text = s.ToString();
}
/// <summary>
/// 当父节点选择时,子节点全选
/// </summary>
/// <param name="parentNode">父节点</param>
private void SetChildChecked(TreeNode parentNode)
{
foreach (TreeNode node in parentNode.ChildNodes)
{
node.Checked = parentNode.Checked;
if (node.ChildNodes.Count > 0)
{
SetChildChecked(node);
}
}
}
/// <summary>
/// gridview中父checkbox选择时,子checkbox全选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void SelectAll(object sender, EventArgs e)
{
bool isChecked = ((CheckBox)(GridView1.HeaderRow.Cells[0].FindControl("cbHead"))).Checked;
foreach (GridViewRow gvRow in GridView1.Rows)
{
((CheckBox)(gvRow.Cells[0].FindControl("cbItem"))).Checked = isChecked;
}
}
protected void btnOk_Click(object sender, EventArgs e)
{
string getName = "";
foreach (GridViewRow row in this.GridView1.Rows)
{
CheckBox chk = row.FindControl("cbItem") as CheckBox;
if (chk.Checked)
{
//int bookId = Convert.ToInt32((row.FindControl("UserName") as Label).Text);
//int BoxID = Convert.ToInt32( .DataKeys[gdvWishList.Rows[i].DataItemIndex].Value);
string name = GridView1.DataKeys[row.RowIndex].Value.ToString();
getName += name + ",";
}
}
TextBox1.Text = getName;
//btnOk.Visible = false;
//GridView1.Visible = false;
//GridView1.DataSource = "";
//GridView1.DataBind();
//DBUtility.SqlHelper sql = new DBUtility.SqlHelper();
//string insertText = "insert into Files(READNAME)values " + getName.ToString();
//sql.ExecuteNonQuery(System.Configuration.ConfigurationManager.ConnectionStrings["DB"].ConnectionString, CommandType.Text, insertText.ToString(), null);
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.btnOk.Visible = true;
this.GridView1.DataSource = "";
this.GridView1.DataBind();
List<Int32> roleId = new List<int>();
for (int i = 0; i < this.CheckBoxList1.Items.Count; i++)
{
if (this.CheckBoxList1.Items[i].Selected == true)
{
roleId.Add(Convert.ToInt32(this.CheckBoxList1.Items[i].Value));
}
}
if (roleId.Count!=0)
{
StringBuilder sb = new StringBuilder();//拼接查询字符串
sb.Append("select UserName from Users where PrivId in");
sb.Append("(");
foreach (int a in roleId)
{
sb.Append(a);
sb.Append(",");
}
sb.Remove(sb.Length - 1, 1);
sb.Append(")");
DataSet ds = new DataSet();
DBUtility.SqlHelper sql = new DBUtility.SqlHelper();
ds = sql.ExecuteQuery(System.Configuration.ConfigurationManager.ConnectionStrings["DB"].ConnectionString, CommandType.Text, sb.ToString(), null);
GridView1.DataSource = ds;
GridView1.DataKeyNames = new String[] { "UserName" };
GridView1.DataBind();
}
}
}