TreeView Dynamic XML Binding


R

Rich Fowler

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(XmlDocument
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.
 
Ad

Advertisements

R

Rich Fowler

David,

Thanks - quick and great response.

It actually addressed one issue I was interested in and that is turnning off
the postback at all nodes and expand/collapse images. This opens up my
research into the use of the other events. Implementing as you suggest
removed the error. However, it is not exactly the result I was looking for.
I want the Expand/Collapse action to happen on the client but the Select
action to be a postback. This works fine when I explicitly set the DataFile
on the aspx page, as in your code below. However, when I dynamically set the
DataFile in the Page_Load, the postback generates the null reference error
when clicking the tree node. Any suggesting into what is causing this?

Your response brings up a side question. When I implemented it and debug on
the TreeNodeDataBound event, it fires when expanding. Is this using AJAX?
I'm moving directly from .NET 1.1 to 3.5, so much of this is new to me.

Thanks again.

David R. Longnecker said:
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.
 
S

Steven Cheng [MSFT]

Hi Rich,

As for the TreeView page, would you provide some further information, such
as the TreeView aspx template and attributes you set. I have created a
simple test page (paste page source below) and didn't encounter the same
behavior. I'm wondering whether there is anything else cause the problem.
Here is my test page

#use the sample xml file you provided in first message
====aspx template====
</asp:XmlDataSource>
<asp:TreeView ID="tv" runat="server"
DataSourceID="XmlDataSource1"
AutoGenerateDataBindings="False" ExpandDepth="1"
onselectednodechanged="Unnamed1_SelectedNodeChanged">
<DataBindings>
<asp:TreeNodeBinding ValueField="FullPath"
TextField="FullPath"
DataMember="Company" Depth="0" />
<asp:TreeNodeBinding DataMember="Name" Depth="1"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="Address" Depth="1"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="USAddress" Depth="2"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="ForeignAddress" Depth="2"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="Street" Depth="3"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="City" Depth="3"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="ZipCode" Depth="3"
TextField="FullPath"
ValueField="FullPath" />
</DataBindings>
</asp:TreeView>


</div>

=======code behind================

public partial class TreeView_TreeViewPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDataSource1.DataFile = "~/TreeView/data.xml";
}
protected void Unnamed1_SelectedNodeChanged(object sender, EventArgs e)
{
Response.Write("<br/>Unnamed1_SelectedNodeChanged: " +
tv.SelectedNode.Text);
}
}
=======================
Please let me know if there is anything I've missed.

BTW, by monitoring the browser http traffic, I get that the node
expanding(without postback) is not using ajax. I think the page should have
already loaded the nodes in response html and just use script to display
them when you expanding nodes. Also, if you think the Page_load event is
the problem, you can try put the Xml file path setting code into the
"Page_Init" or "Page_PreInit" event to see whether the behavior changes.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
From: "Rich Fowler" <[email protected]>
References: <[email protected]>
In-Reply-To: <[email protected]>
Subject: Re: TreeView Dynamic XML Binding
Date: Tue, 25 Mar 2008 17:01:19 -0600
David,

Thanks - quick and great response.

It actually addressed one issue I was interested in and that is turnning off
the postback at all nodes and expand/collapse images. This opens up my
research into the use of the other events. Implementing as you suggest
removed the error. However, it is not exactly the result I was looking for.
I want the Expand/Collapse action to happen on the client but the Select
action to be a postback. This works fine when I explicitly set the DataFile
on the aspx page, as in your code below. However, when I dynamically set the
DataFile in the Page_Load, the postback generates the null reference error
when clicking the tree node. Any suggesting into what is causing this?

Your response brings up a side question. When I implemented it and debug on
the TreeNodeDataBound event, it fires when expanding. Is this using AJAX?
I'm moving directly from .NET 1.1 to 3.5, so much of this is new to me.

Thanks again.

David R. Longnecker said:
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.
 
R

Rich Fowler

Steven,

Sure, it is very simple. Most everything are the defaults. I do not provide
any databinding because I have no control of the element names and do not
know them in advance. I'm displaying the element name. At this point, I do
not have a onselectednodechanged event, but when I do have one, it does not
make any difference. The error occurs prior to reaching the Page_Load event.

I've tried adding the TreeNodeDataBound event and setting the e.Node.Value
but that makes no difference.

aspx code:
<form id="form1" runat="server">
<div>
<asp:TreeView ID="cSchemaTree" runat="server"
DataSourceID="cSchemaXML" ExpandDepth="1">
</asp:TreeView>
<asp:XmlDataSource ID="cSchemaXML"
runat="server"></asp:XmlDataSource>
</div>
</form>

aspx.cs code:
public partial class SchemaTree : System.Web.UI.Page
{
protected void Page_Load()
{
cSchemaXML.DataFile = @"D:\DotNet\TestData\TestXml.xml";
}
}

TestXml.xml file - extract from much larger file:

<Return Name="Return">
<returnVersion Name="Return/returnVersion" />
<ReturnHeader Name="Return/ReturnHeader">
<binaryAttachmentCount Name="Return/ReturnHeader/binaryAttachmentCount"
/>
<Timestamp Name="Return/ReturnHeader/Timestamp" />
<TaxPeriodEndDate Name="Return/ReturnHeader/TaxPeriodEndDate" />
<DisasterRelief Name="Return/ReturnHeader/DisasterRelief" />
<ISPNumber Name="Return/ReturnHeader/ISPNumber" />
<PreparerFirm Name="Return/ReturnHeader/PreparerFirm">
<EIN Name="Return/ReturnHeader/PreparerFirm/EIN" />
<PreparerFirmBusinessName
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmBusinessName">
<BusinessNameLine1
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmBusinessName/BusinessNameLine1"
/>
<BusinessNameLine2
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmBusinessName/BusinessNameLine2"
/>
</PreparerFirmBusinessName>
<PreparerFirmUSAddress
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress">
<AddressLine1
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/AddressLine1"
/>
<AddressLine2
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/AddressLine2"
/>
<City
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/City" />
<State
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/State" />
<ZIPCode
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/ZIPCode" />
</PreparerFirmUSAddress>
<PreparerFirmForeignAddress
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress">
<AddressLine1
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/AddressLine1"
/>
<AddressLine2
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/AddressLine2"
/>
<City
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/City" />
<ProvinceOrState
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/ProvinceOrState"
/>
<Country
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/Country"
/>
<PostalCode
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/PostalCode"
/>
</PreparerFirmForeignAddress>
</PreparerFirm>
</ReturnHeader>
</Return>

Notes: The test xml file is not in the website folder as in your code.
Everything works if I set the DataFile in the XmlDataSource control. Does
not work when I dynamically set the DataFile in the Page_Load.

To reproduce the error:
Open web page.
Expand the ReturnHeader node.
Click the Timestamp node.
The null reference error is displayed.

Notes: To get the error, you need to expand a node and click on one of the
elements in the expanded node.

Thanks - Rich F.


Steven Cheng said:
Hi Rich,

As for the TreeView page, would you provide some further information, such
as the TreeView aspx template and attributes you set. I have created a
simple test page (paste page source below) and didn't encounter the same
behavior. I'm wondering whether there is anything else cause the problem.
Here is my test page

#use the sample xml file you provided in first message
====aspx template====
</asp:XmlDataSource>
<asp:TreeView ID="tv" runat="server"
DataSourceID="XmlDataSource1"
AutoGenerateDataBindings="False" ExpandDepth="1"
onselectednodechanged="Unnamed1_SelectedNodeChanged">
<DataBindings>
<asp:TreeNodeBinding ValueField="FullPath"
TextField="FullPath"
DataMember="Company" Depth="0" />
<asp:TreeNodeBinding DataMember="Name" Depth="1"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="Address" Depth="1"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="USAddress" Depth="2"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="ForeignAddress" Depth="2"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="Street" Depth="3"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="City" Depth="3"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="ZipCode" Depth="3"
TextField="FullPath"
ValueField="FullPath" />
</DataBindings>
</asp:TreeView>


</div>

=======code behind================

public partial class TreeView_TreeViewPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDataSource1.DataFile = "~/TreeView/data.xml";
}
protected void Unnamed1_SelectedNodeChanged(object sender, EventArgs e)
{
Response.Write("<br/>Unnamed1_SelectedNodeChanged: " +
tv.SelectedNode.Text);
}
}
=======================
Please let me know if there is anything I've missed.

BTW, by monitoring the browser http traffic, I get that the node
expanding(without postback) is not using ajax. I think the page should
have
already loaded the nodes in response html and just use script to display
them when you expanding nodes. Also, if you think the Page_load event is
the problem, you can try put the Xml file path setting code into the
"Page_Init" or "Page_PreInit" event to see whether the behavior changes.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.


--------------------
From: "Rich Fowler" <[email protected]>
References: <[email protected]>
In-Reply-To: <[email protected]>
Subject: Re: TreeView Dynamic XML Binding
Date: Tue, 25 Mar 2008 17:01:19 -0600
David,

Thanks - quick and great response.

It actually addressed one issue I was interested in and that is turnning off
the postback at all nodes and expand/collapse images. This opens up my
research into the use of the other events. Implementing as you suggest
removed the error. However, it is not exactly the result I was looking for.
I want the Expand/Collapse action to happen on the client but the Select
action to be a postback. This works fine when I explicitly set the DataFile
on the aspx page, as in your code below. However, when I dynamically set the
DataFile in the Page_Load, the postback generates the null reference error
when clicking the tree node. Any suggesting into what is causing this?

Your response brings up a side question. When I implemented it and debug on
the TreeNodeDataBound event, it fires when expanding. Is this using AJAX?
I'm moving directly from .NET 1.1 to 3.5, so much of this is new to me.

Thanks again.

David R. Longnecker said:
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.
 
R

Rich Fowler

David,

The functionality that exists when I explicitly set the DataFile property in
the XmlDataSource control is exactly the functionality I want when I set the
value dynamically in the Page_Load event. Everything works fine when it is
set in the control, however the error occurs when it is set dynamically. See
my reply the Steven Cheng for more detailed code. The following code works
the way I want it to (I'll add the SelectedNodeChanged event once I get the
rest stable. Having the event defined does not change the issue I'm
having.).

<asp:TreeView ID="cSchemaTree" runat="server"
DataSourceID="cSchemaXML" ExpandDepth="1">
</asp:TreeView>
<asp:XmlDataSource ID="cSchemaXML" runat="server"
DataFile="D:\DotNet\TestData\TestXml.xml"></asp:XmlDataSource>

I'll try changing the code to set the datasource as you show and see if it
makes any difference. According to the documentation, I should just be able
to set the XmlDataSource.DataFile value and the binding will proceed because
of the relationship between the IDs.

Thanks - Rich F.


David R. Longnecker said:
Rich-

How are you implementing loading loading the data file on Page_Load?
Here's an example of loading the data file dynamically in code behind.

aspx :

<asp:TreeView ID="CompanyTreeXml" runat="server"
ExpandDepth="1" EnableClientScript="true"
PopulateNodesFromClient="true"
ontreenodedatabound="CompanyTreeXml_TreeNodeDataBound">
<NodeStyle ForeColor="Black" />
</asp:TreeView>

(Notice no DataSourceId attribute or XmlDataSource control).

code-behind:

protected void Page_Load(object sender, EventArgs e)
{
CompanyTreeXml.DataSource = new XmlDataSource { DataFile =
"~/Company.xml" };
CompanyTreeXml.DataBind();
}

Programatically loading the DataFile (and generating the XmlDataSource
since XmlDataSource implements IHeirarchicalDataSource, which the TreeView
will need) is taken care of in those two lines.

Now, with that, I need a bit more to understand what you're trying to do.
If you click on the tree node, say USAddress for example, and click that
word, the tree expands out--but that's because we're telling it to .Expand
rather than .Select.action to be a postback.

How would you know when the user wants to "select" and when they "expand"?


Side question:
It's not quite AJAX, but it is JavaScript that toggles the tables around.
If you view the source of a generated page, you can see the WebResource
scripts and then the javascript method calls on each of the tree nodes.
For example, here's the HTML code generated by the root node, Company:

<td>

<a id="CompanyTreeXmln0"
href="javascript:TreeView_ToggleNode(CompanyTreeXml_Data,0,document.getElementById('CompanyTreeXmln0'),'
',document.getElementById('CompanyTreeXmln0Nodes'))">

<img
src="/WebResource.axd?d=UK9sboTp8hUXuZ9MjiTSmX-mKNPjvYpQyEaZoUuGRa01&amp;t=633408388558438142"
alt="Collapse Company" style="border-width:0;" /></a>

</td>

<td class="CompanyTreeXml_2" style="white-space:nowrap;">

<a class="CompanyTreeXml_0 CompanyTreeXml_1"
href="javascript:TreeView_ToggleNode(CompanyTreeXml_Data,0,document.getElementById('CompanyTreeXmln0'),'
',document.getElementById('CompanyTreeXmln0Nodes'))"
id="CompanyTreeXmlt0">Company</a>

</td>

If that HTML is a bit heavy (for a single node on your TreeView), I'd
recommend checking out the CSS Friendly Adapters
(http://www.codeplex.com/cssfriendly). It started out as a MS project,
it's now on CodePlex. That same code with the adapters looks like:

<span class="AspNet-TreeView-ClickableNonLink"
onclick="ExpandCollapse__AspNetTreeView(this.parentNode.getElementsByTagName('span')[0])"
onmouseover="Hover__AspNetTreeView(this)"
onmouseout="UnHover__AspNetTreeView(this)">
Company</span>


HTH.

-dl


--
David R. Longnecker
http://blog.tiredstudent.com
David,

Thanks - quick and great response.

It actually addressed one issue I was interested in and that is
turnning off the postback at all nodes and expand/collapse images.
This opens up my research into the use of the other events.
Implementing as you suggest removed the error. However, it is not
exactly the result I was looking for. I want the Expand/Collapse
action to happen on the client but the Select action to be a postback.
This works fine when I explicitly set the DataFile on the aspx page,
as in your code below. However, when I dynamically set the DataFile in
the Page_Load, the postback generates the null reference error when
clicking the tree node. Any suggesting into what is causing this?

Your response brings up a side question. When I implemented it and
debug on the TreeNodeDataBound event, it fires when expanding. Is this
using AJAX? I'm moving directly from .NET 1.1 to 3.5, so much of this
is new to me.

Thanks again.

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(XmlDocum
en
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.IPostBackDataHandle
r.
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.
 
R

Rich Fowler

David,

I implemented your changes and the error goes away. However, I've lost the
speed of the default functionality when loading it from the XmlDataSource.
With your changes, the entire xml structure is included in the web page
(which for the actual xml makes it very slow). This does not happen with the
'default' setting. I'll work on it some more. It seems as if the default
settings are doing the populate on expand processing even though the
property for PopulateNodesFromClient is true. I think it is a combination of
not having all of the data downloaded and clicking a node that was expanded
on the client that may cause the object is null error (just a guess). BTW, I
needed to put the setting of the datasource in a !this.IsPostBack
conditionally, otherwise the entire tree reset.

Thanks for your help.

Rich F.

David R. Longnecker said:
Rich-

How are you implementing loading loading the data file on Page_Load?
Here's an example of loading the data file dynamically in code behind.

aspx :

<asp:TreeView ID="CompanyTreeXml" runat="server"
ExpandDepth="1" EnableClientScript="true"
PopulateNodesFromClient="true"
ontreenodedatabound="CompanyTreeXml_TreeNodeDataBound">
<NodeStyle ForeColor="Black" />
</asp:TreeView>

(Notice no DataSourceId attribute or XmlDataSource control).

code-behind:

protected void Page_Load(object sender, EventArgs e)
{
CompanyTreeXml.DataSource = new XmlDataSource { DataFile =
"~/Company.xml" };
CompanyTreeXml.DataBind();
}

Programatically loading the DataFile (and generating the XmlDataSource
since XmlDataSource implements IHeirarchicalDataSource, which the TreeView
will need) is taken care of in those two lines.

Now, with that, I need a bit more to understand what you're trying to do.
If you click on the tree node, say USAddress for example, and click that
word, the tree expands out--but that's because we're telling it to .Expand
rather than .Select.action to be a postback.

How would you know when the user wants to "select" and when they "expand"?


Side question:
It's not quite AJAX, but it is JavaScript that toggles the tables around.
If you view the source of a generated page, you can see the WebResource
scripts and then the javascript method calls on each of the tree nodes.
For example, here's the HTML code generated by the root node, Company:

<td>

<a id="CompanyTreeXmln0"
href="javascript:TreeView_ToggleNode(CompanyTreeXml_Data,0,document.getElementById('CompanyTreeXmln0'),'
',document.getElementById('CompanyTreeXmln0Nodes'))">

<img
src="/WebResource.axd?d=UK9sboTp8hUXuZ9MjiTSmX-mKNPjvYpQyEaZoUuGRa01&amp;t=633408388558438142"
alt="Collapse Company" style="border-width:0;" /></a>

</td>

<td class="CompanyTreeXml_2" style="white-space:nowrap;">

<a class="CompanyTreeXml_0 CompanyTreeXml_1"
href="javascript:TreeView_ToggleNode(CompanyTreeXml_Data,0,document.getElementById('CompanyTreeXmln0'),'
',document.getElementById('CompanyTreeXmln0Nodes'))"
id="CompanyTreeXmlt0">Company</a>

</td>

If that HTML is a bit heavy (for a single node on your TreeView), I'd
recommend checking out the CSS Friendly Adapters
(http://www.codeplex.com/cssfriendly). It started out as a MS project,
it's now on CodePlex. That same code with the adapters looks like:

<span class="AspNet-TreeView-ClickableNonLink"
onclick="ExpandCollapse__AspNetTreeView(this.parentNode.getElementsByTagName('span')[0])"
onmouseover="Hover__AspNetTreeView(this)"
onmouseout="UnHover__AspNetTreeView(this)">
Company</span>


HTH.

-dl


--
David R. Longnecker
http://blog.tiredstudent.com
David,

Thanks - quick and great response.

It actually addressed one issue I was interested in and that is
turnning off the postback at all nodes and expand/collapse images.
This opens up my research into the use of the other events.
Implementing as you suggest removed the error. However, it is not
exactly the result I was looking for. I want the Expand/Collapse
action to happen on the client but the Select action to be a postback.
This works fine when I explicitly set the DataFile on the aspx page,
as in your code below. However, when I dynamically set the DataFile in
the Page_Load, the postback generates the null reference error when
clicking the tree node. Any suggesting into what is causing this?

Your response brings up a side question. When I implemented it and
debug on the TreeNodeDataBound event, it fires when expanding. Is this
using AJAX? I'm moving directly from .NET 1.1 to 3.5, so much of this
is new to me.

Thanks again.

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(XmlDocum
en
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.IPostBackDataHandle
r.
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.
 
Ad

Advertisements

R

Rich Fowler

David,

That is the direction I'm going. I think I found the issue (problem). By
monitoring the treeview child nodes when I explicitly set the
XmlDataSource.DataFile, I determined that the process is actually using the
PopulateOnDemand processing (even though I have no code for it). This is
also true when I dynamically set XmlDataSource.DataFile. During the Select
postback, the framework tries to populate the XmlDataSource control (prior
to the my code getting control). It does not know the xml file path at that
point and errors with a null reference error. I received the same error in
my TreeNodePopulate event code when I tried to access the
XmlDataSource.GetXmlDocument method. This method is called prior to the
Page_Load event during the Select postback, therefore the DataFile property
is not set. For now, I'm going to put the xml document into a session
variable (I don't have a performance/memory concern at this time). I've not
researched the XmlDataSource caching options and what they mean. I do not
know if the caching looks at the file name and does not reload or what, and
what this implies if the source xml file changes.

I'll try not using the XmlDataSource control and set the DataSource in the
TreeView control but I think this will have the same issue when doing
PopulateOnDemand processing and repopulating the control.

Thanks for all your time - Rich F.

David R. Longnecker said:
In the TreeNodeDataBound method, does adding the following speed it up at
all?

e.Node.PopulateOnDemand = true;

-dl

--
David R. Longnecker
http://blog.tiredstudent.com
David,

I implemented your changes and the error goes away. However, I've lost
the speed of the default functionality when loading it from the
XmlDataSource. With your changes, the entire xml structure is included
in the web page (which for the actual xml makes it very slow). This
does not happen with the 'default' setting. I'll work on it some more.
It seems as if the default settings are doing the populate on expand
processing even though the property for PopulateNodesFromClient is
true. I think it is a combination of not having all of the data
downloaded and clicking a node that was expanded on the client that
may cause the object is null error (just a guess). BTW, I needed to
put the setting of the datasource in a !this.IsPostBack conditionally,
otherwise the entire tree reset.

Thanks for your help.

Rich F.

Rich-

How are you implementing loading loading the data file on Page_Load?
Here's an example of loading the data file dynamically in code
behind.

aspx :

<asp:TreeView ID="CompanyTreeXml" runat="server"
ExpandDepth="1" EnableClientScript="true"
PopulateNodesFromClient="true"
ontreenodedatabound="CompanyTreeXml_TreeNodeDataBound">
<NodeStyle ForeColor="Black" />
</asp:TreeView>
(Notice no DataSourceId attribute or XmlDataSource control).

code-behind:

protected void Page_Load(object sender, EventArgs e)
{
CompanyTreeXml.DataSource = new XmlDataSource { DataFile =
"~/Company.xml" };
CompanyTreeXml.DataBind();
}
Programatically loading the DataFile (and generating the
XmlDataSource since XmlDataSource implements IHeirarchicalDataSource,
which the TreeView will need) is taken care of in those two lines.

Now, with that, I need a bit more to understand what you're trying to
do. If you click on the tree node, say USAddress for example, and
click that word, the tree expands out--but that's because we're
telling it to .Expand rather than .Select.

I want the Expand/Collapse action to happen on the client but the
Select

action to be a postback.

How would you know when the user wants to "select" and when they
"expand"?

Side question:
It's not quite AJAX, but it is JavaScript that toggles the tables
around.
If you view the source of a generated page, you can see the
WebResource
scripts and then the javascript method calls on each of the tree
nodes.
For example, here's the HTML code generated by the root node,
Company:
<td>

<a id="CompanyTreeXmln0"
href="javascript:TreeView_ToggleNode(CompanyTreeXml_Data,0,document.g
etElementById('CompanyTreeXmln0'),'
',document.getElementById('CompanyTreeXmln0Nodes'))">

<img
src="/WebResource.axd?d=UK9sboTp8hUXuZ9MjiTSmX-mKNPjvYpQyEaZoUuGRa01&
amp;t=633408388558438142" alt="Collapse Company"
style="border-width:0;" /></a>

</td>

<td class="CompanyTreeXml_2" style="white-space:nowrap;">

<a class="CompanyTreeXml_0 CompanyTreeXml_1"
href="javascript:TreeView_ToggleNode(CompanyTreeXml_Data,0,document.g
etElementById('CompanyTreeXmln0'),'
',document.getElementById('CompanyTreeXmln0Nodes'))"
id="CompanyTreeXmlt0">Company</a>

</td>

If that HTML is a bit heavy (for a single node on your TreeView), I'd
recommend checking out the CSS Friendly Adapters
(http://www.codeplex.com/cssfriendly). It started out as a MS
project, it's now on CodePlex. That same code with the adapters
looks like:

<span class="AspNet-TreeView-ClickableNonLink"
onclick="ExpandCollapse__AspNetTreeView(this.parentNode.getElementsBy
TagName('span')[0])"
onmouseover="Hover__AspNetTreeView(this)"
onmouseout="UnHover__AspNetTreeView(this)">
Company</span>
HTH.

-dl

--
David R. Longnecker
http://blog.tiredstudent.com
David,

Thanks - quick and great response.

It actually addressed one issue I was interested in and that is
turnning off the postback at all nodes and expand/collapse images.
This opens up my research into the use of the other events.
Implementing as you suggest removed the error. However, it is not
exactly the result I was looking for. I want the Expand/Collapse
action to happen on the client but the Select action to be a
postback. This works fine when I explicitly set the DataFile on the
aspx page, as in your code below. However, when I dynamically set
the DataFile in the Page_Load, the postback generates the null
reference error when clicking the tree node. Any suggesting into
what is causing this?

Your response brings up a side question. When I implemented it and
debug on the TreeNodeDataBound event, it fires when expanding. Is
this using AJAX? I'm moving directly from .NET 1.1 to 3.5, so much
of this is new to me.

Thanks again.

message
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(XmlDoc
um
en
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.IPostBackDataHand
le
r.
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.
 
S

Steven Cheng [MSFT]

Thanks for your reply Rich,

Not sure whether you have got further progress. Yes, I have repro the same
error through the new XML data file you provided. Also, I've tested using
the "Page_Init" event to dynamically add the data file for the
XmlDataSource. It seems that when I put the file assigning code into
Page_Init event hander, the exception disappeared. e.g.

protected void Page_Init(object sender, EventArgs e)
{
XmlDataSource1.DataFile = "~/TreeView/data.xml";
}

I think the problem may due to the initializing steps and sequence at the
server-side for the XmlDataSource and the TreeView control. You can also
try this on your side to see whether it works. If so, I think this should
be a much simpler solution.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: "Rich Fowler" <[email protected]>
References: <[email protected]>
<[email protected]>
In-Reply-To: <[email protected]>
Subject: Re: TreeView Dynamic XML Binding
Date: Wed, 26 Mar 2008 08:53:42 -0600
Steven,

Sure, it is very simple. Most everything are the defaults. I do not provide
any databinding because I have no control of the element names and do not
know them in advance. I'm displaying the element name. At this point, I do
not have a onselectednodechanged event, but when I do have one, it does not
make any difference. The error occurs prior to reaching the Page_Load event.

I've tried adding the TreeNodeDataBound event and setting the e.Node.Value
but that makes no difference.

aspx code:
<form id="form1" runat="server">
<div>
<asp:TreeView ID="cSchemaTree" runat="server"
DataSourceID="cSchemaXML" ExpandDepth="1">
</asp:TreeView>
<asp:XmlDataSource ID="cSchemaXML"
runat="server"></asp:XmlDataSource>
</div>
</form>

aspx.cs code:
public partial class SchemaTree : System.Web.UI.Page
{
protected void Page_Load()
{
cSchemaXML.DataFile = @"D:\DotNet\TestData\TestXml.xml";
}
}

TestXml.xml file - extract from much larger file:

<Return Name="Return">
<returnVersion Name="Return/returnVersion" />
<ReturnHeader Name="Return/ReturnHeader">
<binaryAttachmentCount Name="Return/ReturnHeader/binaryAttachmentCount"
/>
<Timestamp Name="Return/ReturnHeader/Timestamp" />
<TaxPeriodEndDate Name="Return/ReturnHeader/TaxPeriodEndDate" />
<DisasterRelief Name="Return/ReturnHeader/DisasterRelief" />
<ISPNumber Name="Return/ReturnHeader/ISPNumber" />
<PreparerFirm Name="Return/ReturnHeader/PreparerFirm">
<EIN Name="Return/ReturnHeader/PreparerFirm/EIN" />
<PreparerFirmBusinessName
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmBusinessName">
<BusinessNameLine1
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmBusinessName/BusinessNam eLine1"
/>
<BusinessNameLine2
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmBusinessName/BusinessNam
eLine2"
/>
</PreparerFirmBusinessName>
<PreparerFirmUSAddress
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress">
<AddressLine1
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/AddressLine1"
/>
<AddressLine2
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/AddressLine2"
/>
<City
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/City" />
<State
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/State" />
<ZIPCode
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/ZIPCode" />
</PreparerFirmUSAddress>
<PreparerFirmForeignAddress
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress">
<AddressLine1
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/AddressLi ne1"
/>
<AddressLine2
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/AddressLi
ne2"
/>
<City
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/City" />
<ProvinceOrState
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/ProvinceO rState"
/>
<Country
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/Country"
/>
<PostalCode
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/PostalCod
e"
/>
</PreparerFirmForeignAddress>
</PreparerFirm>
</ReturnHeader>
</Return>

Notes: The test xml file is not in the website folder as in your code.
Everything works if I set the DataFile in the XmlDataSource control. Does
not work when I dynamically set the DataFile in the Page_Load.

To reproduce the error:
Open web page.
Expand the ReturnHeader node.
Click the Timestamp node.
The null reference error is displayed.

Notes: To get the error, you need to expand a node and click on one of the
elements in the expanded node.

Thanks - Rich F.


Steven Cheng said:
Hi Rich,

As for the TreeView page, would you provide some further information, such
as the TreeView aspx template and attributes you set. I have created a
simple test page (paste page source below) and didn't encounter the same
behavior. I'm wondering whether there is anything else cause the problem.
Here is my test page

#use the sample xml file you provided in first message
====aspx template====
</asp:XmlDataSource>
<asp:TreeView ID="tv" runat="server"
DataSourceID="XmlDataSource1"
AutoGenerateDataBindings="False" ExpandDepth="1"
onselectednodechanged="Unnamed1_SelectedNodeChanged">
<DataBindings>
<asp:TreeNodeBinding ValueField="FullPath"
TextField="FullPath"
DataMember="Company" Depth="0" />
<asp:TreeNodeBinding DataMember="Name" Depth="1"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="Address" Depth="1"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="USAddress" Depth="2"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="ForeignAddress" Depth="2"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="Street" Depth="3"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="City" Depth="3"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="ZipCode" Depth="3"
TextField="FullPath"
ValueField="FullPath" />
</DataBindings>
</asp:TreeView>


</div>

=======code behind================

public partial class TreeView_TreeViewPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDataSource1.DataFile = "~/TreeView/data.xml";
}
protected void Unnamed1_SelectedNodeChanged(object sender, EventArgs e)
{
Response.Write("<br/>Unnamed1_SelectedNodeChanged: " +
tv.SelectedNode.Text);
}
}
=======================
Please let me know if there is anything I've missed.

BTW, by monitoring the browser http traffic, I get that the node
expanding(without postback) is not using ajax. I think the page should
have
already loaded the nodes in response html and just use script to display
them when you expanding nodes. Also, if you think the Page_load event is
the problem, you can try put the Xml file path setting code into the
"Page_Init" or "Page_PreInit" event to see whether the behavior changes.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.


--------------------
From: "Rich Fowler" <[email protected]>
References: <[email protected]>
In-Reply-To: <[email protected]>
Subject: Re: TreeView Dynamic XML Binding
Date: Tue, 25 Mar 2008 17:01:19 -0600
David,

Thanks - quick and great response.

It actually addressed one issue I was interested in and that is turnning off
the postback at all nodes and expand/collapse images. This opens up my
research into the use of the other events. Implementing as you suggest
removed the error. However, it is not exactly the result I was looking for.
I want the Expand/Collapse action to happen on the client but the Select
action to be a postback. This works fine when I explicitly set the DataFile
on the aspx page, as in your code below. However, when I dynamically set the
DataFile in the Page_Load, the postback generates the null reference error
when clicking the tree node. Any suggesting into what is causing this?

Your response brings up a side question. When I implemented it and debug on
the TreeNodeDataBound event, it fires when expanding. Is this using AJAX?
I'm moving directly from .NET 1.1 to 3.5, so much of this is new to me.

Thanks again.

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.
 
R

Rich Fowler

I thought I had tried Page_Init event but when I modified the code it
worked, so obviously it was a different event I tried.

A followup question. Since I am setting the DataFile each time, will this
invalidate the cache on the postback, causing another read of the file. Or
is the cache based on the value in the DataFile property at the time of data
access.

Thanks much, now I can remove all my PopulateOnDemand code.

Rich F.

Steven Cheng said:
Thanks for your reply Rich,

Not sure whether you have got further progress. Yes, I have repro the
same
error through the new XML data file you provided. Also, I've tested using
the "Page_Init" event to dynamically add the data file for the
XmlDataSource. It seems that when I put the file assigning code into
Page_Init event hander, the exception disappeared. e.g.

protected void Page_Init(object sender, EventArgs e)
{
XmlDataSource1.DataFile = "~/TreeView/data.xml";
}

I think the problem may due to the initializing steps and sequence at the
server-side for the XmlDataSource and the TreeView control. You can also
try this on your side to see whether it works. If so, I think this should
be a much simpler solution.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
From: "Rich Fowler" <[email protected]>
References: <[email protected]>
<[email protected]>
In-Reply-To: <[email protected]>
Subject: Re: TreeView Dynamic XML Binding
Date: Wed, 26 Mar 2008 08:53:42 -0600
Steven,

Sure, it is very simple. Most everything are the defaults. I do not provide
any databinding because I have no control of the element names and do not
know them in advance. I'm displaying the element name. At this point, I do
not have a onselectednodechanged event, but when I do have one, it does not
make any difference. The error occurs prior to reaching the Page_Load event.

I've tried adding the TreeNodeDataBound event and setting the e.Node.Value
but that makes no difference.

aspx code:
<form id="form1" runat="server">
<div>
<asp:TreeView ID="cSchemaTree" runat="server"
DataSourceID="cSchemaXML" ExpandDepth="1">
</asp:TreeView>
<asp:XmlDataSource ID="cSchemaXML"
runat="server"></asp:XmlDataSource>
</div>
</form>

aspx.cs code:
public partial class SchemaTree : System.Web.UI.Page
{
protected void Page_Load()
{
cSchemaXML.DataFile = @"D:\DotNet\TestData\TestXml.xml";
}
}

TestXml.xml file - extract from much larger file:

<Return Name="Return">
<returnVersion Name="Return/returnVersion" />
<ReturnHeader Name="Return/ReturnHeader">
<binaryAttachmentCount Name="Return/ReturnHeader/binaryAttachmentCount"
/>
<Timestamp Name="Return/ReturnHeader/Timestamp" />
<TaxPeriodEndDate Name="Return/ReturnHeader/TaxPeriodEndDate" />
<DisasterRelief Name="Return/ReturnHeader/DisasterRelief" />
<ISPNumber Name="Return/ReturnHeader/ISPNumber" />
<PreparerFirm Name="Return/ReturnHeader/PreparerFirm">
<EIN Name="Return/ReturnHeader/PreparerFirm/EIN" />
<PreparerFirmBusinessName
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmBusinessName">
<BusinessNameLine1
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmBusinessName/BusinessNam eLine1"
/>
<BusinessNameLine2
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmBusinessName/BusinessNam
eLine2"
/>
</PreparerFirmBusinessName>
<PreparerFirmUSAddress
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress">
<AddressLine1
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/AddressLine1"
/>
<AddressLine2
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/AddressLine2"
/>
<City
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/City" />
<State
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/State" />
<ZIPCode
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/ZIPCode" />
</PreparerFirmUSAddress>
<PreparerFirmForeignAddress
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress">
<AddressLine1
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/AddressLi ne1"
/>
<AddressLine2
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/AddressLi
ne2"
/>
<City
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/City" />
<ProvinceOrState
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/ProvinceO rState"
/>
<Country
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/Country"
/>
<PostalCode
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/PostalCod
e"
/>
</PreparerFirmForeignAddress>
</PreparerFirm>
</ReturnHeader>
</Return>

Notes: The test xml file is not in the website folder as in your code.
Everything works if I set the DataFile in the XmlDataSource control. Does
not work when I dynamically set the DataFile in the Page_Load.

To reproduce the error:
Open web page.
Expand the ReturnHeader node.
Click the Timestamp node.
The null reference error is displayed.

Notes: To get the error, you need to expand a node and click on one of the
elements in the expanded node.

Thanks - Rich F.


Steven Cheng said:
Hi Rich,

As for the TreeView page, would you provide some further information, such
as the TreeView aspx template and attributes you set. I have created a
simple test page (paste page source below) and didn't encounter the same
behavior. I'm wondering whether there is anything else cause the
problem.
Here is my test page

#use the sample xml file you provided in first message
====aspx template====
<div>
<asp:XmlDataSource ID="XmlDataSource1" runat="server"
</asp:XmlDataSource>
<asp:TreeView ID="tv" runat="server"
DataSourceID="XmlDataSource1"
AutoGenerateDataBindings="False" ExpandDepth="1"
onselectednodechanged="Unnamed1_SelectedNodeChanged">
<DataBindings>
<asp:TreeNodeBinding ValueField="FullPath"
TextField="FullPath"
DataMember="Company" Depth="0" />
<asp:TreeNodeBinding DataMember="Name" Depth="1"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="Address" Depth="1"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="USAddress" Depth="2"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="ForeignAddress"
Depth="2"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="Street" Depth="3"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="City" Depth="3"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="ZipCode" Depth="3"
TextField="FullPath"
ValueField="FullPath" />
</DataBindings>
</asp:TreeView>


</div>

=======code behind================

public partial class TreeView_TreeViewPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDataSource1.DataFile = "~/TreeView/data.xml";
}
protected void Unnamed1_SelectedNodeChanged(object sender, EventArgs e)
{
Response.Write("<br/>Unnamed1_SelectedNodeChanged: " +
tv.SelectedNode.Text);
}
}
=======================
Please let me know if there is anything I've missed.

BTW, by monitoring the browser http traffic, I get that the node
expanding(without postback) is not using ajax. I think the page should
have
already loaded the nodes in response html and just use script to display
them when you expanding nodes. Also, if you think the Page_load event
is
the problem, you can try put the Xml file path setting code into the
"Page_Init" or "Page_PreInit" event to see whether the behavior changes.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments
and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach
the
most efficient resolution. The offering is not appropriate for
situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are
best
handled working with a dedicated Microsoft Support Engineer by
contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.


--------------------
From: "Rich Fowler" <[email protected]>
References: <[email protected]>
<[email protected]>
In-Reply-To: <[email protected]>
Subject: Re: TreeView Dynamic XML Binding
Date: Tue, 25 Mar 2008 17:01:19 -0600


David,

Thanks - quick and great response.

It actually addressed one issue I was interested in and that is turnning
off
the postback at all nodes and expand/collapse images. This opens up my
research into the use of the other events. Implementing as you suggest
removed the error. However, it is not exactly the result I was looking
for.
I want the Expand/Collapse action to happen on the client but the Select
action to be a postback. This works fine when I explicitly set the
DataFile
on the aspx page, as in your code below. However, when I dynamically set
the
DataFile in the Page_Load, the postback generates the null reference error
when clicking the tree node. Any suggesting into what is causing this?

Your response brings up a side question. When I implemented it and
debug
on
the TreeNodeDataBound event, it fires when expanding. Is this using
AJAX?
I'm moving directly from .NET 1.1 to 3.5, so much of this is new to me.

Thanks again.

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.
 
S

Steven Cheng [MSFT]

Thanks for your reply Rich,

I'm glad that it also works on your side. For the new question, I've just
had a quick look at the XmlDataSource through reflector. It seems the cache
dependecny generation or Cache accessing won't quite rely on whether you
change the DataFile path and you can keep programmtically assigning the
path.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
From: "Rich Fowler" <[email protected]>
Subject: Re: TreeView Dynamic XML Binding
Date: Thu, 27 Mar 2008 10:08:32 -0600
I thought I had tried Page_Init event but when I modified the code it
worked, so obviously it was a different event I tried.

A followup question. Since I am setting the DataFile each time, will this
invalidate the cache on the postback, causing another read of the file. Or
is the cache based on the value in the DataFile property at the time of data
access.

Thanks much, now I can remove all my PopulateOnDemand code.

Rich F.

Steven Cheng said:
Thanks for your reply Rich,

Not sure whether you have got further progress. Yes, I have repro the
same
error through the new XML data file you provided. Also, I've tested using
the "Page_Init" event to dynamically add the data file for the
XmlDataSource. It seems that when I put the file assigning code into
Page_Init event hander, the exception disappeared. e.g.

protected void Page_Init(object sender, EventArgs e)
{
XmlDataSource1.DataFile = "~/TreeView/data.xml";
}

I think the problem may due to the initializing steps and sequence at the
server-side for the XmlDataSource and the TreeView control. You can also
try this on your side to see whether it works. If so, I think this should
be a much simpler solution.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
From: "Rich Fowler" <[email protected]>
References: <[email protected]soft.com>
<[email protected]>
In-Reply-To: <[email protected]>
Subject: Re: TreeView Dynamic XML Binding
Date: Wed, 26 Mar 2008 08:53:42 -0600
Steven,

Sure, it is very simple. Most everything are the defaults. I do not provide
any databinding because I have no control of the element names and do not
know them in advance. I'm displaying the element name. At this point, I do
not have a onselectednodechanged event, but when I do have one, it does not
make any difference. The error occurs prior to reaching the Page_Load event.

I've tried adding the TreeNodeDataBound event and setting the e.Node.Value
but that makes no difference.

aspx code:
<form id="form1" runat="server">
<div>
<asp:TreeView ID="cSchemaTree" runat="server"
DataSourceID="cSchemaXML" ExpandDepth="1">
</asp:TreeView>
<asp:XmlDataSource ID="cSchemaXML"
runat="server"></asp:XmlDataSource>
</div>
</form>

aspx.cs code:
public partial class SchemaTree : System.Web.UI.Page
{
protected void Page_Load()
{
cSchemaXML.DataFile = @"D:\DotNet\TestData\TestXml.xml";
}
}

TestXml.xml file - extract from much larger file:

<Return Name="Return">
<returnVersion Name="Return/returnVersion" />
<ReturnHeader Name="Return/ReturnHeader">
<binaryAttachmentCount Name="Return/ReturnHeader/binaryAttachmentCount"
/>
<Timestamp Name="Return/ReturnHeader/Timestamp" />
<TaxPeriodEndDate Name="Return/ReturnHeader/TaxPeriodEndDate" />
<DisasterRelief Name="Return/ReturnHeader/DisasterRelief" />
<ISPNumber Name="Return/ReturnHeader/ISPNumber" />
<PreparerFirm Name="Return/ReturnHeader/PreparerFirm">
<EIN Name="Return/ReturnHeader/PreparerFirm/EIN" />
<PreparerFirmBusinessName
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmBusinessName">
<BusinessNameLine1
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmBusinessName/BusinessN
am
eLine1"
/>
<BusinessNameLine2
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmBusinessName/BusinessN
am
eLine2"
/>
</PreparerFirmBusinessName>
<PreparerFirmUSAddress
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress">
<AddressLine1
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/AddressLine1 "
/>
<AddressLine2
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/AddressLine2 "
/>
<City
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/City" />
<State
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/State" />
<ZIPCode
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmUSAddress/ZIPCode" />
</PreparerFirmUSAddress>
<PreparerFirmForeignAddress
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress">
<AddressLine1
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/Address
Li
ne1"
/>
<AddressLine2
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/Address
Li
ne2"
/>
<City
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/City" />
<ProvinceOrState
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/Provinc
eO
rState"
/>
<Country
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/Country "
/>
<PostalCode
Name="Return/ReturnHeader/PreparerFirm/PreparerFirmForeignAddress/PostalC
od
e"
/>
</PreparerFirmForeignAddress>
</PreparerFirm>
</ReturnHeader>
</Return>

Notes: The test xml file is not in the website folder as in your code.
Everything works if I set the DataFile in the XmlDataSource control. Does
not work when I dynamically set the DataFile in the Page_Load.

To reproduce the error:
Open web page.
Expand the ReturnHeader node.
Click the Timestamp node.
The null reference error is displayed.

Notes: To get the error, you need to expand a node and click on one of the
elements in the expanded node.

Thanks - Rich F.


Hi Rich,

As for the TreeView page, would you provide some further information, such
as the TreeView aspx template and attributes you set. I have created a
simple test page (paste page source below) and didn't encounter the same
behavior. I'm wondering whether there is anything else cause the
problem.
Here is my test page

#use the sample xml file you provided in first message
====aspx template====
<div>
<asp:XmlDataSource ID="XmlDataSource1" runat="server"
</asp:XmlDataSource>
<asp:TreeView ID="tv" runat="server"
DataSourceID="XmlDataSource1"
AutoGenerateDataBindings="False" ExpandDepth="1"
onselectednodechanged="Unnamed1_SelectedNodeChanged">
<DataBindings>
<asp:TreeNodeBinding ValueField="FullPath"
TextField="FullPath"
DataMember="Company" Depth="0" />
<asp:TreeNodeBinding DataMember="Name" Depth="1"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="Address" Depth="1"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="USAddress" Depth="2"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="ForeignAddress"
Depth="2"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="Street" Depth="3"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="City" Depth="3"
TextField="FullPath"
ValueField="FullPath" />
<asp:TreeNodeBinding DataMember="ZipCode" Depth="3"
TextField="FullPath"
ValueField="FullPath" />
</DataBindings>
</asp:TreeView>


</div>

=======code behind================

public partial class TreeView_TreeViewPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDataSource1.DataFile = "~/TreeView/data.xml";
}
protected void Unnamed1_SelectedNodeChanged(object sender, EventArgs e)
{
Response.Write("<br/>Unnamed1_SelectedNodeChanged: " +
tv.SelectedNode.Text);
}
}
=======================
Please let me know if there is anything I've missed.

BTW, by monitoring the browser http traffic, I get that the node
expanding(without postback) is not using ajax. I think the page should
have
already loaded the nodes in response html and just use script to display
them when you expanding nodes. Also, if you think the Page_load event
is
the problem, you can try put the Xml file path setting code into the
"Page_Init" or "Page_PreInit" event to see whether the behavior changes.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments
and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach
the
most efficient resolution. The offering is not appropriate for
situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are
best
handled working with a dedicated Microsoft Support Engineer by
contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.


--------------------
From: "Rich Fowler" <[email protected]>
References: <[email protected]>
<[email protected]>
In-Reply-To: <[email protected]>
Subject: Re: TreeView Dynamic XML Binding
Date: Tue, 25 Mar 2008 17:01:19 -0600


David,

Thanks - quick and great response.

It actually addressed one issue I was interested in and that is turnning
off
the postback at all nodes and expand/collapse images. This opens up my
research into the use of the other events. Implementing as you suggest
removed the error. However, it is not exactly the result I was looking
for.
I want the Expand/Collapse action to happen on the client but the Select
action to be a postback. This works fine when I explicitly set the
DataFile
on the aspx page, as in your code below. However, when I dynamically set
the
DataFile in the Page_Load, the postback generates the null reference error
when clicking the tree node. Any suggesting into what is causing this?

Your response brings up a side question. When I implemented it and
debug
on
the TreeNodeDataBound event, it fires when expanding. Is this using
AJAX?
I'm moving directly from .NET 1.1 to 3.5, so much of this is new to me.

Thanks again.

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.
 
M

Michael Gledhill

It's been a horrible couple of days.
It's my first attempt at binding .xml (and using a .xls file) to an
ASP.Net
Treeview, and every step of the way I've hit problems.

My latest (final ?) problem is pretty much the same as the original one
in this thread, but I'm using the "XmlDataSource.Data" value to pass in my
xml string, rather than "XmlDataSource.DataFile".

My TreeView draws fine, and I have a TreeNodeBinding setup:

<DataBindings>
<asp:TreeNodeBinding DataMember="Rules" TextField="Description"
ValueField="RSRule_ID" PopulateOnDemand="True" />
</DataBindings>


It displays fine, but, just like Rich Fawler's problem, as soon as I click
on a TreeNode, I get the wonderful ASP.Net error:

--------------------------------
Object reference not set to an instance of an object.
....
Exception Details: System.NullReferenceException: Object reference
not set to an instance of an object.
--------------------------------

I'm baffled.

Now, I'm filling my XMLDataSource at run-time, by reading in a
DataSet, then binding it to my Tree control:

XmlDataSource1.Data = XMLstring;
XmlDataSource1.DataBind();
tvSegments3.DataBind();

However, if I stick a breakpoint in there, copy the XMLstring's value
into an .xml file, add the .xml file to my project, and tell the TreeView
to use that (using XmlDataSource1.DataFile), then it all works fine.

But asking XmlDataSource to just use exactly the same data, but via
the "XmlDataSource1.Data" field causes this exception when I click on
a node.
Aaaaaggggggghhhhhhhhh ! Why ? It's the same data ! I don't
understand it...

Please help... any suggestions ?



Mike "off for a large beer" Gledhill

Btw, I tried adding the EnableClientScript="false" tag to my
TreeView.. but this made absolutely no difference.
 
Ad

Advertisements

R

Rich Fowler

Did you move the setting of the data to Page_Init() instead of having it in
Page_Load(). That solved my problem.
Also, when using the XmlDataSource, I did not have to do the bind
operations.
 
M

Michael Gledhill

Hi Rich,
Did you move the setting of the data to Page_Init() instead of having it in
Page_Load(). That solved my problem.

I tried this - I moved the .Data setting into a Page_Init() function:

protected void Page_Init(object sender, EventArgs e)
{
XmlDataSource1.Data = XMLstring;
}

This works, now, when I click on a node, my app doesn't crash
anymore, and you can see the page redraw itself.

Unfortunately, this also has the side-effect that my treeView's
OnSelectedNodeChanged function is completely ignored.

Doing a quick Google search on "TreeView OnSelectedNodeChanged"
results in hundreds of similarly logged error reports from users saying
that their OnSelectedNodeChanged also isn't firing.. a few suggestions
from Microsoft that perhaps you should add a selectAction to your
nodes:

node.SelectAction = TreeNodeSelectAction.SelectExpand;

- (this doesn't fix my issue) - but no actual fix to the problem.


So, when I click on a TreeView node now, my page refreshes, the
Page_Init gets called (but, by this point, my TreeViews SelectedNode
value is null) and I can't work out how to get my ASP.Net code to
call a function when a user clicks on a node.


Can anyone help ?
Or recommend a third-party tree control without issues like this
at every turn ? ;-)


Mike
 
M

Michael Gledhill

Interestingly, if I add a TreeNodePopulate() function to my TreeView,
that also doesn't ever get called. It does manage to call
TreeNodeDataBound() for each node though.

If I ditch using XMLDataSource.Data completely, and go back to using
XMLDataSource.DataFile instead... well... TreeNodePopulate() still
doesn't ever get called and SelectedNodeChanged() continues to be
ignored as before.


So, unless I'm missing something obvious (other than sleep and
my sanity), when you're using ASP.Net TreeViews with an XML
Data Source, you just can't trap nodes being clicked on.


Oh, one other note:
If I add an OnClick value in my <asp:Tree> tag, it will happily
call that. So, with the following code, I *will* see a JavaScript
dialog appear when I click on a node.
Good to see that JavaScript works, if nothing else !


<asp:TreeView
ID="tvSegments3"
runat="server"
DataSourceID="XmlDataSource1"
OnDataBinding="tvSegments3_DataBinding"
EnableViewState="False" MaxDataBindDepth="10"
CssClass="tree" ImageSet="Simple2"
OnSelectedNodeChanged="tvSegments3_SelectedNodeChanged"
OnTreeNodeDataBound="tvSegments3_TreeNodeDataBound"
EnableClientScript="true"
onclick="alert('ASP.Net Treeviews are incredibly unreliable.');
return false;"
... etc ....


But, still no solution to being able to call an ASP.Net function when
I click on a Node.

(Quick node: my TreeView is NOT in an UpdatePanel. Some of the
many TreeView complaints on the web seem to be a result of having
TreeViews in UpdatePanels, but this is not my problem. I've also
tried changing the EnableClientScript, PathSeparator and any other
TreeView variable that might make a difference. They don't.)


Mike
 
M

Michael Gledhill

Hi Mike,

Yeeeeah, I'd give up going down that route if I were you.

Try sticking a button on your page, which, when clicked, attempts to
select the first Node in your TreeView.

protected void Button1_Click(object sender, EventArgs e)
{
if (tvSegments3.Nodes.Count == 0)
return;

TreeNode tn = tvSegments3.Nodes[0];
tn.Select();
}

You'll find that when you click on it, it'll find the first node in your
tree, successfully "select" it, but again, the SelectedNodeChanged
function will never get called.

Why ?

Well, because it'll cause a PostBack, which'll empty the data from
your TreeView when the page redraws, so any attempt to select your
Node which no longer exists will fail miserably.


We would have fixed this, but couldn't be bothered.
Besides, we had more interesting Silverlight coding to do instead, so
gave up on this ASP.Net feature. We didn't really think it all through,
did we ? Mixing SelectedNodeChanges with a TreeView whose data
wasn't static.
Aaah, we doubt anyone will notice.

Sorry for wasting the last 3 days of your development time.


Have a nice day now, y'hear ?



Mike

Michaelsoft Developer Network
 
R

Rich Fowler

Michael,

Mine worked simply by moving the DataFile assignment to the Page_Init event.
I did not do any binding methods. Also, I did not define a DataBindings
section in my asp:Treeview control. I did not need this. I found that
working with an XML file automatically performed PopulateOnDemand
funcationlity. I would try taking that option out of your
asp:TreeNodeBindings node (this is just a trial and error suggestion). My
onselectednodechanged function is firing correctly. Below is all I do:

Aspx web page
<asp:TreeView ID="cSchemaTree" runat="server" ExpandDepth="1"
DataSourceID="cSchemaXml"
Font-Bold="False"
onselectednodechanged="cSchemaTree_SelectedNodeChanged">
</asp:TreeView>

<asp:XmlDataSource ID="cSchemaXml" runat="server"></asp:XmlDataSource>


aspx.cs code
protected void Page_Init()
{
cSchemaXml.DataFile = @"D:\TestFiles\TestFile.xml";
}

protected void cSchemaTree_SelectedNodeChanged(object sender,
EventArgs e)
{
....
}

Rich
 
Ad

Advertisements

M

Michael Gledhill

Hi Rich,

Thanks for your help.

Unfortunately, after three days of fighting, I have surrendered. ASP.Net's
TreeView, when used with the "XmlDataSource.Data" field simply isn't
stable, and I've had to ditch the XML side of my code.

Yesterday, I *manually* filled my TreeView, taking data directly from my
DataSet, and putting it into the relevant branches, and all of my problems
have gone away.

It's interesting how many other TreeView threads there are about this
same problem, many from a few years ago, and almost all simply "die" -
they never have a resolution or a "thanks ! that solved my problem !"
at the end.

I just don't think the .Data field works properly, or is worth the effort.


Btw, the closest I got to being able to track when a user had clicked
NameValueCollection col = Request.Form;

object targetObj = col["__EVENTTARGET"];
object argObj = col["__EVENTARGUMENT"];
object nodeObj = col["ctl00_MainContentHolder_tvSegments_SelectedNode"];

...to idenitfy when a Postback had occured due to a user clicking on
a Tree node, then manually searching all of the TreeView nodes for
the one containing the primary key (from the __EVENTARGUMENT
param).

Yeah, disgusting, isn't it ? However, it worked nicely, I could find the
correct tree node that was clicked on, but, again, if I ran
selectedNode.Select() then it still doesn't call the TreeView's
SelectedNodeChanged() function.

I also had a look at the JavaScript created by the TreeView control, to
see if I could manually do a __postback from JavaScript, as this was
correctly being called when clicking on a Node, but had to give up.


Basically, the TreeView isn't reliable when using XmlDataSource and
it's .Data field, and I couldn't base my application on controls that are
this unstable, untested, and unfinished.


Mike
 
Ad

Advertisements

P

priyanka bhatt

hi this was the same error i was getting
try removing the line

ExpandDepth="1"

i had written this line in Page_Load event and after removing it my code worked just fine.

i hope it helps..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top