Link to home
Start Free TrialLog in
Avatar of Christian de Bellefeuille
Christian de BellefeuilleFlag for Canada

asked on

XPATH Filter not working

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
Avatar of Christian de Bellefeuille
Christian de Bellefeuille
Flag of Canada image

ASKER

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."
Avatar of Ryan Chong
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

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?
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?
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.
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial