Wednesday, November 19, 2008

Cascading Drop downs in InfoPath Web Forms made easy

To create user‐friendly web forms, cascading drop down list boxes is a must, but InfoPath Form Services doesn’t allow you to use them, like you can use them with InfoPath Client.
(Filter Data on DataConnections/DataSources are not allowed with InfoPath Web Forms)


This article explains how to use Cascading Drop down list boxes within an InfoPath Web Form in a very easy and re-usable way.

Please download the PDF file containing the step by step guide

HOW_TO_use_Cascading_Dropdown_ListBoxes_within_an_InfoPath_Web_Form.pdf

27 comments:

KapilK said...

Can you suggest me somethings...

I have got
- Web form
- cascading drop downs with large drop down lists with up to 12000 items
- Web service as drop down data source.

My questions are,
1) What would be best approach to let user select from large list?
Can you suggest controls or methods?

2) What can we do to let user fast select from combo list?

Kapil

Unknown said...

Thanks, this worked well. There is a typo in the last line of code:

items = xnFD.Selct(xPathString, this.NamespaceManager);

Selct should be Select.

Rich said...

Hi, I can't bind a drop down list to my data connection as "Binding a non-repeating control to a repeating group is not supported bi InfoPath forms service".

I don't need to create cascading drop downs, but I need to create one drop down on a browser enabled form. The drop down will display a list of items returned from a particular view of a sharepoint list. How can I do this?

Many thanks

Anonymous said...

Hello,

Just a little question: when i selected the first dropdown the second one filters just fine, but for some reason my first dropdown looses all itens but the selected one....

what did i do wrong? i did exactally what is specified on pdf file! =D

thx for your attention

Unknown said...

Hi Federick,

Is there a solution that I could download based on your cascading drop down article?

I downloaded the pdf guide, unfortunately, i'm having trouble following it.

Your expert advise is much appreciated

Scott said...

By any chances could you please share the xsn file you used in your example with us? I follow this instruction and get error at runtime.

Unknown said...

cmpsfc:

You need to remove the first two lines of the code, where in the comments you see "Set the Value of the Cascading Dropdown in the Main Data Source to empty" and then the new value will stay. I don't know why is it proposed to clear the value, may be it depends on application.

Regards,
Alexey Zimarev

Unknown said...

I also get an error: The name 'selectedMainValue' does not exist in the current contex. Any ideas on what might cause that?

Unknown said...
This comment has been removed by the author.
Unknown said...

The answer to my earlier post: I misspelled
“selectedMainValue” in the variable declarations in the
“PopulateCascadingDropDown” function.

There is still one other issue with this code: 

Description: The second line in the
“PopulateCascadingDropdown” function (as referenced by Alexey
above) is causing the error.  It is essential to have code that clears the
cascading dropdown, however this code clears the Region dropdown (the same
dropdown that is running the code) and causes an error.

 The Fix: Replace Text1 with Text2:

 Text1:
xnMain.SelectSingleNode(CascadingDropDownDataSourceField,
this.NamespaceManager).SetValue(string.Empty);

 Text2:
xnMain.SelectSingleNode("/my:myFields/my:Country",
this.NamespaceManager).SetValue(string.Empty);

Tobe said...

Hello together,

i'm get an error at this line in the code:

XPathNodeIterator items = xnFD.Select(xPathString, this.NamespaceManager);

I have the german version so the error is not exactly in english:
"XPathExceptoion was not treated by user code"

I'm not shure what i have to set in the "ForeignKey" string.

Has anyone an example form?
thanks!

Houssam Zein said...

What format the data connections should be?

Should I have them XML, sharepoint list.

can someone please clarify?

thanks

Unknown said...
This comment has been removed by the author.
Unknown said...

Tobe - foreignkey should be the name of the field in the data source you are filtering. i.e. you have a sharepoint list with managers and employees. To filter employees by manager, the foreignkey should be "Manager"
Mindraider - I used sharepoint lists. I'll try to come up with a good example using xml and post a link soon.

SSS said...

Can we have the cascading dropdownlist working on Repeating table. My problem is that when I select the field from the cascading dropdownlist, it shows only first field even if I selected other field. What is the best solution for this kind of problem?

Unknown said...

Sagun, can you provide more specifics?

SSS said...

Hello Marko,
My problem is that I followed this one for my Purchase Request Form. When I choose the value from the first dropdownlist, it shows the desired value on the second dropdownlist .
I have Category Field and CategoryItem Field. When I select Value from the Category dropdownlist, it shows values depend upon Category dropdownlist on Item dropdownlist.

It works fine for the first row. But when I insert the second item, I was able to select the value for both Category and Item dropdown list but first value for the Item dropdownlist shows blank field. And when I try to select the value for the item dropdownlist that is blank, it shows the value for the item that I selected for the category on the second row. I don’t know, it may be due to the problem of repeating table. Please take a look on it. And If you find any solution on it, please do reply me.

SSS said...

Hello,
Frederik Van Lierde,
Thanks for your work. But I am facing difficulty while implementing it on the Repeating table. First row works fine but when you add next item, first row cascading dropdownlist goes empty and when you try to select the value for it, it shows the values that depends on what you selected on the second row of dropdown list.
If you have any solution on it, please post it.

Sagun

Unknown said...

Sagun, I think I found something that might help:

http://www.infopathdev.com/forums/p/9659/34136.aspx.

This method would let you populate whatever you want in the combobox and add the data to a repeating table. Hope this helps.

Tvan said...

Hello Marko,
I'm a newbie and I've spent a week trying to get this cascading dropdown with repeating table to work. Things breakdown when I try to add another row to the repeating table.

private void PopulateCascadingDropDown(string selectedMainValue,
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];
//my:myFields/my:group1/my:group2/my:field2
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 + "!='" +
selectedMainValue + "']";
//-- 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);

}
}

public void CTRL30_5_Clicked(object sender, ClickedEventArgs e)
{
const string f1 = "/my:myFields/my:group1/my:group2"; //repeating group name
XPathNavigator root = e.Source; //assign to root navigator

XPathNodeIterator rows = root.Select(f1, NamespaceManager); //use select to iterate

string id = rows.Current.SelectSingleNode("/my:myFields/my:field1", NamespaceManager).Value; //this returns value of the current row

//XPathNavigator root = MainDataSource.CreateNavigator();

string dataConnectionName = "testDetail";
string dataSourceName = "testDetail";
string ForeignKey = "masterid";
string CascadingDropDownDataSourceField = "/my:myFields/my:group1/my:group2/my:field2";
//string selectedMainValue = root.SelectSingleNode("/my:myFields/my:field1", NamespaceManager).Value;
string selectedMainValue = id;
string repeatingNode = "/dfs:myFields/dfs:dataFields/d:testDetail";
PopulateCascadingDropDown(selectedMainValue, dataConnectionName,
dataSourceName, repeatingNode, ForeignKey, CascadingDropDownDataSourceField);

}
Can someone please help me to get the repeating table to work when I add a new row? Thank you in advance. I'm very new to InfoPath so an example would be very helpful. Thanks.

Vikas Salvi said...

Thnks a lot!!!!1

Unknown said...

this dont seem to work for me...

Is it possible to do this with 1 XML file and if not what is the construction of the XML??

the current consturction i use is this

(exchanged the <> with -)
-SalarisSchalen-
-CaoKinderOpvang-
-Schalen-
-Schaal-1-/Schaal-
-Schaal-2-/Schaal-
-/Schalen-
-Inpasnummers-
-Inpasnummer schaal="1"-1-/Inpasnummer-
-Inpasnummer schaal="1"-2-/Inpasnummer-
-Inpasnummer schaal="2"-2-/Inpasnummer-
-Inpasnummer schaal="2"-3-/Inpasnummer-
-/Inpasnummers-
-Salarissen-
-Salaris schaal="1" inpasnummer="1"-1406-/Salaris-
-Salaris schaal="1" inpasnummer="2"-1445-/Salaris-
-Salaris schaal="2" inpasnummer="2"-1486-/Salaris-
-Salaris schaal="2" inpasnummer="3"-1524-/Salaris-
-/Salarissen-
-/CaoKinderOpvang-

Greetings
Wouter Goeman

pb45102 said...

The PDF does not seem to be available any longer. Is the URL incorrect or has the article been pulled?

Pam

Ghazaleh Firouz said...

the file is not available... where we can reach the solution?

Karthik Murugesan said...

The link to the PDF file isn't working anymore. I love to see how you did it. Can you please fix the link?

Stukkend said...

Can we please get a proper link to the pdf file? the one in this post no longer exist.

Lydia said...

You can find the pdf here
http://www.scribd.com/doc/23282823/HOW-TO-use-Cascading-Dropdown-ListBoxes-within-an-InfoPath-Web-Form