using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.SqlClient;

public partial class include_Productlist : System.Web.UI.UserControl
{
    static DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            
        }
        Fillfenlei1(TreeView1);
    }
    //添加数据一二级分类
    private void Fillfenlei1(TreeView myTreeView)
    {
        ds = Getleibie();
        DataRow[] datarow = ds.Tables["aa"].Select("ParrentID=0", "Showorder DESC");      
        foreach (DataRow row in datarow)
        {
            TreeNode rootnode = new TreeNode();
            string mystr = Jiequ(row["Title"].ToString());
            rootnode.Value = row["ProductID"].ToString();
            rootnode.ToolTip = row["Title"].ToString();
            rootnode.SelectAction = TreeNodeSelectAction.Expand;
          
            DataRow[] noderow = ds.Tables["aa"].Select("ParrentID=" + rootnode.Value,"Showorder DESC");
            foreach (DataRow row1 in noderow)
            {
                TreeNode node = new TreeNode();
                string nodestr = Jiequ(row1["Title"].ToString());              
                nodestr = "<a href='Showproduct.aspx?ShowID="+row1["ProductID"].ToString()+"' target='_blank'>" + nodestr + "</a>";
     
                node.Text = nodestr;
                node.Value = row1["ProductID"].ToString();
                rootnode.ChildNodes.Add(node);
               
            }
            if (row["Folder"].ToString()!="1")
            {
                mystr = "<a href='Showproduct.aspx?ShowID=" + row["ProductID"].ToString() + "' target='_blank'>" + mystr + "</a>";
            }
            rootnode.Text = mystr;
            myTreeView.Nodes.Add(rootnode);
        }
    }
    //长字截取
    public string Jiequ(string str)
    {
        if (str.Length > 16)
        {
            str = str.Substring(0, 16) + "..";
        }
        return str;
    }
    //打开类别数据库
    public DataSet Getleibie()
    {
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MYCONNECTIONSTRING"].ConnectionString);
        string cmdText = "SELECT ProductID,ParrentID,Title,Showorder,Folder FROM Product ORDER BY Showorder DESC";
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmdText, myConnection);
        try
        {
            myConnection.Open();
            da.Fill(ds, "aa");
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myConnection.Close();
        }
        return ds;
    }
}
