XPATH Filter not working

Christian de Bellefeuille
Christian de Bellefeuille used Ask the Experts™
on
I'm trying to do this:
  • Display 2 DropDownList (one for Country, one for State)
  • When the user select a Country, it show the states or provinces for this country
  • The data where i get the country is an XML, but the choice has to be store in our MS-SQL Database using a StoreProc

I've this piece of code which affect XPath to filter the XMLDataSource for the states.  In this example i've just hardcoded "US" but i'll change that for the value taken from the Country DDL later.

protected void Country_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList list = sender as DropDownList;
        XmlDataSource wSource = (XmlDataSource)BYS.Web.UI.ControlExtensions.FindControlRecursive(this.Controls[0], "data_state");

        string wActualValue = list.SelectedValue;
        wSource.XPath = "//state[@country='US']";

    }

Open in new window


When i do this, i've this error message on the aspx web page:
(the error message is in french, i'll translate as best as i can)
The data link methods such as Eval(), XPath() and Bind() can only be used within a context of a Control linked to data
Les méthodes de liaison de données telles que Eval(), XPath() et Bind() peuvent uniquement être utilisées dans le contexte d'un contrôle lié aux données.

The XML files look like this...
Country
<?xml version="1.0" encoding="utf-8"?>
<countries>
  <country iso="CA" name="Canada" />
  <country iso="US" name="États-Unis" />
</countries>

Open in new window


State
<?xml version="1.0" encoding="utf-8"?>
<states>
  <state iso="QC" country="CA" name="Québec" />
  <state iso="ON" country="CA" name="Ontario" />
  <state iso="MO" country="US" name="Missouri" />
  <state iso="NY" country="US" name="NewYork" />
</states>

Open in new window


The ASPX file contain a FormView linked to the DS of our MS-SQL Server like this:
<asp:FormView ID="f_organisation" runat="server" DefaultMode="Edit" DataSourceID="data_organisation" DataKeyNames="CustID" Width="100%">
<EditItemTemplate>
<asp:DropDownList ID="i_addrprov" runat="server" Width="15em" DataSourceID="data_state" DataTextField="name" DataValueField="iso" SelectedValue='<%# Bind("CustAddrProv") %>'  />
<asp:XmlDataSource ID="data_state" runat="server" DataFile="~/App_Data/state.xml" XPath="//state" />
</EditItemTemplate>
</asp:FormView>

Open in new window


The CustAddrProv is the field where we stored the Customer Province/State in our database.

Anyone can give me a hint?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I've found a way to get the exact error message in english:
"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
Ryan ChongSoftware Team Lead

Commented:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

are you able to highlight which line of codes generated that error?

I have done a simple test below, seems working fine for me.

<asp:DropDownList ID="Country" runat="server" Width="15em" DataSourceID="data_country" DataTextField="name" DataValueField="iso" OnSelectedIndexChanged="Country_SelectedIndexChanged" AutoPostBack="True"  />
            <asp:XmlDataSource ID="data_country" runat="server" DataFile="~/App_Data/country.xml" XPath="//country" />
            <asp:DropDownList ID="i_addrprov" runat="server" Width="15em" DataSourceID="data_state" DataTextField="name" DataValueField="iso"  />
            <asp:XmlDataSource ID="data_state" runat="server" DataFile="~/App_Data/state.xml" XPath="//state" />

Open in new window


protected void Country_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList list = sender as DropDownList;
            XmlDataSource wSource = (XmlDataSource)Page.FindControl("data_state");

            string wActualValue = list.SelectedValue;
            wSource.XPath = "//state[@country='"+ wActualValue + "']";
        }

Open in new window

Author

Commented:
It's on this line of the aspx

<asp:DropDownList ID="i_addrprov" runat="server" Width="15em" DataSourceID="data_state" DataTextField="name" DataValueField="iso" SelectedValue='<%# Bind("CustAddrProv") %>'  />

Open in new window


The difference between your code and mine is that yours doesn't store the result anywhere, while what i need is to store the 2 letter (ISO) state name in an MS-SQL database.  That's the reason why i've added Bind in the SelectedValue.

How can we do this?
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Ryan ChongSoftware Team Lead

Commented:
I don't have your codes behind for troubleshooting, but what if you remove:
SelectedValue='<%# Bind("CustAddrProv") %>

Open in new window


from the code above? Is there any error encountered then?

you may consider to populate the selected Value in code behind instead?

Author

Commented:
There's no error, but i already known this.  I thought that maybe i was doing something wrong.   The only difference between the Country and State DDL is the change of the XPath in the code behind.   The country xpath is not changing, and it work very well.

Are you aware of any reason why its not working this way?   The way i see it is that the DDL take the XML in it's DataSourceObject, and usually when a control is bound it's in "DataSourceID" or "DataSource" and trying to have 2 source make it clash.

One way to solve this problem is to fill the DDL, not by binding its content but only it's selected value.
The other way would be to fill the Selected Value on load and force and to update the DataSource on change.
Software Team Lead
Commented:
to me, it seems to be a binding issue where it causing that error. hence, i would try to do some debugging (by removing the binding part in front end)

i can't replicate a fully workable solution as same as what currently you have, so i only can provide you with some suggestions.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial