Re: TreeView Dynamic XML Binding



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:
msdnmg@xxxxxxxxxxxxxx

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



--------------------
From: "Rich Fowler" <RJF@xxxxxxxxxxxxxxxx>
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 [MSFT]" <stcheng@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:sGpXHX%23jIHA.6264@xxxxxxxxxxxxxxxxxxxxxxxxx
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:
msdnmg@xxxxxxxxxxxxxx

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

--------------------
From: "Rich Fowler" <RJF@xxxxxxxxxxxxxxxx>
References: <E25C0122-2976-4D54-8836-0B382773ED16@xxxxxxxxxxxxx>
<a0e9b9da344298ca5ca0634b8a3a@xxxxxxxxxxxxxxxxxxxx>
<D0E82462-8CC5-4885-B8E0-D7747D7B4FDD@xxxxxxxxxxxxx>
<N0LRVpvjIHA.1500@xxxxxxxxxxxxxxxxxxxxxx>
In-Reply-To: <N0LRVpvjIHA.1500@xxxxxxxxxxxxxxxxxxxxxx>
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.


"Steven Cheng [MSFT]" <stcheng@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:N0LRVpvjIHA.1500@xxxxxxxxxxxxxxxxxxxxxxxxx
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:
msdnmg@xxxxxxxxxxxxxx

==================================================
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" <RJF@xxxxxxxxxxxxxxxx>
References: <E25C0122-2976-4D54-8836-0B382773ED16@xxxxxxxxxxxxx>
<a0e9b9da344298ca5ca0634b8a3a@xxxxxxxxxxxxxxxxxxxx>
In-Reply-To: <a0e9b9da344298ca5ca0634b8a3a@xxxxxxxxxxxxxxxxxxxx>
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" <dlongnecker@xxxxxxxxxxxxxxxx> wrote in message
news:a0e9b9da344298ca5ca0634b8a3a@xxxxxxxxxxxxxxxxxxxxxxx
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.












.



Relevant Pages

  • Re: TreeView Dynamic XML Binding
    ... the entire xml structure is ... otherwise the entire tree reset. ... (Notice no DataSourceId attribute or XmlDataSource control). ... telling it to .Expand rather than .Select. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: TreeView Dynamic XML Binding
    ... in the web page (which for the actual xml makes it very slow). ... It seems as if the default settings are doing the populate on expand ... otherwise the entire tree reset. ... (Notice no DataSourceId attribute or XmlDataSource control). ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: TreeView Dynamic XML Binding
    ... Subject: TreeView Dynamic XML Binding ... Expand the ReturnHeader node. ... When I click on the tree node for the select ... I'm simply trying to display the xml element tree ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: TreeView Dynamic XML Binding
    ... I do not provide any databinding because I have no control of the element names and do not know them in advance. ... The test xml file is not in the website folder as in your code. ... you need to expand a node and click on one of the elements in the expanded node. ... When I click on the tree node for the select ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: TreeView Dynamic XML Binding
    ... By monitoring the treeview child nodes when I explicitly set the XmlDataSource.DataFile, I determined that the process is actually using the PopulateOnDemand processing. ... I'm going to put the xml document into a session variable. ... It seems as if the default settings are doing the populate on expand ... otherwise the entire tree reset. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)