c# - Binding a tree view structure , in asp .net? -
i have tried create asp,net treeview structure using below
<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>untitled page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:treeview id="treeview1" runat="server" /> </div> </form>
and in code behind
protected void page_load(object sender, eventargs e) { if (!page.ispostback) populatetreeview(); } private void populatetreeview() { datatable treeviewdata = gettreeviewdata(); addtoptreeviewnodes(treeviewdata); } private datatable gettreeviewdata() { string selectcommand = "select top 10 idx_client,idx_branch,client_name tbl_client"; string constring =system.configuration.configurationmanager.appsettings["connectionstring"].tostring(); sqldataadapter dad = new sqldataadapter(selectcommand, constring); datatable dtbldiscuss = new datatable(); dad.fill(dtbldiscuss); return dtbldiscuss; } private void addtoptreeviewnodes(datatable treeviewdata) { dataview view = new dataview(treeviewdata); foreach (datarowview row in view) { treenode newnode = new treenode(row["client_name"].tostring(), row["idx_client"].tostring()); treeview1.nodes.add(newnode); newnode.populateondemand = true; addchildtreeviewnodes(treeviewdata, newnode); } } private void addchildtreeviewnodes(datatable treeviewdata, treenode parenttreeviewnode) { dataview view = new dataview(treeviewdata); foreach (datarowview row in view) { treenode newnode = new treenode(row["client_name"].tostring(), row["idx_client"].tostring()); parenttreeviewnode.childnodes.add(newnode); newnode.populateondemand = true; addchildtreeviewnodes(treeviewdata, newnode); } }
but gives exception
cannot evaluate expression because current thread in stack overflow state
please point out changes needed, because using treeview first time how solve exception. 1 me.
the problem lies in recursive call in addchildtreeviewnodes
:
private void addchildtreeviewnodes(datatable treeviewdata, treenode parenttreeviewnode) { dataview view = new dataview(treeviewdata); foreach (datarowview row in view) { treenode newnode = new treenode(row["client_name"].tostring(), row["idx_client"].tostring()); parenttreeviewnode.childnodes.add(newnode); newnode.populateondemand = true; // error caused recursion below addchildtreeviewnodes(treeviewdata, newnode); // calls again remove } }
what happening here inserting child node of view , try insert child node within (which in turn same thing). keep on happening until stack blown.
i suspect don't need line have marked remove this
Comments
Post a Comment