Re: TreeView Dynamic XML Binding
- From: David R. Longnecker <dlongnecker@xxxxxxxxxxxxxxxx>
- Date: Tue, 25 Mar 2008 20:02:32 +0000 (UTC)
Rich-
If I understand correctly, you're trying to create a TreeView based off of an XML file and retain the expand/collapse functionality but AVOID the postbacks and flicker, correct?
You can do this by setting the SelectAction to Expand when you databind your nodes. Here's some sample code I whipped up using your XML:
aspx:
<asp:TreeView ID="CompanyTreeXml" runat="server" DataSourceID="CompanyXml"
ExpandDepth="1" EnableClientScript="true" PopulateNodesFromClient="true" ontreenodedatabound="CompanyTreeXml_TreeNodeDataBound">
</asp:TreeView>
<asp:XmlDataSource ID="CompanyXml" runat="server" DataFile="~/Company.xml">
</asp:XmlDataSource>
code-behind:
protected void CompanyTreeXml_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
{
e.Node.SelectAction = TreeNodeSelectAction.Expand;
}
At this point, CSS or a NodeStyle would be required because the TreeView generates any Expandable node as a hyperlink and any non-expanding node as as plain text.
Since EnableClientScript is still enabled, the control generates the according JavaScript and the Expand links do not fire postbacks. By default, I think, the TreeNodeSelectAction is set to Select, not Expand, which fires the SelectedNodeChanged event.
Hope this helps!
-dl
--
David R. Longnecker
http://blog.tiredstudent.com
I'm dynamically binding an XmlDataSource to an xml file in the
Page_Load event. This XmlDataSource control is the DataSourceID for a
TreeView control. I'm receiving a null reference error prior to
entering the Page_Load event when selecting a tree text value.
Page_Load code snippet:
cSchemaXML.DataFile = @"D:\Schemas\2008\Schema_Tree.xml";
Note: The file path above will be calculated once I get it working.
This is for testing only. The code is not in a !this.IsPostBack
conditional.
Aspx page snippet:
<asp:TreeView ID="cSchemaTree" runat="server"
DataSourceID="cSchemaXML"
ExpandDepth="1">
</asp:TreeView>
<asp:XmlDataSource ID="cSchemaXML"
runat="server"></asp:XmlDataSource>
When I click on the tree node (not the Expand icon) for the select
processing, a null reference error (see below) is displayed.
The only way I can get this to work without a the error is to disable
client
side code:
EnableClientScript="False"
I prefer not to suffer the flashing and time required during
postbacks. I'm simply trying to display the xml element tree
structure. The xml being displayed is nested elements, each containing
one attribute. If I set the DataFile in the XmlDataSource control
directly, everything works fine. Do I need to set DataFile someplace
other than in the Page_Load event? Some other solution?
Note: If I click a tree node that is part of the initial display
(based on the ExpandDepth setting) I do not get the error until I
expand the tree and then click the node (or one of the expanded
nodes). This may indicate some variance between the viewstate and the
returned tree structure (only a guess).
Sample XML - the actual xml is much more complex and nested much
deeper, but this gives the idea.
<Company FullPath="Company">
<Name FullPath="Company/Name"/>
<Address FullPath="Company/Address">
<USAddress FullPath="Company/Address/USAddress">
<Street FullPath="Company/Address/USAddress/Street" />
<City FullPath="Company/Address/USAddress/City"/>
<State FullPath="Company/Address/USAddress/State" />
<ZipCode FullPath="Company/Address/USAddress/ZipCode" />
</USAddress>
<ForeignAddress FullPath="Company/Address/ForeignAddress" >
<Street FullPath="Company/Address/USAddress/Street" />
<City FullPath="Company/Address/USAddress/City"/>
<StateOrProvince
FullPath="Company/Address/USAddress/StateOrProvince"
/>
<PostalCode FullPath="Company/Address/USAddress/PostalCode" />
<Country FullPath="Company/Address/ForeignAddress/Country" />
</ForeignAddress>
</Address>
</Company>
The error returned when clicking the tree node is:
Server Error in '/' Application.
----------------------------------------------------------------------
----------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the
current web request. Please review the stack trace for more
information
about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader,
Boolean
preserveWhitespace) +24
System.Xml.XmlDocument.Load(XmlReader reader) +96
System.Web.UI.WebControls.XmlDataSource.PopulateXmlDocument(XmlDocumen
t
document, CacheDependency& dataCacheDependency, CacheDependency&
transformCacheDependency) +305
System.Web.UI.WebControls.XmlDataSource.GetXmlDocument() +154
System.Web.UI.WebControls.XmlHierarchicalDataSourceView.Select()
+14
System.Web.UI.WebControls.TreeView.DataBindNode(TreeNode node) +126
System.Web.UI.WebControls.TreeView.PopulateNode(TreeNode node) +27
System.Web.UI.WebControls.TreeView.LoadPostData(String postDataKey,
NameValueCollection postCollection) +1082
System.Web.UI.WebControls.TreeView.System.Web.UI.IPostBackDataHandler.
LoadPostData(String
postDataKey, NameValueCollection postCollection) +11
System.Web.UI.Page.ProcessPostData(NameValueCollection postData,
Boolean
fBeforeLoad) +661
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+1194
Thanks for any help - Rich F.
.
- Follow-Ups:
- Re: TreeView Dynamic XML Binding
- From: Rich Fowler
- Re: TreeView Dynamic XML Binding
- References:
- TreeView Dynamic XML Binding
- From: Rich Fowler
- TreeView Dynamic XML Binding
- Prev by Date: TreeView Dynamic XML Binding
- Next by Date: Having a checkbox make something available when it's checked.
- Previous by thread: TreeView Dynamic XML Binding
- Next by thread: Re: TreeView Dynamic XML Binding
- Index(es):
Relevant Pages
|