ASP.NET Zone

Die ASP.NET Community. Alle Antworten rund um Microsoft® Webtechnologien
Willkommen bei ASP.NET Zone. Anmelden | Registrieren | Hilfe | Impressum | ASP.NET Zone Suchplugin

Home Blogs Foren FAQ Communities Fotos Downloads
Werbeanzeige

Treeview, DataBind ,TreeView1_SelectedNodeChanged

  •  03-26-2009, 21:38

    Treeview, DataBind ,TreeView1_SelectedNodeChanged

    Hallo,

    erstmal wollt ich euch sagen, daß Ihr das Forum hier richtig gut macht.

    Nun zu meinem Problem.

    Ich will mein treeview mit einer datenbank Füllen. Das mach ich wie folgt:




       protected void Page_Load(object sender, EventArgs e)

    {

    if (!IsPostBack)

    fill_Tree();

    }



    void fill_Tree()

    {

    /*

    * Fill the treeview control Root Nodes From Parent Table

    * and child nodes from ChildTables

    */



    /*

    * Create an SQL Connection to connect to you our database

    */



    string Connst = WebConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ToString();

    SqlConnection Conn = new SqlConnection(Connst);


    Conn.Open();


    /*

    * Query the database

    */



    SqlCommand SqlCmd = new SqlCommand("Select * from SprachenTabelle ORDER BY SprachenName", Conn);


    /*

    *Define and Populate the SQL DataReader

    */



    SqlDataReader Sdr = SqlCmd.ExecuteReader();


    /*

    * Dispose the SQL Command to release resources

    */



    SqlCmd.Dispose();


    /*

    * Initialize the string ParentNode.

    * We are going to populate this string array with our ParentTable Records

    * and then we will use this string array to populate our TreeView1 Control with parent records

    */



    string[,] ParentNode = new string[100, 2];


    /*

    * Initialize an int variable from string array index

    */



    int count = 0;


    /*

    * Now populate the string array using our SQL Datareader Sdr.


    * Please Correct Code Formatting if you are pasting this code in your application.

    */



    while (Sdr.Read())

    {


    ParentNode[count, 0] = Sdr.GetValue(Sdr.GetOrdinal("SprachenID")).ToString();

    ParentNode[count++, 1] = Sdr.GetValue(Sdr.GetOrdinal("SprachenName")).ToString();


    }


    /*

    * Close the SQL datareader to release resources

    */



    Sdr.Close();


    /*

    * Now once the array is filled with [Parentid,ParentName]

    * start a loop to find its child module.

    * We will use the same [count] variable to loop through ChildTable

    * to find out the number of child associated with ParentTable.

    */



    for (int loop = 0; loop < count; loop++)

    {


    /*

    * First create a TreeView1 node with ParentName and than

    * add ChildName to that node

    */



    TreeNode Sprache = new TreeNode();

    Sprache.Text = ParentNode[loop, 1];



    Sprache.NavigateUrl = "~/Sprache.aspx";


    SqlCommand Module_SqlCmd = new SqlCommand("SELECT * FROM GruppenTabelle WHERE SprachenId =" + ParentNode[loop, 0]+"ORDER BY Gruppenname " , Conn);


    SqlDataReader Module_Sdr = Module_SqlCmd.ExecuteReader();


    while (Module_Sdr.Read())

    {


    // Sprachen Modul hinzufügen zum Root Knoten


    TreeNode Gruppe= new TreeNode();


    Gruppe.Text = Module_Sdr.GetValue(Module_Sdr.GetOrdinal("GruppenName")).ToString();


    Gruppe.NavigateUrl = "~/Gruppe.aspx";


    Sprache.ChildNodes.Add(Gruppe);


    }


    Module_Sdr.Close();


    // Add root node to TreeView

    TreeView1.Nodes.Add(Sprache);

    }



    TreeView1.CollapseAll();

    Conn.Close();




    }


    Leider kann ich jetz TreeView1_SelectedNodeChanged nicht mehr auswerten bzw. überprüfen.

    Kann da evtl wer helfen oder mir ne besser Möglichkeit sagen?

    Mfg Jens

Das ganze Thema ansehen
Powered by Community Server (Commercial Edition)