zoukankan      html  css  js  c++  java
  • Asp.Net中动态初始化、添加TreeView中数据

     这里主要的任务是一个TreeView,通过TreeView可以动态添加数据,这里是添加一个公司部门,比如董事局、研发、财务等。数据库名为department,一张表表名为dept.四个字段,分别是deptID(编号,通过函数自动生成),deptName(部门名称),deptLevel(部门等级),parentNo(父结点编号)。

    源代码如下:

            private static DataSet ds;

            private void Page_Load(object sender, System.EventArgs e)

            {

                // 在此处放置用户代码以初始化页面

                this.LblMessage.Text="";

                this.BtnDelete.Attributes.Add("onclick","return delConfirm()");

                if(!this.IsPostBack)

                {

                    this.initButtons();

                    this.initTree();

                   

                }

            }

            

            //初始化各个按钮的状态

            private void initButtons()

            {

                this.BtnAdd.Visible=true;

                this.BtnDelete.Visible=true;

                this.BtnEdit.Visible=true;

                this.BtnSave.Visible=false;

                this.BtnReturn.Visible=false;

            }

            //返回从部门表中的所有数据

            private DataSet ReturnDS()

            {

                SqlConnection con=DBCon.createcon();

                con.Open();

                string sql="select * from dept";

                SqlDataAdapter sda=new SqlDataAdapter(sql,con);

                DataSet ds1=new DataSet();

                sda.Fill(ds1);

                con.Close();

                return ds1;

               

            }

            //动态初始化TreeView

            private void initTree()

            {

               

                TreeNode ParentNode=new TreeNode();

                ParentNode.ID="100000000";

                ParentNode.Text="栏目树";

                this.TvDepartMent.Nodes.Add(ParentNode);

                CreateChildNode(ParentNode,"100000000");

            }

            //初始化TextBox,DropDownList

            private void initText()

            {

                this.TbDepName.Text="";

                this.TbDepName.Enabled=true;

                this.DdlLevel.Enabled=true;

            }

            //动态生成各个树结点的子结点

            private void CreateChildNode(TreeNode node,string ParentNo)

            {

                ds=this.ReturnDS();

                DataRow[] rows=ds.Tables[0].Select("parentNo='"+ParentNo+"'");

                foreach(DataRow dr in rows)

                {

                    TreeNode ChildNode=new TreeNode();

                    ChildNode.ID=dr["deptID"].ToString();

                    ChildNode.Text=dr["deptName"].ToString();

                    node.Nodes.Add(ChildNode);

                    this.CreateChildNode(ChildNode,dr["deptID"].ToString());

                }

            }

            //按添加按钮后各个按钮的状态

            private void addButtonsStatus()

            {

                this.BtnSave.Visible=true;

                this.BtnReturn.Visible=true;

                this.BtnAdd.Visible=false;

                this.BtnDelete.Visible=false;

                this.BtnEdit.Visible=false;

            }

            //添加结点

            private void BtnAdd_Click(object sender, System.Web.UI.ImageClickEventArgs e)

            {

                this.addButtonsStatus();

                ViewState["type"]="add";

                this.initText();

               

            }

            //保存

            private void BtnSave_Click(object sender, System.Web.UI.ImageClickEventArgs e)

            {

               

               

                    SqlConnection con=DBCon.createcon();

                    con.Open();

                if(ViewState["type"].ToString()=="add")

                {

                    if(this.TbDepName.Text.Trim()=="")

                    {

                        this.LblMessage.Text="<script language=javascript>alert('部门名称不能为空!');</script>";

                        return;

                    }

                    string deptName=this.TbDepName.Text.Trim();

                    string deptLevel=this.DdlLevel.SelectedValue.ToString();

                    string deptNo=AutoNo.getAutoNo("dept","deptID");

                    string parentNO=this.TvDepartMent.GetNodeFromIndex(this.TvDepartMent.SelectedNodeIndex).ID.ToString();

                    string sql="insert into dept(deptID,deptName,deptLevel,parentNo) values('"+deptNo+"','"+deptName+"','"+deptLevel+"','"+parentNO+"')";

                    SqlCommand cmd=new SqlCommand(sql,con);

                    cmd.ExecuteNonQuery();

                    con.Close();

                    this.TvDepartMent.Nodes.Clear();

                    this.initTree();

                    this.LblMessage.Text="<script language=javascript>alert('恭喜新增成功!');</script>";

                }

                if(ViewState["type"].ToString()=="edit")

                {

                    if(this.TbDepName.Text=="")

                    {

                        this.LblMessage.Text="<script language=javascript>alert('您还有选择任何一项!');</script>";

                        return;

                    }

                    string deptName=this.TbDepName.Text.Trim();

                    string deptLevel=this.DdlLevel.SelectedValue.ToString();

                    string deptID=this.TvDepartMent.GetNodeFromIndex(this.TvDepartMent.SelectedNodeIndex).ID.ToString();

                    string sql="update dept set deptName='"+deptName+"',deptLevel='"+deptLevel+"'where deptID='"+deptID+"'";

                    SqlCommand cmd=new SqlCommand(sql,con);

                    int k=cmd.ExecuteNonQuery();

                    if(k!=0)

                    {

                        this.TvDepartMent.Nodes.Clear();

                        this.initTree();

                        this.LblMessage.Text="<script language=javascript>alert('恭喜您,修改成功!');</script>";

                    }

                    else

                    {

                        this.LblMessage.Text="<script language=javascript>alert('抱歉,出错了,请您重新试一下。');</script>";

                    }

                }

            }

           //返回

            private void BtnReturn_Click(object sender, System.Web.UI.ImageClickEventArgs e)

            {

             initButtons();

             this.initText();

       

            }

           //编辑结点

            private void BtnEdit_Click(object sender, System.Web.UI.ImageClickEventArgs e)

            {

                ViewState["type"]="edit";

                this.EditTbStatus();

                this.addButtonsStatus();

            }

            //选择一个结点后TextBox,DropDownList的状态的改变

            private void SelectTbStatus()

            {

                this.TbDepName.Enabled=false;

                this.DdlLevel.Enabled=false;

            }

            //编辑状态下TextBox,DropDownList的状态的改变

            private void EditTbStatus()

            {

                this.TbDepName.Enabled=true;

                this.DdlLevel.Enabled=true;

            }

            //选择一个结点后发生的事件

            private void TvDepartMent_SelectedIndexChange(object sender, Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs e)

            {

                this.initButtons();

                string deptNo=this.TvDepartMent.GetNodeFromIndex(this.TvDepartMent.SelectedNodeIndex).ID.ToString();

                if(deptNo!="100000000")

                {

                    SqlConnection con=DBCon.createcon();

                    con.Open();

                    string sql="select * from dept where deptID='"+deptNo+"'";

                    DataSet ds=new DataSet();

                    SqlDataAdapter sda=new SqlDataAdapter(sql,con);

                    sda.Fill(ds);

                    this.TbDepName.Text=ds.Tables[0].Rows[0]["deptName"].ToString();

                    this.DdlLevel.SelectedValue=ds.Tables[0].Rows[0]["deptLevel"].ToString();

                    con.Close();

                }

                this.SelectTbStatus();

            }

            //删除某个结点

            private void BtnDelete_Click(object sender, System.Web.UI.ImageClickEventArgs e)

            {

                string deptNo=this.TvDepartMent.GetNodeFromIndex(this.TvDepartMent.SelectedNodeIndex).ID.ToString();

                if(deptNo=="100000000")

                {

                 this.LblMessage.Text="<script language=javascript>alert('此根结点不能删除。');</script>";

                 return;

                }

                SqlConnection con=DBCon.createcon();

                con.Open();

                string sql="delete from dept where deptID='"+deptNo+"'";

                SqlCommand cmd=new SqlCommand(sql,con);

                int k=cmd.ExecuteNonQuery();

                if(k!=0)

                {

                   this.LblMessage.Text="<script language=javascript>alert('删除成功!');</script>";

                    this.TvDepartMent.Nodes.Clear();

                    this.initTree();

    /p>

                }

                con.Close();

            }

           

        }

    }

  • 相关阅读:
    HTTP状态详解
    表锁和行锁
    memcache 加载(对象)所遇到的问题。资源
    php 数据导出csv 注意问题。
    文件不存在的话创建文件 文件上传所遇到的问题、
    获取文件的后缀名。phpinfo
    手机访问pc网站自动跳转手机端网站代码
    计算机网络学习-20180826
    计算机网络学习-20180811
    集线器和交换机的区别
  • 原文地址:https://www.cnblogs.com/chenbg2001/p/1357003.html
Copyright © 2011-2022 走看看