How to Filter a DropDownList from another DropDownList (SPDataSource + ControlParameter way)
I’m trying to connect two DropDownList (ddlCountry and ddlCity) together. I want that when the user changes the Country, the ddlCity is updated consequently.
To do it, I created two lists:
· Country (ID, Title)
· City (ID, Title, Country) [Country is a lookup field]
Then I created an ASPX page with 2 DropDownList connected to Country and City through 2 SPDataSource (dsCountry and dsCity).
When I load my page, the City DropDownList is populated accordingly t开发者_StackOverflowo the Country DropDownList, but when I change the Country, nothing happens and ddlCity shows null always This is my code:
<SharePoint:SPDataSource runat="server" DataSourceMode="List" UseInternalName="true" selectcommand="<Query><OrderBy><FieldRef Name='Title' /></OrderBy></Query>" id="dsCountry">
<SelectParameters>
<asp:Parameter Name="ListName" DefaultValue="Country"/>
</SelectParameters>
</SharePoint:SPDataSource>
<p>Country:
<asp:DropDownList runat="server" id="ddlCountry" DataValueField="Title" DataTextField="Title" DataSourceID="dsCountry" AutoPostBack="True" />
</p>
<SharePoint:SPDataSource runat="server" DataSourceMode="List" UseInternalName="true" selectcommand="<Query><Where><Eq><FieldRef Name="Country" /><Value Type="Lookup">{country}</Value></Eq></Where></Query>" id="dsCity">
<SelectParameters>
<asp:parameter DefaultValue="City" Name="ListName"></asp:parameter>
<asp:controlparameter name="country" controlid="ddlCountry" propertyname="SelectedValue"/>
</SelectParameters>
</SharePoint:SPDataSource>
<p>City:
<asp:DropDownList runat="server" id="ddlCity" DataValueField="Title" DataTextField="Title" DataSourceID="dsCity" />
</p>
Please note:
· In ddlCountry AutoPostBack = True
· In dsCity, in the CAML query there’s a parameter called “country”, connected to ddlCountry through a ControlParameter
I don’t understand why my filtering doesn’t work when I change the country… I can see the page reloading, but nothing happens… any suggestion?
Shouldn't the variable be:
{$country}
Interested in knowing if you ever found a solution.
I realise this is serious thread necromancy, but in case you're still interested, or anyone else finds this through Google...
Try setting the EnableViewState
attribute of the second (filtered) dropdown to false
. This will force the control to get a new set of data (with the new filter) from the SPDataSource, instead of getting its previous set from before the post back from the View State.
Have a look at this page for this specific example, and this page for a more general overview of the View State and what it does.
精彩评论