TreeView

public partial class admin_Admin_ProductFlAdd : System.Web.UI.Page
{
    static int id;
    OleDbConnection con = new OleDbConnection(DataProvider.ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["admin"] == null)
            {
                Response.Redirect("Admin_login.aspx");
            }
            DataSet odr = SqlHelper.ExecuteDataSet("select * from ProductCategories");
            this.ViewState["odr"] = odr;

            AddTree(0, (TreeNode)null);
        }
    }
    public void AddTree(int ParentID, TreeNode pNode)
    {
        try
        {
            DataSet ds = (DataSet)this.ViewState["odr"];
            DataView dvTree = new DataView(ds.Tables[0]);
            //过滤ParentID,得到当前的所有子节点
            dvTree.RowFilter = "[ProFatherTag] = " + ParentID;

            foreach (DataRowView Row in dvTree)
            {
                TreeNode Node = new TreeNode();
                if (pNode == null)
                { //添加根节点
                    Node.Text = Row["CatName"].ToString();
                    Node.Value = Row["ProCateID"].ToString();
                    TreeView1.Nodes.Add(Node);
                    Node.Expanded = true;
                    AddTree(Int32.Parse(Row["ProCateID"].ToString()), Node); //再次递归
                }
                else
                { //?添加当前节点的子节点
                    Node.Text = Row["CatName"].ToString();
                    Node.Value = Row["ProCateID"].ToString();
                    pNode.ChildNodes.Add(Node);
                    Node.Expanded = true;
                    AddTree(Int32.Parse(Row["ProCateID"].ToString()), Node); //再次递归
                }
            }
        }
        catch (Exception e1)
        {
            MessageBox.Show("" + e1.Message + "", "Admin_ProductFlAdd.aspx");
        }
    }
   

    protected void  Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtClassName.Text == "")
            {
                MessageBox.Show("请输入你要添加的分类", "Admin_ProductFlAdd.aspx");
            }
            else
            {
                 string d = "";
                id = int.Parse(this.TreeView1.SelectedNode.Value);
                string a = Convert.ToString(SqlHelper.ExecuteScalar(con, "select Flag from ProductCategories where mailto:ProCateID=@ProCateID%22,new OleDbParameter("@ProCateID",id)));
                string a1 = "select top 1 flag from  ProductCategories  where Flag like '" + a + "._' order by Flag desc";

                string b = Convert.ToString(SqlHelper.ExecuteScalar(con, a1));
                if (b == "")
                {

                    d = a + ".1";
                    //SqlHelper.ExecuteNonQuery("insert into ProductCategories (ProFatherTag,CatName,Flag)values(" + id + ",'" + this.txtClassName.Text + "','" + d + "')");
                    SqlHelper.ExecuteNonQuery("insert into ProductCategories (ProFatherTag,CatName,Flag)values(@ProFatherTag,@CatName,@Flag)",new OleDbParameter("@ProFatherTag",id),new OleDbParameter("@CatName",this.txtClassName.Text),new OleDbParameter("@Flag",d));
                    MessageBox.Show("添加分类成功", "Admin_ProductFlAdd.aspx");
                }
                else
                {
                    string[] c = b.Split('.');

                    c[c.Length - 1] = (Convert.ToInt32(c[c.Length - 1]) + 1).ToString();
                    for (int i = 0; i < c.Length; i++)
                    {
                       
                        if (i == c.Length - 1)
                        {
                            d += c[i];
                            break;
                        }
                        d += c[i] + ".";
                    }

                    OleDbDataReader odr = SqlHelper.ExecuteReader("select Flag from ProductCategories where CatName='" + this.txtClassName.Text + "'");
                    if (odr.Read())
                    {
                        MessageBox.Show("该分类已被添加过", "Admin_ProductFlAdd.aspx");
                    }
                    else
                    {
                        //SqlHelper.ExecuteNonQuery("insert into ProductCategories (ProFatherTag,CatName,Flag)values(" + id + ",'" + this.txtClassName.Text + "','" + d + "')");
                        SqlHelper.ExecuteNonQuery("insert into ProductCategories (ProFatherTag,CatName,Flag)values(@ProFatherTag,@CatName,@Flag)", new OleDbParameter("@ProFatherTag", id), new OleDbParameter("@CatName", this.txtClassName.Text), new OleDbParameter("@Flag", d));
                        MessageBox.Show("添加分类成功", "Admin_ProductFlAdd.aspx");
                    }
                }
            }
        }
        catch (Exception e1)
        {
            MessageBox.Show("" + e1.Message + "", "Admin_ProductFlAdd.aspx");
        }
    }

  
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        try
        {
            id = int.Parse(this.TreeView1.SelectedNode.Value);


            OleDbDataReader odr = SqlHelper.ExecuteReader("select * from ProductCategories where mailto:ProCateID=@ProCateID%22,new OleDbParameter("@ProCateID",id));
            if (odr.Read())
            {
                this.txtFlClassName.Text = odr["CatName"].ToString();
            }
        }
        catch (Exception e1)
        {
            MessageBox.Show("" + e1.Message + "", "Admin_ProductFlAdd.aspx");
        }

    }
}

你可能感兴趣的:(treeview)