Question

In my application, I defined an XMLDataSource in the ASPX:

<asp:XmlDataSource ID="XmlThickness" runat="server"
    DataFile="~/XML/Data/products.xml" EnableCaching="False" 
    EnableViewState="False" 
    ></asp:XmlDataSource>

This datasource feeds a grid.

In my code, I set the XPath at a specific point in a specific function, and then issue a new DataBound() on the XMLDataSource, causing the grid to update:

XmlThickness.XPath = "/Item[@label='" + tvwMaterials.SelectedNode.Text + "']/Thickness/Thick";
XmlThickness.DataBind();

All of this works a charm, but when I attempt to read out the XPAth from within a different function, I receive an error message.

XmlNode thick = myDataSource.SelectSingleNode(XmlThickness.XPath.ToString());

Debugging shows the XPath expression actually is empty.

I attempted to move the creation of the DataSource to code, and only then setting the DataSourceID of the grid, but that did not work.

Prior to trying this, I defined a default XPath in the XMLDataSource definition, and at that point, the XMLNode thick line returned this default string.

I'm a bit confused as to why the XPath is not retained at the XMLDataSource afdter I explicitly set it in the earlier function. Could someone shed some light on this for me, please?

Was it helpful?

Solution

XmlDataSource nodes are not stored in the viewState, like most datasource controls. You should call databind only once, and the control should rebuild itself automatically on postback. I case you want to change datasource, you can save the selectedNode form the treeviw in Viewstate, and on postback, change your dataSource, by retrieving the value from viewState, before you assign the DataSourceID and call DataBind(). Have a look here:

http://forums.asp.net/t/1437802.aspx/1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top