Re: TreeView Dynamic XML Binding
- From: "Rich Fowler" <RJF@xxxxxxxxxxxxxxxx>
- 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/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 [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><a0e9b9da344298ca5ca0634b8a3a@xxxxxxxxxxxxxxxxxxxx>
References: <E25C0122-2976-4D54-8836-0B382773ED16@xxxxxxxxxxxxx>
In-Reply-To: <a0e9b9da344298ca5ca0634b8a3a@xxxxxxxxxxxxxxxxxxxx>
Subject: Re: TreeView Dynamic XML Binding
Date: Tue, 25 Mar 2008 17:01:19 -0600
off
David,
Thanks - quick and great response.
It actually addressed one issue I was interested in and that is turnningthe postback at all nodes and expand/collapse images. This opens up myfor.
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
I want the Expand/Collapse action to happen on the client but the SelectDataFile
action to be a postback. This works fine when I explicitly set the
on the aspx page, as in your code below. However, when I dynamically settheDataFile in the Page_Load, the postback generates the null reference erroron
when clicking the tree node. Any suggesting into what is causing this?
Your response brings up a side question. When I implemented it and debugthe TreeNodeDataBound event, it fires when expanding. Is this using AJAX?of
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 offasan 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 plain text.
Since EnableClientScript is still enabled, the control generates the
according JavaScript and the Expand links do not fire postbacks. By
default, I think, the TreeNodeSelectAction is set to Select, not Expand,
which fires the SelectedNodeChanged event.
Hope this helps!
-dl
--
David R. Longnecker
http://blog.tiredstudent.com
I'm dynamically binding an XmlDataSource to an xml file in the
Page_Load event. This XmlDataSource control is the DataSourceID for a
TreeView control. I'm receiving a null reference error prior to
entering the Page_Load event when selecting a tree text value.
Page_Load code snippet:
cSchemaXML.DataFile = @"D:\Schemas\2008\Schema_Tree.xml";
Note: The file path above will be calculated once I get it working.
This is for testing only. The code is not in a !this.IsPostBack
conditional.
Aspx page snippet:
<asp:TreeView ID="cSchemaTree" runat="server"
DataSourceID="cSchemaXML"
ExpandDepth="1">
</asp:TreeView>
<asp:XmlDataSource ID="cSchemaXML"
runat="server"></asp:XmlDataSource>
When I click on the tree node (not the Expand icon) for the select
processing, a null reference error (see below) is displayed.
The only way I can get this to work without a the error is to disable
client
side code:
EnableClientScript="False"
I prefer not to suffer the flashing and time required during
postbacks. I'm simply trying to display the xml element tree
structure. The xml being displayed is nested elements, each containing
one attribute. If I set the DataFile in the XmlDataSource control
directly, everything works fine. Do I need to set DataFile someplace
other than in the Page_Load event? Some other solution?
Note: If I click a tree node that is part of the initial display
(based on the ExpandDepth setting) I do not get the error until I
expand the tree and then click the node (or one of the expanded
nodes). This may indicate some variance between the viewstate and the
returned tree structure (only a guess).
Sample XML - the actual xml is much more complex and nested much
deeper, but this gives the idea.
<Company FullPath="Company">
<Name FullPath="Company/Name"/>
<Address FullPath="Company/Address">
<USAddress FullPath="Company/Address/USAddress">
<Street FullPath="Company/Address/USAddress/Street" />
<City FullPath="Company/Address/USAddress/City"/>
<State FullPath="Company/Address/USAddress/State" />
<ZipCode FullPath="Company/Address/USAddress/ZipCode" />
</USAddress>
<ForeignAddress FullPath="Company/Address/ForeignAddress" >
<Street FullPath="Company/Address/USAddress/Street" />
<City FullPath="Company/Address/USAddress/City"/>
<StateOrProvince
FullPath="Company/Address/USAddress/StateOrProvince"
/>
<PostalCode FullPath="Company/Address/USAddress/PostalCode" />
<Country FullPath="Company/Address/ForeignAddress/Country" />
</ForeignAddress>
</Address>
</Company>
The error returned when clicking the tree node is:
Server Error in '/' Application.
----------------------------------------------------------------------
----------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the
current web request. Please review the stack trace for more
information
about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader,
Boolean
preserveWhitespace) +24
System.Xml.XmlDocument.Load(XmlReader reader) +96
System.Web.UI.WebControls.XmlDataSource.PopulateXmlDocument(XmlDocumen
t
document, CacheDependency& dataCacheDependency, CacheDependency&
transformCacheDependency) +305
System.Web.UI.WebControls.XmlDataSource.GetXmlDocument() +154
System.Web.UI.WebControls.XmlHierarchicalDataSourceView.Select()
+14
System.Web.UI.WebControls.TreeView.DataBindNode(TreeNode node) +126
System.Web.UI.WebControls.TreeView.PopulateNode(TreeNode node) +27
System.Web.UI.WebControls.TreeView.LoadPostData(String postDataKey,
NameValueCollection postCollection) +1082
System.Web.UI.WebControls.TreeView.System.Web.UI.IPostBackDataHandler.
LoadPostData(String
postDataKey, NameValueCollection postCollection) +11
System.Web.UI.Page.ProcessPostData(NameValueCollection postData,
Boolean
fBeforeLoad) +661
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+1194
Thanks for any help - Rich F.
.
- Follow-Ups:
- Re: TreeView Dynamic XML Binding
- From: Steven Cheng [MSFT]
- Re: TreeView Dynamic XML Binding
- References:
- TreeView Dynamic XML Binding
- From: Rich Fowler
- Re: TreeView Dynamic XML Binding
- From: David R . Longnecker
- Re: TreeView Dynamic XML Binding
- From: Rich Fowler
- Re: TreeView Dynamic XML Binding
- From: Steven Cheng [MSFT]
- TreeView Dynamic XML Binding
- Prev by Date: Re: Having a checkbox make something available when it's checked.
- Next by Date: Re: TreeView Dynamic XML Binding
- Previous by thread: Re: TreeView Dynamic XML Binding
- Next by thread: Re: TreeView Dynamic XML Binding
- Index(es):
Relevant Pages
|