28. januar 2007 - 10:55Der er
10 kommentarer og 1 løsning
Expand Treeview ved redirect
Hej,
Nu har jeg forsøgt med alt muligt de sidste mange mange dage, og kan bare ikke få det til at fungere/finde noget. Jeg håber der er en herinde, der kan hjælpe mig.
Jeg har et treeview, hvor jeg henter indholdet fra en database. Fjerner jeg "root.NavigateUrl = "Default.aspx?id=" + ParentNode[loop, 0];" fra min kode, så expander den fint, den tekst jeg trykker på. Har jeg kodelinien på (som jo er nødvendigt, for vide, hvilken side der skal tilgås) vil den bare ikke expande.
Håber der er en, der kan hjælpe mig med, at få noget til at fungere....Jeg er ved at blive desperat :-/
mvh simsen
Min kode:
using System; using System.Data; using System.Configuration; 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.OleDb;
public partial class _Default : System.Web.UI.Page { private OleDbConnection connection = null; public OleDbCommand command = null; public string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\Inetpub\\wwwroot\\Test\\Menu3\\menu.mdb;";
void fill_Tree() { /* * Fill the treeview control Root Nodes From Parent Table * and child nodes from ChildTables */ connection = new OleDbConnection(connectionString); connection.Open();
/* * Query the database */ command = new OleDbCommand(); command.Connection = connection;
DataTable myDataTable = new DataTable(); myDataTable.Columns.Add(new DataColumn("CategoryID", Type.GetType("System.String"))); myDataTable.Columns.Add(new DataColumn("ParentCategoryID", Type.GetType("System.String"))); myDataTable.Columns.Add(new DataColumn("CategoryName", Type.GetType("System.String"))); command.CommandText = "SELECT * FROM CATEGORIES WHERE ParentCategoryID = 0";
command.Parameters.Clear();
/* *Define and Populate the SQL DataReader */ OleDbDataReader myReader = command.ExecuteReader();
/* * Dispose the SQL Command to release resources */ command.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. */ while (myReader.Read()) { ParentNode[count, 0] = myReader.GetValue(myReader.GetOrdinal("CategoryID")).ToString(); ParentNode[count++, 1] = myReader.GetValue(myReader.GetOrdinal("CategoryName")).ToString(); }
/* * Close the SQL datareader to release resources */ //myReader.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 root = new TreeNode(); root.Text = ParentNode[loop, 1]; root.PopulateOnDemand = false; root.SelectAction = TreeNodeSelectAction.SelectExpand;
//root.Target = "_self";
/* * Give the url of your page */ root.NavigateUrl = "Default.aspx?id=" + ParentNode[loop, 0];
myReader.Close();
/* * Now that we have [ParentId] in our array we can find out child modules */ OleDbCommand Module_SqlCmd = new OleDbCommand("Select * from CATEGORIES where ParentCategoryID =" + ParentNode[loop, 0], connection);
Læg et svar og du får dine points. Jeg har brugt din oprindelige - dog med nogle modifikationer. Og den virker også.......den har bare problemer med at få værdien @n ud....men det skal jeg nok også få til at virke.
Hvis du er interesseret i den kode, det er endt ud med - så blev det sådan:
using System; using System.Data; using System.Configuration; 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.OleDb; using System.Xml; using System.Xml.Xsl; using System.Xml.XPath; using System.IO;
public partial class _Default : System.Web.UI.Page { private OleDbConnection connection = null; public OleDbCommand command = null; public string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\Inetpub\\wwwroot\\Test\\Menu\\menu.mdb;";
//Henter Niveau 1 ud public void getProductGroups(TreeView n) { connection = new OleDbConnection(connectionString); command = new OleDbCommand(); command.Connection = connection; connection.Open();
DataTable myDataTable = new DataTable(); myDataTable.Columns.Add(new DataColumn("CategoryID", Type.GetType("System.String"))); myDataTable.Columns.Add(new DataColumn("ParentCategoryID", Type.GetType("System.String"))); myDataTable.Columns.Add(new DataColumn("CategoryName", Type.GetType("System.String"))); myDataTable.Columns.Add(new DataColumn("HasChilds", Type.GetType("System.String"))); command.CommandText = "SELECT CategoryID, ParentCategoryID, CategoryName, HasChilds FROM CATEGORIES WHERE ParentCategoryID = 0";
while (myReader.Read()) { TreeNode t = new TreeNode(myReader["CategoryName"].ToString(), myReader["CategoryID"].ToString()); //if (Convert.ToInt32(myReader["HasChilds"]) == 1) t.PopulateOnDemand = true; t.SelectAction = TreeNodeSelectAction.Expand; t.Expanded = false;
n.Nodes.Add(t); }
}
//Henter Niveau 2 ud public void getProducts(TreeNode n) { connection = new System.Data.OleDb.OleDbConnection(connectionString); System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(); command.Connection = connection; connection.Open();
while (myReader.Read()) { TreeNode t = new TreeNode(myReader["CategoryName"].ToString(), myReader["CategoryID"].ToString()); t.SelectAction = TreeNodeSelectAction.Expand; t.Expanded = false; n.ChildNodes.Add(t); } }
private void ExpandNodes(string valuepath) { string[] tmp = valuepath.Split('/'); string tmpValuePath = string.Empty; for (int i = 0; i < tmp.Length; i++) {
if (i == 0) tmpValuePath = tmp[i]; else tmpValuePath += "/" + tmp[i];
TreeNodeEventArgs e = new TreeNodeEventArgs(ProductsTree.FindNode(tmpValuePath)); e.Node.ChildNodes.Clear(); e.Node.PopulateOnDemand = false; ProductsTree_TreeNodePopulate(ProductsTree, e); ProductsTree.FindNode(tmpValuePath).Expand(); ProductsTree.FindNode(tmpValuePath).Selected = true; } }
protected void ProductsTree_TreeNodePopulate(object sender, TreeNodeEventArgs e) { //hvis der ikke er nogen child nodes til noden if (e.Node.Depth == 0) { getProducts(e.Node); } }
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.