Posted: 12/1/2010
Hi,I am working on an asp.net web application that uses treeview control to display hierarchical data from an xml data source. When I changed the xml data source the treeview doesn't update itself. The treeview data source is asp XMLDataSource that is transformed by an xlst file. The xml data is generated from SQL database and dynamically bind to the XmlDataSource. Is it possible to use this method to bind data to treeview control?
Thanks,
Sumit Arora
Please explain your issue with relevant code.
The problem which I'm having is I'm using asp.net treeview control. And there is a button on the same page where I add a new node to a tree. When the page postbacks it doesn't show the new node which I added in the treeview.
I'm binding the treeview like this:
Test test=new Test(); test.ID= Convert.ToInt32(Request.QueryString["Id"].Trim()); DataSet ds= TestBLL.GetOrganizationTreeByID(test.ID); if (ds.Tables[0].Rows.Count > 0) { foreach (DataRow dr1 in DsTree.Tables[0].Rows) { if (dr1["parentid"] == DBNull.Value) { dr1["parentid"] = "0"; } } DataRow dr = ds.Tables[0].NewRow(); dr["UnitID"] = "0"; dr["OfficeID"] = ds.Tables[0].Rows[0]["OfficeID"].ToString(); dr["UnitName"] = ds.Tables[0].Rows[0]["OfficeName"].ToString(); dr["ParentID"] =DBNull.Value; dr["Rank"] = "0"; ds.Tables[0].Rows.InsertAt(dr, 0); ds.AcceptChanges(); ds.DataSetName = "Menus"; ds.Tables[0].TableName = "Menu"; DataRelation relation = new DataRelation("ParentChild", DsTree.Tables["Menu"].Columns["UnitID"], DsTree.Tables["Menu"].Columns["ParentID"], true); relation.Nested = true; ds.Relations.Add(relation); xmlDataSource.Data = ds.GetXml(); trTreeView.Visible = true; }
Mark up looks like this:
<asp:TreeView ID="treeView" runat="server" ShowCheckBoxes="All" ForeColor="#990000" AutoGenerateDataBindings="False" style="margin-right: 0px" TabIndex="2" ontreenodedatabound="Menu1_TreeNodeDataBound" DataSourceID="xmlDataSource" ShowLines="True" ontreenodecheckchanged="Menu1_TreeNodeCheckChanged"> <DataBindings> <asp:TreeNodeBinding DataMember="MenuItem" NavigateUrlField="NavigateUrl" TextField="UnitName" ToolTipField="ToolTip" TargetField="Rank" ValueField="Value" /> </DataBindings> </asp:TreeView> <asp:XmlDataSource ID="xmlDataSource" TransformFile="~/XSLT.xsl" XPath="MenuItems/MenuItem" runat="server" />
Now when I add a new node in on button click event. And calling the method that excutes the above code. It doesn't show the new item that being added.
I hope you would be able to understand now what I want to do exactly.
Found the mystery behind the problem....just need to set the EnableCahing="false" in XmlDataSource control.
<asp:XmlDataSource ID="xmlDataSource" EnableCaching="false" TransformFile="~/TransformXSLT.xsl" XPath="MenuItems/MenuItem" runat="server"/>