<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Sharepoint 2007 Load DropDownList from list that depends on other</Title>
      <Shortcut>Sharepoint2007LoadDropDownListfromlistthatdependsonother</Shortcut>
      <Description>Sharepoint 2007 Load DropDownList from list that depends on other [C#]</Description>
      <Author>Jaime López</Author>
      <HelpUrl>/PreviewSnippet.aspx?SnippetID=fda18110-98db-42ef-98ca-04324bd6ccf9</HelpUrl>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="csharp"><![CDATA[/// <summary>
		/// Generic method to load information in a DropDownList control.
		/// </summary>
		/// <param name="ddl">DropDownList control.</param>
		/// <param name="list">List to get data.</param>
		/// <param name="titleField">Column to get the text for the elements.</param>
		/// <param name="valueField">Column to get the value for the elements.</param>
		/// <param name="ddlDependsOn">This DropDownList control is necessary for fill the DropDownList.</param>
		/// <param name="filterValue">Column to filter values from the DropDownList that depends on.</param>
		private void LoadDropDownList( DropDownList ddl, SPList list, string titleField, string valueField, 
			DropDownList ddlDependsOn, string filterField )
		{
			if ( ddlDependsOn.SelectedIndex < 1 )
			{
				ddl.Items.Clear();
				ddl.Enabled = false;
			}
			else
			{
				ddl.Enabled = true;
				ArrayList elements = new ArrayList();
				ListItem li = new ListItem();
				li.Text = "No filtrar";
				li.Value = "0";
				ddl.Items.Add( li );
				for( int i = 0; i < list.ItemCount; i++ )
				{
					li = new ListItem();
					try
					{
						string filter = "";
						if ( null == list.Items[i].ContentType.Fields.GetField(filterField) )
							filter = list.Items[i][list.Items[i].Fields[filterField].InternalName].ToString();
						else
							filter = list.Items[i][list.Items[i].ContentType.Fields[filterField].InternalName].ToString();
						if ( filter.Trim().CompareTo( ddlDependsOn.SelectedItem.Text.Trim() ) == 0 )
						{
							if ( null == list.Items[i].ContentType.Fields.GetField(titleField) )
								li.Text = list.Items[i][list.Items[i].Fields[titleField].InternalName].ToString();
							else
								li.Text = list.Items[i][list.Items[i].ContentType.Fields[titleField].InternalName].ToString();
							if ( null != list.Items[i].Fields.GetField( valueField ) )
								li.Value = list.Items[i][list.Items[i].Fields[valueField].InternalName].ToString();
							else
								li.Value = list.Items[i][list.Items[i].ContentType.Fields[valueField].InternalName].ToString();
							if ( !elements.Contains( li.Text ) )
							{
								elements.Add( li.Text );
								ddl.Items.Add( li );
							}
						}
					}
					catch ( Exception )
					{
						// This exception is for the elements that are in blank or doesn't exists.
					}
				}
			}
		}]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>