Friday, January 16, 2009

Thank You - TOP 100 and Cascading Dropdows

I want to thank all of you interested in InfoPath.  I became a TOP 100 contributor on the InfoPathDev.com website, and I see several times that the solutions I gave are working.

Also when searching for How to use Cascading Dropdown ListBoxes with InfoPath Services (webbassed) I see many companies implemented the out-of-the-box solution I proposed in my article and many others are using the link in their blogs and replies on forums.

Thanks!

2 comments:

ChrisR said...

Does your solution for cascading dropdowns work within a repeation section on a form? I am not able to get it to work.

dvn said...

My situation is slightly deviated from your example and i could not get it to work completely. Can you tell me why?
I have two tables:
table1 has two columns named id, location and location column will be display in dropdownlist1.
table2 has many columns including the resource column that i want to display in dropdownlist2 when the user select a name from dropdownlist1, content of dropdownlist2 should dipslay the right choices only. The two tables are linked by id.
Here are my two dataconnections query:
1. select "LOCATION" from "dbo"."ScheduleLocation" as "Location"
2. shape
{select "LocationID","Resource","RowVersion" from "dbo"."ScheduleResource" as "ScheduleResource"} as "ScheduleResource"
append
({select "LocationID","LOCATION" from "dbo"."ScheduleLocation" as "ScheduleLocation"}
relate "LocationID" TO "LocationID") as "ScheduleLocation"

And here are my event handler:
public void Location_Changed(object sender, XmlEventArgs e)
{

string dataConnectionName = "ScheduleResource";
string dataSourceName = "ScheduleResource";
string ForeignKey = "LOCATION";
string cascadingDropDownDataSourceField = "/my:myFields/my:Resource";
string selectedLocationValue = e.NewValue;
string repeatingNode = "/dfs:myFields/dfs:dataFields/d:ScheduleResource/d:ScheduleLocation";
//string repeatingNode = "/dfs:myFields/dfs:dataFields/dfs:ScheduleResource";
PopulateCascadingDropDown(selectedLocationValue, dataConnectionName, dataSourceName, repeatingNode, ForeignKey, cascadingDropDownDataSourceField);
}

private void PopulateCascadingDropDown(string selectedLocationValue, string dataConnectionName, string dataSourceName, string repeatingNode,
string ForeignKey, string cascadingDropDownDataSourceField)
{
//-- Set the Value of the Cascading Dropdown in the Main
//-- Data Source to empty --
//-- This is needed, the previous selected node will stay
//-- in the list --
XPathNavigator xnMain = this.MainDataSource.CreateNavigator();
xnMain.SelectSingleNode(cascadingDropDownDataSourceField,
this.NamespaceManager).SetValue(string.Empty);
//-- Execute the DataConnection --
DataConnection dcSecond = this.DataConnections[dataConnectionName];
dcSecond.Execute();
//-- Get a Navigator to the Secondary DataSource, just created by
//-- previous line of code --
XPathNavigator xnFD =
this.DataSources[dataSourceName].CreateNavigator();
//-- Create xPathString: Select all nodes where the foreignkey is
//-- different from the selected Value --
string xPathString = repeatingNode + "[@" + ForeignKey + "!='" +
selectedLocationValue + "']";
//-- Get the nodes, corresponding the xPathString --
XPathNodeIterator items = xnFD.Select(xPathString,
this.NamespaceManager);
while (items.Count != 0)
{
xnFD.SelectSingleNode(xPathString,
this.NamespaceManager).DeleteSelf();
items = xnFD.Select(xPathString, this.NamespaceManager);
}
}
The problem was whenever the user select a location from the first dropdownlist, the content of the second dropdownlist always the same which contained very thing.
thank you in advance